260 likes | 558 Views
CSE 326 Trees. David Kaplan Dept of Computer Science & Engineering Autumn 2001. Trees. Family Trees Organization Charts Classification trees what kind of flower is this? is this mushroom poisonous? File directory structure Parse trees (x + y * z) Search trees
E N D
CSE 326Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001
Trees • Family Trees • Organization Charts • Classification trees • what kind of flower is this? • is this mushroom poisonous? • File directory structure • Parse trees (x + y * z) • Search trees • Non-recursive procedure call chains Non-recursive? (I thought a tree was a recursive structure …) CSE 326 Autumn 2001 2
Definition of a Tree Recursive definition: • empty tree has no root • given trees T1,…,Tk and a node r, there is a tree T where • r is the root of T • the children of r are the roots of T1, …, Tk r T1 T2 T3 CSE 326 Autumn 2001 3
Tree Terminology A root: leaf: child: parent: sibling: ancestor: descendent: subtree: B C D E F G H I J K L M N CSE 326 Autumn 2001 4
More Tree Terminology A depth: height: degree: branching factor: B C D E F G H I J K L M N CSE 326 Autumn 2001 5
YMTT (Yet More Tree Terminology) binary: n-ary: complete: A B C D E F G H I J CSE 326 Autumn 2001 6
r Tree Calculations Example:Longest Undirected Path Find longest undirected path • ignore direction • don’t repeat nodes Observations Longest undirected path is either: • longest path within a subtree • longest path through root If path goes through root, its length is: height(tallest subtree) + height(next-tallest subtree) + 2 Why? CSE 326 Autumn 2001 7
Algorithm Start at root Recursively calculate {LUD path length, height} for each subtree A B C D E F G H I J K L L M N Tree Calculations Example:Longest Undirected Path {?,?} {2,1} {?,?} {?,?} {?,?} {2,2} CSE 326 Autumn 2001 8
a e b c d h i j f g k l Logical View of Tree CSE 326 Autumn 2001 9
Basic Tree Data Structure:First child/next sibling data next_sibling first_child a b c d e CSE 326 Autumn 2001 10
a c d e h i j f g k l Actual Data Structure b CSE 326 Autumn 2001 11
Combined View of Tree a e b c d h i j f g k l CSE 326 Autumn 2001 12
Tree Traversals • Many algorithms involve walking through a tree, and performing some computation at each node • Walking through a tree is called a traversal • Common kinds of traversal • Pre-order: node, then children • Post-order: children, then node • Level-order: nodes at depth d, nodes at depth d+1, … • In-order: left, then node, then right (specific to binary trees) CSE 326 Autumn 2001 13
Pre-Order Traversal Perform computation at the node, then recursively perform computation on each child preorder(node * n) { node * c; if (n != NULL) { DO SOMETHING; c = n->first_child; while (c != NULL) { preorder(c); c = c->next_sibling; } } } CSE 326 Autumn 2001 14
a e b c d h i j f g k l Pre-Order Traversal Example CSE 326 Autumn 2001 15
Pre-Order Applications • Use when computation at node depends upon values calculated higher in the tree (closer to root) • Example: computing depth depth(node) = 1 + depth( parent of node ) • Example: printing out a directory structure CSE 326 Autumn 2001 16
Pre-Order Example:Computing Depth of All Nodes Depth(node * n, int d) { node * c; if (n != NULL) { n->depth = d; c = n->first_child; while (c != NULL) { Depth(c, d+1); c = c->next_sibling; } } } • Add a field depth to all nodes • Call Depth(root,0) to set depth field CSE 326 Autumn 2001 17
Post-Order Traversal postorder(node * n) { node * c; if (n != NULL) { c = n->first_child; while (c != NULL) { postorder(c); c = c->next_sibling; } DO SOMETHING; } Recursively perform computation on each child, then perform computation at node CSE 326 Autumn 2001 18
Post-Order Applications • Use when computation at node depends on values calculated lower in tree (closer to leaves) • Example: computing height height(node) = 1 + MAX( height(child1), … height(childk) ) • Example: size of tree rooted at node size(node) = 1 + size(child1) + … + size(childk) CSE 326 Autumn 2001 19
Post-Order Example:Computing Size of Tree int Size(node * n) { node * c; if (n == NULL) return 0; else { int m = 1; c = n->first_child; while (c != NULL) { m += Size(c); c = c->next_sibling; } } return m; } • Call Size(root) to compute number of nodes in tree CSE 326 Autumn 2001 20
Depth-First Search • Pre-Order and Post-Order traversals are examples of depth-first search: • Nodes are visited deeply on left-most branches before any nodes are visited on right-most branches • NOTE: visiting right deeply before left would still be depth-first - crucial idea is “go deep first” • In DFS the nodes “being worked on” are kept on a stack (where?) CSE 326 Autumn 2001 21
a e b c d h i j f g k l Level-Order (Breadth-First) Traversal • Consider task of traversing tree level-by-level fromtop to bottom (alphabetic order, in example below) • Which data structure can best keep track of nodes? CSE 326 Autumn 2001 22
Level-Order (Breadth-First) Algorithm Put root in a Queue Repeat until Queue is empty: • Dequeue a node • Process it • Add its children to queue CSE 326 Autumn 2001 23
Level-Order Example:Printing the Tree print(node * root) { node * n, c; queue Q; Q.enqueue(root); while (! Q.empty()) { n = Q.dequeue(); print n->data; c = n->first_child; while (c != NULL) { Q.enqueue(c); c = c->next_sibling; } } } • Call Print(root) to print tree contents CSE 326 Autumn 2001 24
a e b c d h i j f g k l Example:Level-Order Queue CSE 326 Autumn 2001 25
Applications of Breadth-First Search • Find shortest path from root to a given node N • if N is at depth k, BFS will never visit a node at depth>k • important for really deep trees • Generalizes to finding shortest paths in graphs • Spidering the world wide web • From a root URL, fetch pages that are farther and farther away CSE 326 Autumn 2001 26