1 / 75

Chapter 5. TREES

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 choo@ece.skku.ac.kr. Trees. Trees. Introduction

tyrell
Télécharger la présentation

Chapter 5. TREES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 choo@ece.skku.ac.kr

  2. Trees

  3. 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 T1,T2,···,Tn where each of these is a tree; we call each Ti subtree of the root • acyclic graph : contain no cycle • hierarchical structure

  4. A B C D E F G H I J K L M Trees Level 1 2 3 4

  5. Trees • 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: • a node 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)

  6. 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

  7. Trees • 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),I,J))) • representation of a tree in memory where n is the degree of the tree data link 1 link 2 · · · link n

  8. data left child right sibling Trees • Representation of trees • left-child right-sibling representation • nodes of a fixed size • easier to work • two link/pointer fields per node • left-child right-sibling node structure

  9. A B C D E F G H I J K L M Trees • left-child right-sibling representation of a tree

  10. Trees • 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

  11. A B E C K F G D L H M I J Trees • left-child right-child tree representation of a tree

  12. A A A B B B A A A B B C B C C Trees • tree representations tree left child-right sibling tree binary tree tree left child-right sibling tree binary tree

  13. Binary Trees

  14. A A B B 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

  15. Binary Trees • 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 2k-1 where k: depth of the tree • relationship between the number of leaf nodes(n0) and the number of nodes with degree 2(n2) n0 = n2 + 1

  16. Binary Trees • Special types of binary trees • skewed binary tree • full binary tree (of depth k) • def) a binary tree of depth k(³0) having 2k - 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

  17. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Binary Trees • full binary tree of depth 4 with sequential node numbers

  18. A A B B C C D E F G D H I E Binary Trees • skewed and complete binary trees skewed binary tree complete binary tree

  19. 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 if i ¹ 1 if i = 1, no parent 2) left_child(i) is at 2·i if 2in 3) right_child(i) is at 2·i + 1 if 2·i + 1  n

  20. [1] A [2] B [3] - [1] A [4] C [2] B [5] - [3] C [6] - [4] D [7] - [5] E [8] D [6] F [9] - [7] G · · [8] H · · [9] I · · [16] E Binary Trees • array representation of binary trees skew binary tree complete binary tree

  21. Binary Trees • Problems of array representation • inefficient storage utilization S(n) = 2k-1 where k: depth of binary tree ideal for complete binary trees • hard to insert/delete

  22. data left_child data right_child left_child right_child Binary Trees • Linked representation • each node has three fields left_child, data, and right_child typedef struct node *tree_ptr; typedef struct node { int data; tree_ptr left_child, right_child; }; • node representation for binary trees

  23. root A NULL root A B NULL B C C NULL D E F G NULL NULL NULL NULL NULL NULL D NULL H I NULL NULL NULL NULL E NULL NULL Binary Trees • linked representation for the binary trees skewed binary tree complete binary tree

  24. data NULL NULL parent parent left_child data right_child data left_child right_child Binary Trees • leaf node’s link fields contain null pointer • add a fourth field, parent, to know the parent of random nodes leaf node

  25. Binary Trees • 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)

  26. Binary Trees • Convert a tree into a binary tree 1) (parent,child1,child2,...,childk)  (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

  27. A B C D A B E F G E C A F D G B C D E F G Binary Trees tree binary tree left child-right sibling tree

  28. Binary Tree Traversal and Other Operations

  29. Binary Tree Traversals • 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) • N.B. • correspondence between these traversals and producing the infix, postfix, and prefix forms of expressions

  30. 1 + 2 17 * E 18 19 3 14 * D 15 16 4 11 / C 12 13 5 8 A B 6 7 9 10 Binary Tree Traversals • binary tree with arithmetic expression A / B * C * D + E (infix form)

  31. Binary Tree Traversals • Inorder traversal void inorder(tree_ptr ptr) { if(ptr) { 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

  32. Binary Tree Traversals • trace of inorder

  33. Binary Tree Traversals • 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

  34. Binary Tree Traversals • 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

  35. 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

  36. 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; } node = pop(&top); if (!node) break; printf(“%d”, node->data); node = node->right_child; } } • iterative inorder traversal

  37. 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)

  38. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Binary Tree Traversals • Level order traversal • traversal by using queue(FIFO) • output in the order: 123456789•••15 for previous example: +*E*D/CAB

  39. 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,ptr); 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); } else break; } }

  40. Additional Binary Tree Operations • Copying binary trees • modified version of postorder tree_ptr copy(tree_ptr original) { tree_ptr temp; if (original) { temp = (tree_ptr)malloc(sizeof(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; }

  41. Additional Binary Tree Operations • Testing for equality of binary trees • modified version of preorder int equal(tree_ptr first,tree_ptr second) { return ((!first && !second) || (first && second && (first->data == second->data) && equal(first->left_child, second->left_child) && equal(first->right_child, second->right_child)); }

  42. Heaps

  43. Heaps • 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) • def) max(min) heap: a complete binary tree that is also a max(min) tree • the root of a max(min) tree contains the largest(smallest) key in the tree

  44. Heaps • 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; /* other field */ } element; element heap[MAX_ELEMENTS]; int n = 0;

  45. [1] 14 [2] [3] 12 7 [4] [5] [6] 10 8 6 [1] [1] 30 9 [2] [2] [3] 25 6 3 [4] 5 Heaps • sample max heaps

  46. [1] 2 [2] [3] 7 4 [4] [5] [6] 10 8 6 [1] [1] 11 10 [2] [2] [3] 21 20 83 [4] 50 Heaps • sample min heaps

  47. 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

  48. [1] [1] 20 20 [2] [3] [2] [3] 15 2 15 2 [4] [5] [4] [5] [6] 14 10 14 10 [1] [1] 20 21 [2] [3] [2] [3] 15 5 15 20 [4] [5] [6] [4] [5] [6] 14 10 2 14 10 6 Heaps (a) heap before insertion (b) initial location of new node (c) insert 5 into heap (a) (d) inset 21 into heap (a)

  49. 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(log2n)

  50. 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 = ++(*n); while ((i!=1) && (item.key > heap[i/2].key)) { heap[i] = heap[i/2]; i /= 2; } heap[i] = item; } • insertion into a max heap

More Related