1 / 110

Chapter 5 Trees

Chapter 5 Trees. Instructors: C. Y. Tang and J. S. Roger Jang. All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement from the slides of Prof. Hsin-Hsi Chen (NTU). Outline (1). Introduction (5.1) Binary Trees (5.2)

Leo
Download Presentation

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 Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement from the slides of Prof. Hsin-Hsi Chen (NTU).

  2. Outline (1) • Introduction (5.1) • Binary Trees (5.2) • Binary Tree Traversals (5.3) • Additional Binary Tree Operations (5.4) • Threaded Binary Trees (5.5) • Heaps (5.6) & (Chapter 9) • Binary Search Trees (5.7)

  3. Outline (2) • Selection Trees (5.8) • Forests (5.9) • Set Representation (5.10) • Counting Binary Trees (5.11) • References & Exercises

  4. 5.1 Introduction • What is a “Tree”? • For Example : • Figure 5.1 (a) • An ancestor binary tree • Figure 5.1 (b) • The ancestry of modern Europe languages

  5. The Definition of Tree (1) • A tree is a finite set of one or more nodes such that : • (1) There is a specially designated node called the root. • (2) The remaining nodes are partitioned into n ≥ 0 disjoint sets T1, …, Tn, where each of these sets is a tree.We call T1, …, Tn, the sub-trees of the root. root … T1 T2 Tn

  6. The Definition of Tree (2) • The root of this tree is node A. (Fig. 5.2) • Definitions: • Parent (A) • Children (E, F) • Siblings (C, D) • Root (A) • Leaf / Leaves • K, L, F, G, M, I, J…

  7. The Definition of Tree (3) • The degree of a node is the number of sub-trees of the node. • The level of a node: • Initially letting the root be at level one • For all other nodes, the level is the level of the node’s parent plus one. • The height or depth of a tree is the maximum level of any node in the tree.

  8. Representation of Trees (1) • List Representation • The root comes first, followed by a list of sub-trees • Example: (A(B(E(K,L),F),C(G),D(H(M),I, J))) data link 1 link 2 ... link n A node must have a varying number of link fields depending on the number of branches

  9. data right sibling left child Representation of Trees (2) • Left Child-Right Sibling Representation • Fig.5.5 • A Degree Two Tree • Rotate clockwise by 45° • A Binary Tree

  10. 5.2 Binary Trees • A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left sub-tree and the right sub-tree. • Any tree can be transformed into a binary tree. • By using left child-right sibling representation • The left and right subtrees are distinguished root The leftsub-tree The rightsub-tree

  11. Abstract Data Type Binary_Tree (structure 5.1) Structure Binary_Tree (abbreviated BinTree) is: Objects: a finite set of nodes either empty or consisting of a root node, left Binary_Tree, and right Binary_Tree. Functions: For all bt, bt1, bt2 BinTree, item  element Bintree Create()::= creates an empty binary tree Boolean IsEmpty(bt)::= if (bt==empty binary tree) return TRUE else return FALSE

  12. BinTree MakeBT(bt1, item, bt2)::= return a binary tree whose left subtree is bt1, whose right subtree is bt2, and whose root node contains the data item Bintree Lchild(bt)::= if (IsEmpty(bt)) return error else return the left subtree of bt element Data(bt)::= if (IsEmpty(bt)) return error else return the data in the root node of bt Bintree Rchild(bt)::= if (IsEmpty(bt)) return error else return the right subtree of bt

  13. Special Binary Trees • Skewed Binary Trees • Fig.5.9 (a) • Complete Binary Trees • Fig.5.9 (b) • This will be defined shortly

  14. Properties of Binary Trees (1) • Lemma 5.1 [Maximum number of nodes] : • (1) The maximum number of nodes on level i of a binary tree is 2i -1, i≥1. • (2) The maximum number of nodes in a binary tree of depth k is is 2k-1, k≥ 1. • The proof is by induction on i. • Lemma 5.2 : • For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 the number of nodes of degree 2, then n0 = n2 +1.

  15. Properties of Binary Trees (2) • A full binary tree of depth k is a binary tree of depth k having 2k-1 nodes, k≧ 0. • A binary tree with n nodes and depth k is completeiff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k.

  16. Binary Tree Representation • Array Representation (Fig. 5.11) • Linked Representation (Fig. 5.13)

  17. Array Representation • Lemma 5.3 : If a complete binary tree with n nodes (depth = └log2n + 1┘) is represented sequentially, then for any node with index i, 1 ≦i≦n, we have: • (1) parent (i) is at └ i / 2 ┘, i≠ 1. • (2) left-child (i) is 2i, if 2i ≤n. • (3) right-child (i) is 2i+1, if 2i+1 ≤n. • For complete binary trees, this representation is ideal since it wastes no space. However, for the skewed tree, less than half of the array is utilized.

  18. Linked Representation typedef struct node *tree_pointer; typedef struct node { int data; tree_pointer left_child, right_child; };

  19. 5.3 Binary Tree Traversals • Traversing order : L, V, R • L : moving left • V : visiting the node • R : moving right • Inorder Traversal : LVR • Preorder Traversal : VLR • Postorder Traversal : LRV

  20. For Example • Inorder Traversal : A / B * C * D + E • Preorder Traversal : + * * / A B C D E • Postorder Traversal : A B / C * D * E +

  21. Inorder Traversal (1) • A recursive function starting from the root • Move left Visit node Move right

  22. Inorder Traversal (2) In-order Traversal :A / B * C * D + E

  23. Preorder Traversal • A recursive function starting from the root • Visit node Move left  Move right

  24. Postorder Traversal • A recursive function starting from the root • Move left Move right Visit node

  25. Other Traversals • Iterative Inorder Traversal • Using a stack to simulate recursion • Time Complexity: O(n), n is #num of node. • Level Order Traversal • Visiting at each new level from the left-most node to the right-most • Using Data Structure : Queue

  26. Iterative In-order Traversal (1)

  27. Iterative In-order Traversal (2) Delete “C” & Print Delete “*” & Print Add “D” Delete “D” & Print Delete “+” & Print Add “E” Delete “E” & Print Add “+” in stack Add “*” Add “*” Add “/” Add “A” Delete “A” & Print Delete “/” & Print Add “B” Delete “B” & Print Delete “*” & Print Add “C” In-order Traversal :A / B * C * D + E

  28. Level Order Traversal (1)

  29. Level Order Traversal (2) Add “+” in Queue Deleteq “+” Addq “*” Addq “E” Deleteq “*” Addq “*” Addq “D” Deleteq “E” Deleteq “*” Addq “/” Addq “C” Deleteq “D” Deleteq “/” Addq “A” Addq “B” Deleteq “C” Deleteq “A” Deleteq “B” Level-order Traversal :+ * E * D / C A B

  30. 5.4 Additional Binary Tree Operations • Copying Binary Trees • Program 5.6 • Testing for Equality of Binary Trees • Program 5.7 • The Satisfiability Problem (SAT)

  31. Copying Binary Trees • Modified from postorder traversal program

  32. Testing for Equality of Binary Trees • Equality: 2 binary trees having identical topology and data are said to be equivalent.

  33. SAT Problem (1) • Formulas • Variables : X1, X2, …, Xn • Two possible values: True or False • Operators : And (︿), Or (﹀), Not (﹁) • A variable is an expression. • If x and y are expressions,then ﹁ x, x ︿ y, x ﹀y are expressions. • Parentheses can be used to alter the normal order of evaluation,which is ﹁ before ︿ before ﹀.

  34. SAT Problem (2)

  35. SAT Problem (3) • The SAT problem • Is there an assignment of values to the variables that causes the value of the expression to be true? • For n variables, there are 2n possible combinations of true and false. • The algorithm takes O(g 2n) time • g is the time required to substitute the true and false values for variables and to evaluate the expression.

  36. SAT Problem (4) • Node Data Structure for SAT in C

  37. SAT Problem (5) • A Enumerated Algorithm • Time Complexity : O (2n)

  38. SAT Problem (6) void post_order_eval(tree_pointer node){ if (node){ post_order_eval(node->left_child); post_order_eval(node->right_child); switch(node->data){ case not: node->value=!node->right_child->value; break; case and: node->value=node->right_child->value && node->left_child->value; break; case or: node->value=node->right_child->value || node->left_child->value; break; case true: node->value=TRUE; break; case false: node->value=FALSE; break; } } }

  39. 5.5 Threaded Binary Trees (1) • Linked Representation of Binary Tree • more null links than actual pointers (waste!) • Threaded Binary Tree • Make use of these null links • Threads • Replace the null links by pointers (called threads) • If ptr -> left_thread = TRUE • Then ptr -> left_child is a thread (to the node before ptr) • Else ptr -> left_child is a pointer to left child • If ptr -> right_thread = TRUE • Then ptr -> right_child is a thread (to the node after ptr) • Else ptr -> right_child is a pointer to right child

  40. 5.5 Threaded Binary Trees (2) typedef struct threaded_tree *threaded_pointer; typedef struct threaded_tree { short int left_thread; threaded_pointer left_child; char data; short int right_child; threaded_pointer right_child; }

  41. 5.5 Threaded Binary Trees (3) Head node of the tree Actual tree

  42. Inorder Traversal of a Threaded Binary Tree (1) • Threads simplify inorder traversal algorithm • An easy O(n) algorithm (Program 5.11.) • For any node, ptr, in a threaded binary tree • If ptr -> right_thread = TRUE • The inorder successor of ptr = ptr -> right_child • Else (Otherwise, ptr -> right_thread = FALSE) • Follow a path of left_child links from the right_child of ptr until finding a node with left_Thread = TRUE • Function insucc (Program 5.10.) • Finds the inorder successor of any node (without using a stack)

  43. Inorder Traversal of a Threaded Binary Tree (2)

  44. Inorder Traversal of a Threaded Binary Tree (2)

  45. Inserting a Node into a Threaded Binary Tree • Insert a new node as a child of a parent node • Insert as a left child (left as an exercise) • Insert as a right child (see examples 1 and 2) • Is the original child node an empty subtree? • Empty child node (parent -> child_thread = TRUE) • See example 1 • Non-empty child node (parent -> child_thread = FALSE) • See example 2

  46. Inserting a node as the right child of the parent node (empty case) • parent(B) -> right_thread = FALSE • child(D) -> left_thread & right_thread = TURE • child -> left_child = parent • child -> right_child = parent -> right_child • parent -> right_child = child (1) (3) (2)

  47. Inserting a node as the right child of the parent node (non-empty case) (3) (2) (1) (4)

  48. Right insertion in a threaded binary tree void insert_right(threaded_pointer parent, threaded_pointer child){ threaded_pointer temp; child->right_child = parent->right_child; child->right_thread = parent->right_thread; child->left_child = parent; child->left_thread = TRUE; parent->right_child = child; parent->right_thread = FALSE; If (!child->right_thread){/*non-empty child*/ temp = insucc(child); temp->left_child = child; } } (1) (2) (3) (4)

  49. 5.6 Heaps • An application of complete binary tree • Definition • A max (or min) tree • a tree in which the key value in each node is no smaller (or greater) than the key values in its children (if any). • A max (or min) heap • a max (or min) complete binary tree A max heap

  50. Heap Operations • Creation of an empty heap • PS. To build a Heap  O( n log n ) • Insertion of a new element into the heap • O (log2n) • Deletion of the largest element from the (max) heap • O (log2n) • Application of Heap • Priority Queues

More Related