230 likes | 396 Views
CS 261 – Winter 2009. Trees. Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: Vectors and Arrays Lists Trees. Tree Characteristics. A tree consists of a collection of nodes connected by directed arcs A tree has a single root node
E N D
CS 261 – Winter 2009 Trees
Trees • Ubiquitous – they are everywhere in CS • Probably ranks third among the most used data structure: • Vectors and Arrays • Lists • Trees
Tree Characteristics • A tree consists of a collection of nodes connected by directed arcs • A tree has a single root node • By convention, the root node is usually drawn at the top • A node that points to (one or more) other nodes is the parent of those nodes while the nodes pointed to are the children • Every node (except the root) has exactly one parent • Nodes with no children are leaf nodes • Nodes with children are interior nodes
Tree Characteristics (cont.) • Nodes that have the same parent are siblings • The descendents of a node consist of its children, and their children, and so on • All nodes in a tree are descendents of the root node(except, of course, the root node itself) • Any node can be considered the root of a subtree • Like a subset, a subtree need not be “proper”(i.e., be strictly smaller than the original) • A subtree rooted at a node consists of that node and all of its descendents
Tree Characteristics (cont.) • There is a single, unique path from the root to any node • Arcs don’t join together • A path’s length is equal to the number of arcs traversed • A node’s height is equal to the maximum path length from that node to a leaf node: • A leaf node has a height of 0 • The height of a tree is equal to the height of the root • A node’s depth is equal to the path length from the root to that node: • The root node has a depth of 0 • A tree’s depth is the maximum depth of all its leaf nodes (which, of course, is equal to the tree’s height)
Tree Characteristics (cont.) A • Nodes D and E are children of node B • Node B is the parent of nodes D and E • Nodes B, D, and E are descendents of node A(as are all other nodes in the tree…except A) • E is an interior node • F is a leaf node Root(depth = 0, height = 4) B C Subtree rooted at node C D E F Leaf node(depth = 4, height = 0)
Tree Characteristics (cont.) Are these trees? Yes No No
Binary Tree • Nodes have no more than two children: • Children are generally ordered from left to right • Full Binary Tree: every leaf is at the same depth • Every internal node has 2 children • Height of n will have 2n+1 – 1 nodes • Height of n will have 2n leaves
Binary Tree • Nodes have no more than two children: • Children are generally ordered from left to right • Full Binary Tree: every leaf is at the same depth • Every internal node has 2 children • Height of n will have 2n+1 – 1 nodes • Height of n will have 2n leaves • Complete Binary Tree: full except for the bottom level which is filled from left to right
Relationship of Height to Number of Nodes • If a complete binary tree has N nodes, what it its height? • We will come back to this when We later have algorithms That run in time Proportional to the Path length
Array Implementation • Complete binary tree have structure that is efficiently implemented with an array: • Children of node i are stored at 2i + 1 and 2i + 2 • Parent of node i is at floor((i - 1) / 2) a Root b c 0 a 1 b 2 c 3 d 4 e 5 f 6 7 d e f
Vector Implementation (cont.) • If the tree is not complete (it is thin, unbalanced, etc.),the Array • implementation will be full of holes a b c d e f 0 a 1 b 2 c 3 4 d 5 6 e 7 8 9 10 11 12 13 f 14 15
Dynamic Memory Implementation struct node { EleType value; struct node * left; // Left child. struct node * right; // Right child. }; Like the Link class in LinkedList: we will use this class in several data structures
Binary Tree Application: Animal Game • Purpose: guess an animal using a sequence of questions • Internal nodes contain yes/no questions • Leaf nodes are animals • Initially, tree contains a single animal (e.g., a “cat”) stored in the root node • Start at root. • If internal node ask yes/no question • Yes go to left child and repeat step 2 • No go to right child and repeat step 2 • If leaf node ask “I know. Is it a …”: • If right done • If wrong “learn” new animal by asking for a yes/no question that distinguishes the new animal from the guess
Binary Tree Traversals • Just as a list, it is often necessary to examine every node in a tree • A list is a simple linear structure: can be traversed either forward or backward – but usually forward • What order do we visit nodes in a tree? • Most common traversal orders: • Pre-order • In-order • Post-order
Binary Tree Traversals (cont.) • All traversal algorithms have to: • Process node • Process left subtree • Process right subtree Traversal order determined by the order these operations are done. • Six possible traversal orders: • Node, left, right Pre-order • Left, node, right In-order • Left, right, node Post-order • Node, right, left • Right, node, left • Right, left, node • Most common traversals. • Subtrees are not • usually analyzed from right to left.
Pre-Order Traversal • Process order Node, Left subtree, Right subtree // Not in the BinaryNode class. void preorder(BinaryNode node) { if (node != null){ process (node.obj); preorder(node.left); preorder(node.rght); } } Example result:p s a m a e l r t e e p s e a m r l a e t e
Post-Order Traversal • Process order Left subtree, Right subtree, Node void postorder(BinaryNode node) { if (node != null){ postorder(node.left); postorder(node.rght); process (node.obj); } } Example result:a a m s l t e e r e p p s e a m r l a e t e
In-Order Traversal • Process order Left subtree, Node, Right subtree void inorder(BinaryNode node) { if (node != null){ inorder(node.left); process (node.obj); inorder(node.rght); } } Example result:a sample tree p s e a m r l a e t e
Binary Tree Traversals: Euler Tour • An Euler Tour “walks” around the tree’s perimeter • Each node is visited three times: • 1st visit: left side of node • 2nd visit: bottom side of node • 3rd visit: right side of node • Leaf nodes are visited three times in succession • Traversal order depends on when node processed: • Pre-order: 1st visit • In-order: 2nd visit • Post-order: 3rd visit p s e a m r l a e t e
Traversal Example Pre-order:+ a * + b c d(Polish notation) In-order:a + (b + c) * d(parenthesis added) Post-order:a b c + d * +(reverse Polish notation) + a * d + c b
Traversals • Computational complexity: • Each traversal requires constant work at each node (not including the recursive calls) • Order not important • Iterating over all n elements in a tree requires O(n) time • Problem with traversal code: • The process function must be rewritten (or replaced) for each new task • Programmer writing new process function sees the internal tree structure representation • Not good information hiding • Solution Iterator (more on this later)
Questions? • Do the worksheet on trees. • Next topic, how do we make a useful data structure out of a tree?