Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

Similar documents
Trees and Tree Traversal

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Binary Trees, Binary Search Trees

Chapter 20: Binary Trees

Trees. CSE 373 Data Structures

Binary Trees

BBM 201 Data structures

Why Do We Need Trees?

Data Structures and Algorithms

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Topic 14. The BinaryTree ADT

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

Trees. (Trees) Data Structures and Programming Spring / 28

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

tree nonlinear Examples

COSC 2011 Section N. Trees: Terminology and Basic Properties

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

CSI33 Data Structures

March 20/2003 Jayakanth Srinivasan,

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Binary Trees. Height 1

CE 221 Data Structures and Algorithms

A set of nodes (or vertices) with a single starting point

Garbage Collection: recycling unused memory

Tree Travsersals and BST Iterators

Trees, Binary Trees, and Binary Search Trees

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Trees: examples (Family trees)

Data Structure - Binary Tree 1 -

CMPT 225. Binary Search Trees

Binary Trees. Examples:

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

Introduction to Computers and Programming. Concept Question

Principles of Computer Science

COMP26120: Algorithms and Imperative Programming. Lecture 1 Trees

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Tree Structures. A hierarchical data structure whose point of entry is the root node

Friday Four Square! 4:15PM, Outside Gates

Tree Data Structures CSC 221

Trees and Graphs. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Trees. Trees. CSE 2011 Winter 2007

CSI33 Data Structures

Programming II (CS300)

CSE 214 Computer Science II Introduction to Tree

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Lecture 26. Introduction to Trees. Trees

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Trees. Eric McCreath

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

CSE 230 Intermediate Programming in C and C++ Binary Tree

a graph is a data structure made up of nodes in graph theory the links are normally called edges

CSCI2100B Data Structures Trees

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6

CSI 402 Spring 2014 Programming Assignment I 1 / 15

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

CS 206 Introduction to Computer Science II

COMP : Trees. COMP20012 Trees 219

Building Java Programs

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

TREES. Trees - Introduction

Binary Trees and Binary Search Trees

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

INF2220: algorithms and data structures Series 1

Trees. Truong Tuan Anh CSE-HCMUT

CSE 12 Abstract Syntax Trees

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Introduction to Binary Trees

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Trees, Part 1: Unbalanced Trees

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

CSC148 Week 6. Larry Zhang

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Transcription:

Trees

Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program, the children may or may not be ordered Like a binary tree, a tree has a root, internal nodes, and leaves A B C D E F G H I J K L M N Each node contains an element and has branches leading to other nodes (its children) Each node (other than the root) has a parent Each node has a depth (distance from the root) 2

More definitions An empty tree has no nodes The descendents of a node are its children and the descendents of its children The ancestors of a node are its parent (if any) and the ancestors of its parent The subtree rooted at a node consists of the given node and all its descendents An ordered tree is one in which the order of the children is important; an unordered tree is one in which the children of a node can be thought of as a set The branching factor of a node is the number of children it has The branching factor of a tree is the average branching factor of its nodes 3

Data structure for a tree A node in a binary tree can be represented as follows: class BinaryTreeNode { Object value; BinaryTreeNode leftchild, rightchild; However, each node in a tree has an arbitrary number of children, so we need something that will hold an arbitrary number of nodes, such as a Vector or a linked list class TreeNode { Object element; Vector children; If we don t care about the order of children, we might use a Set instead of a Vector 4

ADT for a tree It must be possible to: Construct a new tree If a tree can be empty, this may require a header node Add a child to a node Get (iterate through) the children of a node Access (get and set) the value in a node It should probably be possible to: Remove a child (and the subtree rooted at that child) Get the parent of a node 5

A Tree ADT, I Here is a Tree ADT defined in Java Collections by David A. Watt and Deryck F. Brown: public interface Tree { //...method declarations... public interface Node { public Object getelement(); public void setelement(object elem); An interesting aspect of this ADT is that it uses an inner interface An interface can t have an inner class The details are not important for our purposes The inner interface is referred to by Tree.Node 6

A Tree ADT, II public interface Tree { // Accessors public Tree.Node root(); public Tree.Node parent(tree.node node); public int childcount(tree.node node); // Transformers public void makeroot(object elem); public Tree.Node addchild(tree.node node, Object elem); public void remove(tree.node node); // Iterator public Iterator children(tree.node node); // Inner interface for tree nodes public interface Node { public Object getelement(); public void setelement(object elem); 7

Traversing a tree You can traverse a tree in preorder: void preorderprint(node) { System.out.println(node); Iterator iter = node.children.iterator(); while (iter.hasnext()) { preorderprint(iter.next()); You can traverse a tree in postorder: void postorderprint(node) { Iterator iter = node.children.iterator(); while (iter.hasnext()) { postorderprint(iter.next()); System.out.println(node); You can t usually traverse a tree in inorder Why not? 8

Other tree manipulations There s really nothing new to talk about; you ve seen it all with binary trees A tree consists of nodes, each node has references to some other nodes you know how to do all this stuff There are some useful algorithms for searching trees, and with some modifications they also apply to searching graphs Let s move on to some applications of trees 9

File systems File systems are almost always implemented as a tree structure The nodes in the tree are of (at least) two types: folders (or directories), and plain files A folder typically has children subfolders and plain files A folder also contains a link to its parent in both Windows and UNIX, this link is denoted by.. In UNIX, the root of the tree is denoted by / A plain file is typically a leaf 10

Family trees It turns out that a tree is not a good way to represent a family tree Every child has two parents, a mother and a father Parents frequently remarry An upside down binary tree almost works Since it is a biological fact (so far) that every child has exactly two parents, we can use left child = mother and right child = father The terminology gets a bit confusing If you could go back far enough, it becomes a mathematical certainty that the mother and father have some ancestors in common 11

Part of a genealogy Isaac Steven Danielle David Paul a Winfred Carol Chester Elaine Eugene Pauline 12

Game trees Trees are used heavily in implementing games, particularly board games A node represents a position on the board The children of a node represent all the possible moves from that position More precisely, the branches from a node represent the possible moves; the children represent the new positions Planning ahead (in a game) means choosing a path through the tree However You can t have a cycle in a tree If you can return to a previous position in a game, you have a cycle Graphs can have cycles 13

Binary trees for expressions Ordered trees can be used to represent arithmetic expressions + 2 2 The expression 2+2 + 2 * 3 4 The expression 2+3*4 + * 4 2 3 The expression (2+3)*4 To evaluate an expression (given as a node): If it is a leaf, the element in it specifies the value If the element is a number, that number is the value If the element is a variable, look up its value in a table If it is not a leaf, Evaluate the children and combine them according to the operation specified by the element 14

(General) trees for expressions You can use binary trees for expressions if you have only unary and binary operators Java has a ternary operator?: if > x y > = = x y x y max x max y The expression x > y? x : y The statement if (x > y) max = x; else max = y; Trees can be used to represent statements as well as expressions Statements can be evaluated as easily as expressions 15

More trees for statements while (n >= 1) { exp = x * exp; n--; >= n 1 while = ; -- for (int i = 0; i < n; i++) a[i] = 0; int = 0 < for ++ = i n i [ ] 0 exp * n i a i x exp 16

Writing compilers and interpreters A compiler does three things: Parses the input program (converts it into an abstract syntax tree) (Optionally) optimizes the abstract syntax tree Traverses the tree and outputs assembly language or machine code to do the same operations An interpreter does three things: Parses the input program (converts it into an abstract syntax tree) (Optionally) optimizes the abstract syntax tree Traverses the tree in an order controlled by the node contents, and performs the operations as it goes Parsing is usually the hard part, but there is a very simple technique (called recursive descent parsing) that can be used if the language is carefully designed and you don t care too much about efficiency or good error messages 17

I ll never need to write a compiler... Are you sure? If you can t parse text inputs, you are limited to reading simple things like numbers and Strings If you can parse text input, you can make sense of: tell Mary "Meet me at noon" fire phasers at 3, 7 17.25, 0.203 + 8.97i, 0.95i 28 12"48' 3:30pm-5pm Parsing is less important in these days of GUIs, but it s still pretty important Java provides basic support for parsing with its StringTokenizer and StreamTokenizer classes 18

The End 19