661152846

You might also like

Download as pdf
Download as pdf
You are on page 1of 75
Chapter 5. TREES Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C 2nd Edition Computer Science Press, 2008 Fall 2009 Course, Sungkyunkwan University Hyunseung Choo ch lece.skku.ac.kr Peo tenes ) Trees Orie Wn hens Trees @ Introduction > Def) a tree is a finite set of one or more nodes such that 1) there is a special node (root) 2) remaining nodes are partitioned into n > 0 disjoint trees T,,T.,-* where each of these is a tree; we call each T; subtree of the root > acyclic graph : contain no cycle > hierarchical structure TE Ronen p ele a mael Trees Level (8) RODS (i) 4 TE Ronen p elie emeaed Trees m Terminology > degree of a node: * the number of subtrees of node > degree of a tree: the maximum * degree of the nodes in the tree » leaf(terminal) node: * anode with degree zero (K, L, F, G, M, I, J) > internal(non-terminal) node: * node with degree one or more (A, B, C, D, E, H) TE Renny elena Trees > parent: a node that has subtrees > child: a root of the subtrees > sibling: child nodes of the same parent > ancestor: all the nodes along the path from the root to the node » descendant: all the nodes that are in its subtrees > level: level of node’s parent + 1 (level of the root node is 1) > depth (or height) of a tree: the maximum level of any node in the tree PTE a Pemeraep erie Trees m Representation of trees > list representation * the information in the root node comes first * followed by a list of the subtrees of that node (eg) (A(B(E(K.L),F),C(G),D(H(M),1,J))) > representation of a tree in memory where n is the degree of the tree data link 1 link 2 ad link n TE enya ria ier Trees m Representation of trees > left-child right-sibling representation >» nodes of a fixed size * easier to work * two link/pointer fields per node data left child tight sibling > left-child right-sibling node structure TE Romer p erie a75 Trees Kos fo) pO Ove > left-child right-sibling representation of a tree TE Ronen p elie a med Trees m Representation of trees > representation as a degree two tree > simply rotate the left-child right-sibling tree clockwise by 45 degrees >» two children » left child and right child * degree 2 * binary tree PTE a Wore cad Trees > left-child right-child tree representation of a tree TE emery ee ei ae Led Trees 2 left child-right sibling tree binary tree tree —_leftchild-right sibling tree __binary tree > tree representations TE ero el racran Binary Trees Peo tenes Binary Trees @ Def) a binary tree is a finite set of nodes such that 1) empty or 2) consists of root node and two disjoint binary trees, called, left subtree and right subtree > two different binary trees TE ero ela ran Binary Trees m Properties of binary trees > difference between a binary tree and a tree * may have empty node » the order of subtrees are important * a binary tree is not a subset of a tree * maximum number of nodes in a BT 2-1 where k: depth of the tree * relationship between the number of leaf nodes(n,) and the number of nodes with degree 2(n,) Ng=Na+1 TE erry el rar sme Binary Trees @ Special types of binary trees » skewed binary tree > full binary tree (of depth k) * def) a binary tree of depth k(20) having 2* - 1 nodes. > complete binary tree * def) a binary tree with n nodes that correspond to the nodes numbered from 1 to n in the full binary tree of depth k TE ero elec raan Binary Trees ao Ns £ @) es Eoeodods > full binary tree of depth 4 with sequential node numbers TE errr el ra ees ered Binary Trees ) (2) (2) goe¢ w ® © skewed binary tree complete binary tree » skewed and complete binary trees TE Wert eric med Binary Trees @ Binary tree representation > array representation * sequential representations * determine the locations of the parent, left child, and right child of any node i in the binary tree 1) parent(i) is at i/2 ifi + 1 ifi=1, no parent 2) left_child(i) is at 2-1 if Zisn 3) right _child(i) is at 2141 if2i+1 array representation of binary trees TE Wey errr Binary Trees m Problems of array representation > inefficient storage utilization S(n) = 2-1 where k: depth of binary tree ideal for complete binary trees >» hard to insert/delete TE Binary Trees m Linked representation > each node has three fields left_child, data, and right_child typedef struct node *tree_ptr; typedef struct node { int data; tree_pir left_child, right_child; left_child | data | right_child / \ left_child right_child > node representation for binary trees TE ery el racran Binary Trees skewed binary tree complete binary tree >» linked representation for the binary trees TE root x , [A Nu { root A ,_ |B [nut] wi / B c y_|C | NULL L \ \ { uw fe [Nucl NULL F [NuutiNuLL|G [NUL 7 [pn / i fuuutH [rue hue Jui Nutt] E | Nuwt] ero etic Binary Trees > leaf node’s link fields contain null pointer NULL | data) NULL leaf node > add a fourth field, parent, to know the parent of random nodes parent parent left_child | data | right_child (cata) left_child right_child TE dere Binary Trees m@ Tree representation > each node (in a tree) has variable sized nodes > hard to represent it by using array > use linked list need k link fields per node where k: degree of tree > two types of link: non-null links and null links * number of non-null links: n-1 % number of null links: n-k - (n-1) TE Wey errr Binary Trees m@ Convert a tree into a binary tree 1) (parent,child, ,child,,...,child,) > (parent,leftmost-child,next-right-sibling) = left-child right-sibling representation 2) simply rotate the left-child right-sibling tree clockwise by 45 degrees * — right field of root node always have null link * — null links: approximately 50% * depth increased TE Wey errr Binary Trees (a) (8) vee | Ce) (c) © ©) @Q% © binary tree left child-right sibling tree TE Were ec aed HTH Binary Tree Traversal and Other Operations Binary Tree Traversals g visit each node in the tree exactly once > produce a linear order for the information in a tree > what order? * inorder: LVR (Left Visit Right) * preorder: VLR (Visit Left Right) * postorder: LRV (Left Right Visit) > NB. * correspondence between these traversals and producing the infix, postfix, and prefix forms of expressions TE Wey errr Binary Tree Traversals A/B*C*D-+E (infix form) 1 > binary tree with arithmetic expression TE ere el rar emcee Binary Tree Traversals m Inorder traversal void inorder(tree_ptr ptr) { it(ote { inorder(ptr->left_child); printf(“%d",ptr->data); inorder(ptr->right_child); >» inorder is invoked 19 times for the complete traversal: 19 nodes > output: A/B*C*D+E * corresponds to the infix form TE erry el racran Binary Tree Traversals callof valuein action | callof valuein action inorder root inorder root I + i c 2 * 12 NULL 3 * mn C printf 4 I 13 NULL 5 A 2 * print 6 NULL 4 D 5 A. printf 15 NULL 7 NULL 14 D_ printf 4 1 printf 16 NULL 8 B 1 + printf 9 NULL 7 E 8 B printf 18 NULL 10 NULL 7 E_ printf 3 * printf 19 NULL > trace of inorder TE ero ela ran Binary Tree Traversals m Preorder traversal void preorder(tree_ptr ptr) { if(ptr) { printf(%d", ptr->data); preorder(ptr->left_child): preorder(ptr->right_child); > output in the order: +**/ABCDE * correspond to the prefix form TE ery erase Binary Tree Traversals m Postorder traversal void postorder(tree_ptr ptr) { if (ptr) { postorder(ptr->left_child); postorder(ptr->right_child); printf("%d", ptr->data); > output in the order: AB/C*D*E+ * correspond to the postfix form TE erry el racraan Binary Tree Traversals @ Iterative inorder traversal > Recursion * Call itself directly or indirectly * simple, compact expression: good readability * don’t need to know implementation details %* much storage: multiple activations exist internally * slow execution speed * applications: factorial, fibonacci number, tree traversal, binary search, tower of Hanoi, quick sort, LISP structure TE ere elie ecice Binary Tree Traversals void iter_inorder(tree_ptr node) { int top = -1; tree_ptr stack[MAX_STACK_SIZE]; while (1) { while (node) { push(&top, node); node = node->left_child; t node = pop(&top); if (Inode) break; printf(“%d", node->data); node = node->right_child; } > iterative inorder traversal TE ere ela ice Binary Tree Traversals > every node of the tree is placed on and removed from the stack exactly once > time complexity: O(n) where n: the number of nodes in the tree > space complexity: stack size O(n) where n: worst case depth of the tree (case of skewed binary tree) TE ero ela ran Binary Tree Traversals m Level order traversal > traversal by using queue(FIFO) a (a (s) () @ OW O® 2H > output in the order: 123456789++*15 for previous example: +*E*D/CAB TE ere el rarer ice Binary Tree Traversals void level_order(tree_ptr ptr) { int front = rear = 0; tree_ptr queue[MAX_QUEUE_SIZE]; if (ptr) return; addq(front, &rear,pir); while (1) { ptr = deleteq(&front, rear); if (ptr) { printf(“%d", ptr->data); if (ptr->left_child) addq (front, &rear,ptr->left_child); if (ptr->right_child) addq(front,&rear,ptr->right_child); t else break; } TE ere el rare Additional Binary Tree Operations m@ Copying binary trees > modified version of postorder tree_ptr copy(tree_ptr original) { tree_ptr temp; if (original) { temp = (tree_ptr)malloc(sizeot(node)); if (IS_FULL(temp)) exit(1); temp->left_child = copy(original->left_child); temp->right_child = copy(original->right_child); temp->data = original->data; return temp; } return NULL; } TR ounce erro ela ce Additional Binary Tree Operations m Testing for equality of binary trees > modified version of preorder int equal(tree_ptr first,tree_ptr second) { return ((lfirst && Isecond) || (first && second && (first->data == second->data) && equal(first->left_child, second->left_child) && equal(first->right_child, second->right_child)); TE ery el eacran Hl Heaps Cn Lente Heaps m def) max(min) tree: a tree in which the key value in each node is no smaller(larger) than key values in its children (if any) m def) max(min) heap: a complete binary tree that is also a max(min) tree g the root of a max(min) tree contains the largest(smallest) key in the tree TE ery el racran Heaps m@ Representation of max(min) heaps > array representation because heap is a complete binary tree > simple addressing scheme for parent, left, and right children #define MAX_ELEMENTS 200 #define HEAP_FULL(n) (n == MAX_ELEMENTS - 1) #define HEAP_EMPTY(n) (!n) typedef struct { int key; f other field */ } element; element heap[MAX_ELEMENTS]; int n =0; TE ero el eacran Heaps > sample max heaps TE Heaps >» sample min heaps TE Priority queues @ deletion: deletes the element with the highest (or the lowest) priority @ insertion: insert an element with arbitrary priority into a priority queue at any time (ex) job scheduling of OS representation insertion deletion unordered array Od) O(n) unordered linked list | O(1) On) sorted array O(n) Ol) sorted linked list O(n) OW) max heap O(logan) O(logen) TE ero ela raan (a) heap before insertion _(b) initial location of new node (©) insert 5 into heap (a) (d) inset 21 into heap (a) TE erred el rar me ice Heaps > select the initial location for the node to be inserted — bottommost-rightmost leaf node > insert a new key value > adjust key value from leaf to root parent position: floor(i/2) > time complexity : O(depth of tree) => O(log,n) TE roy el racran Heaps Void insert_max_heap(element item, int *n) { int i; if (HEAP_FULL(*n)) { fprintf(stderr,"The heap is full. \n’); exit(1); } i= +4(*n); while ((il+1) && (item.key > heap[i/2].key)) { heapfi] = heap|i/2}; i/= 2; } heap|i] = item; > insertion into a max heap TE ere ela emcee Heaps @ Deletion from a max heap > always delete an element from the root of the heap > restructure the tree so that it corresponds to a complete binary tree > place the last node to the root and from the root compare the parent node with its children and exchanging out-of-order elements until the heap is reestablished TE roy el racran (b) 10 inserted at the root (c) final heap TE roy etre ran Heaps >» select the removed node bottommost-rightmost leaf node » place the node's element in the root node > adjust key value from root to leaf compare the parent node with its children and exchange out-of-order elements until the heap is reestablished > time complexity : O(depth of tree) => O(log,n) TE roy etre ran Heaps element delete_max_heaplint *n) { element item, temp; if (HEAP_EMPTY("n)) { {print(stderr,"The heap is emptyin’) exit(1); } item = heapl1]; tomp = heap|("n)--]; parent = 15 child = 2; while (chil nt 1° compare left and right child's key values */ it ((child = heap{child}. key) break; * move to the next lower level */ heapiparent] = heap{child]; parent = child; child * } heap[parent] = ter return item; } TE Protea. I Binary Search Trees Binary Search Tree(BST) m Binary search tree(BST) is a binary tree that is empty or each node satisfies the following properties: 1) every element has a key, and no two elements have the same key 2) the keys in a nonempty left subtree must be smaller than the key in the root of the subtree 3) the keys in a nonempty right subtree must be larger than the key in the root of the subtree 4) the left and right subtrees are also BST TE roy etre ran Binary Search Tree(BST) (20) (8) @) (12) (10) (22) (a) not a BST (20) () G) & (70) @ @ © (b) BST (c) BST TE erry el rae es merce Binary Search Tree(BST) m searching, insertion, deletion are all bounded by O(h) where h is the height of the BST > can perform these operations both » by key value and eg) delete the element with key x * by rank eg) delete the fifth smallest element > inorder traversal of BST * generate a sorted list TE roy etre ran Binary Search Tree(BST) m Searching a BST tree_ptr search(tree_ptr root, int key) { p return a pointer to the node that contains key. If there is no such node, return NULL a if (!root) return NULL; if (key == root->data) return root; if (key < root->data) return search(root->left_child, key); return search(root->right_child, key); } > recursive search of a BST TE ero el racran Binary Search Tree(BST) tree_pir iter_search(tree_ptr tree, int key) { while (tree) { if (key == tree->data) return tree; if (key < tree->data) tree = tree->left_child; else tree = tree->right_child; } return NULL; } iterative search of a BST > time complexity for searching * in average: O(h) where h: the height of BST * in worst case: O(n) for skewed binary tree TE ere el raceme Binary Search Tree(BST) g Inserting into a BST void insert_node(tree_ptr *node, int num) { tree_ptr ptr,temp = modified_search(*node, num); if (temp || I(*node)) { ptr = (tree_ptr)malloc(sizeof(node)); if (IS_FULL(ptr)) { fprintf(stderr,”The momory is full\n"); exit(1); ptr->data = num; ptr->left_child = ptr->right_child = NULL; if (node) if (num < temp->data) temp->left_child = ptr; else temp->right_child = ptr; else *node=ptr; PTR oy ela ran Binary Search Tree(BST) ™ modified_search is slightly modified version of function iter_search > return NULL, if the tree is empty or num is present > otherwise, return a pointer to the last node of the tree that was encountered during the search > time complexity for inserting * O(h) where h is the height of the tree TE eo el racran Binary Search Tree(BST) (20) ) & fo) @) {%) fe) GO @®™@@® © ®@ (a) insert 80 (b) insert 35 > inserting into a BST Wetec a eicd Binary Search Tree(BST) @ Deleting from a BST » deletion of a leaf node » deletion of a node with 1 child » deletion of a node with 2 children - time complexity for deleting: O(h) where h is the height of the tree @ Height of a BST the height of a BST with n elements > average case: O(log,n) > worst case: O(n) eg) use insert_node to insert the keys 1, 2, 3, ..., n into an initially empty BST TE rere el rac ice Binary Search Tree(BST) > Deleting a leaf or a node with 1 child (a) delete 35 (leaf) (b) delete 40 (node with single child) TE rey el rar mice Binary Search Tree(BST) (a) tree before deletion of 60 (b) tree after deletion of 60 >» deletion of a node with two children TE erry el rac sm ice Binary Search Tree(BST) m Balanced search trees > worst case height: O(log,n) >» searching, insertion, deletion is bounded by O(h) where h is the height of a binary tree > AVL tree, 2-3 tree, red-black tree TE ero el racran HI Forests Peo tenes Forest m Def) A forest is a set of n = 0 disjoint trees @ removing the root of any tree > produce a forest > forest with three trees TE eo el racran Forest m@ Transforming a forest into a binary tree 1) obtain the binary tree representation for each of the trees in the forest 2) link all the binary trees together through sibling field of the root node TE Wey errr Forest @ Def) If T,,---,T, is a forest of trees, then the binary tree corresponding to this forest, denoted by B(T,,--:,T,): 1) is empty, ifn =0 2) has root equal to root (T;); has left subtree equal to B(T,,,Ty2,"°*, Ty), where T115T12.°7, Tym are the subtrees of root(T,); and has right subtree B(T,, ---. T,) TE ery el racran Forest > binary tree representation of forest TE ery el racraan Forest m Forest traversals > preorder traversal 1) if F is empty, then return 2) visit the root of the first tree of F 3) traverse the subtrees of the first tree in tree preorder 4) traverse the remaining trees of F in preorder — equivalent to the preorder traversal of the corresponding binary tree TE ero erred Forest m Forest traversals > inorder traversal 1) if F is empty, then return 2) traverse the subtrees of the first tree in tree inorder 3) visit the root of the first tree of F 4) traverse the remaining trees of F in inorder — equivalent to the inorder traversal of the corresponding binary tree TE ery el racran Forest m Forest traversals > postorder traversal 1) if F is empty, then return 2) traverse the subtrees of the first tree in tree postorder 3) traverse the remaining trees of F in postorder 4) visit the root of the first tree of F — not equivalent to the postorder traversal of the corresponding binary tree TE ery ela ran

You might also like