* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Tree Introduction
Survey
Document related concepts
Transcript
CS 261 – Winter 2010 Trees Trees • Ubiquitous – they are everywhere in CS • Probably ranks third among the most used data structure: 1. Vectors and Arrays 2. Lists 3. 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 B C • Nodes B, D, and E are descendents of node A (as are all other nodes in the tree…except A) D Root (depth = 0, height = 4) Subtree rooted at node C E • E is an interior node • F is a leaf node 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: a Root b d c e f 0 a 1 b 2 c 3 4 d e – Children of node i are stored at 2i + 1 and 2i + 2 – Parent of node i is at floor((i - 1) / 2) 5 6 f 7 Array 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 14 15 f Dynamic Memory Implementation struct node { EleType value; struct node * left; struct node * right; // Left child. // 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 1. Start at root. 2. 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 3. 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: 1. 2. 3. 4. 5. 6. Node, left, right Pre-order Left, node, right In-order Most common traversals. Left, right, node Post-order Node, right, left Subtrees are not Right, node, left usually analyzed Right, left, node 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); s preorder(node.rght); } } a a p e m r l e t Example result: p s a m a e l r t e e e Post-Order Traversal • Process order Left subtree, Right subtree, Node void postorder(BinaryNode node) { if (node != null){ postorder(node.left); postorder(node.rght); s process (node.obj); } } a a p e m r l e t Example result: a a m s l t e e r e p e In-Order Traversal • Process order Left subtree, Node, Right subtree p void inorder(BinaryNode node) { if (node != null){ inorder(node.left); process (node.obj); s inorder(node.rght); } } e a m a r l e t Example result: a sample tree 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: visit – In-order: 2nd visit – Post-order: 3rd visit 1st p s e a m a r l e t e Traversal Example Pre-order: + a * + b c d (Polish notation) + In-order: a * b Post-order: a b c + d * + (reverse Polish notation) d + c a + (b + c) * d (parenthesis added) 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? • Next topic, how do we make a useful data structure out of a tree?