Presentation is loading. Please wait.

Presentation is loading. Please wait.

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-1 Chapter 5 Trees Introduction to Data Structures CHAPTER 5 Trees 5.1 Introduction 5.2 Binary Trees 5.3.

Similar presentations


Presentation on theme: "Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-1 Chapter 5 Trees Introduction to Data Structures CHAPTER 5 Trees 5.1 Introduction 5.2 Binary Trees 5.3."— Presentation transcript:

1 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-1 Chapter 5 Trees Introduction to Data Structures CHAPTER 5 Trees 5.1 Introduction 5.2 Binary Trees 5.3 Binary Tree Traversal 5.4 Additional Binary Tree Operations 5.5 Threaded Binary Trees 5.6 Heaps 5.7 Binary Search Trees 5.8 Selection Trees 5.9 Forests 5.10 Set Representations 5.11 Counting Binary Trees

2 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-2 Chapter 5 Trees Chapter 1 Basic Concepts Chapter 2 Arrays Chapter 3 Stacks and Queues Chapter 4 Linked Lists Chapter 5 Trees Chapter 6 Graph Chapter 7 Sorting Chapter 8 Hashing Chapter 9 Heap Structures Chapter 10 Search Structures Contents

3 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-3 Chapter 5 Trees 5.1 Introduction Def: Tree A tree T is a finite set of one or more nodes such that (1) root, root(T) (2) the remaining nodes are partitioned into n ≧ 0 disjoint sets T 1,…, T n, and T i is a tree Note 1. Note 2. Recursive definition Note 3. Genealogical trees: Figure 5.1, p.187 T 1 , T 2 , T 3 are called the subtree of the root T root T2T2 T3T3 T1T1

4 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-4 Chapter 5 Trees Trees Dusty Honey BearBrandy BrunhildeTerry GillTanseyTweedZoe CoyoteNugget CrocusPrimroseNousBelle Introduction: Genealogical trees leaf root

5 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-5 Chapter 5 Trees Level 1 2 3 4 node (13) leaf (terminal) nonterminal parent children sibling ancestor level of a node height of a tree (4) A B E KL F CD H MG IJ Introduction: Terminologies

6 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-6 Chapter 5 Trees Introduction: Degree, Level, Depth Degree of a node: # of subtrees of a node Level of a node: root  level 1 (for some textbook , root  level 0) if a node at level L, then its children  L+1. Depth (height) of a tree: max level of any node in the tree. 3 2 degree level 1 222 3 3

7 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-7 Chapter 5 Trees Introduction: Tree Representation  List Representation: ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) The root comes first, followed by a list of sub-trees degree of a node A B C D EF KL G HIJ M

8 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-8 Chapter 5 Trees Introduction: Tree Representation (cont.) List Representation: linked node Figure 5.3 How many link fields are needed in such a representation? Lemma 5.1: If T is a k-ary tree (i.e., a tree of degree k) with n nodes, then n(k – 1) + 1 of the nk child fields are 0, n ≧ 1. Proof: The total number of child fields is nk Non-0 child fields is n –1. Hence, the number of 0 fields is nk – (n – 1) = n(k – 1) + 1 Child n ... Child 2Child 1data

9 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-9 Chapter 5 Trees Another way to represent tree structure (a) Nested sets (b) Nested parentheses (A(B(H)(J))(C(D)E(G))(F)) HJDG F E A BC HJDEF G BC A Introduction: Tree Representation (cont.)

10 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-10 Chapter 5 Trees (c) Indentation (d) Dewey decimal notation A|---------------------------| B|---------------------| H|-----------| J|-----------| C|---------------------| D|-----------| E|-----------| G|-----| F|-----------| Ch2 |---------------------------| 2.1|---------------------| 2.2|---------------------| 2.2.1|-----------| 2.2.2|-----------| 2.3|---------------------| 2.3.1|-----------| 2.3.1.1|-----| 2.3.2|-----------| 2.4|---------------------| 2.5|---------------------| A B C HJDEF G 1 1.1 1.1.11.1.21.2.2 1.2.3 1.2.2.1 1.2.1 1.2 Introduction: Tree Representation (cont.)

11 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-11 Chapter 5 Trees A B C D E F G H IJ K L M data left childright sibling Introduction: Tree Representation as a Left Child-Right Sibling A B C D EF KL G HIJ M Fig. 5.5

12 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-12 Chapter 5 Trees A B C D E F G H I J K L M data left childright child Introduction: Tree Representation as a Left child-Right child (Binary Tree) A B C D E F G H IJ K L M ??? Left child-Right child i.e. Degree-Two Tree i.e. Binary Tree We can represent any tree as a binary tree. Fig. 5.6, p.191 Example 2: Fig. 5.8, p.253

13 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-13 Chapter 5 Trees 5.2 Binary Trees Def: Binary tree is a finite set of nodes, which is (i) empty or (ii) consists of a root and two disjoint binary trees (called the left subtree and right subtree) Cf. ∴ Binary tree isn’t a special case of a tree; it is another concept. TreesBinary Trees min. node #10 degree0, 1, 2, 3, … 0, 1, 2 (right, left subtree) the samedifferent B A A B

14 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-14 Chapter 5 Trees 5.2.1 The Abstract Data Type ADT 5.1, p. 193 structure Binary_Tree (abbreviated BinTree) is objects: a finite set of nodes empty or consisting of a root, left Binary_Tree, and right Binary_Tree. functions: for all bt, bt1, bt2  BinTree, item  element BinTree Create() ::=create an empty binary tree Boolean IsEmpty(bt) ::= if(bt == empty binary tree) return TRUE else return FALSE BinTree MakeBT(bt1, item, bt2) ::= return a binary tree with bt1 as left subtree, bt2 as right subtree, and a root containing item BinTree Rchild(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 root of bt. BinTree Rchild(bt) ::= if(IsEmpty(bt)) return error else return the right subtree of bt.

15 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-15 Chapter 5 Trees A B C D E Skewed Binary Tree Special Binary Trees A B C G E I D H F Complete Binary Tree Level Max # 1 1 2 0 2 2 2 1 3 4 2 3-1 4 8 2 4-1 i 2 i -1

16 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-16 Chapter 5 Trees Strictly Binary Tree Special Binary Trees (cont.) A B C E I D H Every nonleaf node has nonempty left and right subtrees

17 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-17 Chapter 5 Trees 5.2.2 Properties of Binary Trees Lemma 5.1 (Maximum number of nodes, p.194) (1) The maximum number of nodes on level i of a binary tree is 2 i-1, i  1. (2) The maximum number of nodes in a binary tree of depth k is 2 k  1, k  1. Pf: (1) The proof is by induction on i 1 0 Induction Base: The root is the only node on level i = 1. Hence the maximum # of nodes on level i = 1 is 2 0 = 1. 2 0 Induction Hypothesis: For all j, 1  j < i, the max # of nodes on level j is 2 j-1 3 0 Induction step: The max # of nodes on level i-1 is 2 i-2, by the induction hypothesis. Since each node in a binary tree has maximum degree 2, the max # of nodes on level i is 2 times the max # of level i-1 or 2 i-1.

18 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-18 Chapter 5 Trees Lemma 5.1 (cont.) Pf: (2) The max # of nodes in a binary tree of depth k is  Note: k=1 k=2 k=3 2021222021222

19 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-19 Chapter 5 Trees Lemma 5.2 ( Relationship between the # of leaf nodes and the # of nodes of degree 2 in a binary tree) For any nonempty binary tree, T n 0 = the # of leaf (terminal) nodes, n 2 = the # of nodes of degree 2 then n 0 = n 2 + 1 e.g. Skewed tree n 0 =1 n 2 =0  n 0 =n 2 +1 Complete binary tree n 0 =5 n 2 =4  n 0 =n 2 +1 Lemma 5.2

20 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-20 Chapter 5 Trees Pf : 1 0 Let n 1 = the #of nodes of degree 1 n = the total # of nodes Since all nodes in T are of degree  2, we have: n = n 0 + n 1 + n 2 ---------- (1) 2 0 Let B = the # of branches, then n = B + 1 ( ∵ except for tree root, each node has a branch leading into it) 3 0 ∵ All branches emanate either from a node of degree one or from a node of degree 2 ∴ B = n 1 + 2 n 2 => n = B + 1 = n 1 + 2n 2 + 1 ---------- (2) (2)  (1)  0 = n 2 + 1  n 0  n 0 = n 2 + 1 Lemma 5.2 (cont.)

21 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-21 Chapter 5 Trees  A full binary tree of depth k is a binary tree of depth k having 2 k -1 nodes, k  0.  A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. A B C G E I D H F A B C G E K D J F I H O N M L Full binary tree of depth 4Complete binary tree Full Binary Tree vs. Complete Binary Tree

22 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-22 Chapter 5 Trees A B C D -------D---C-BA 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0D00C40B30A2 1 2 3 4 5  Method 1. Array Representation:  Method 2. Linked Representation: 0A 0B 0C 0D0 5.2.3 Binary Tree Representations

23 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-23 Chapter 5 Trees [1] [2] [3] [4] [5] [6] [7] [8] [9] ABCDEFGHIABCDEFGHI A B C G E I D H F 1 2 3 4567 8 9 Array Representation (in computer)

24 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-24 Chapter 5 Trees A B -- C -- D --. E [1] [2] [3] [4] [5] [6] [7] [8] [9]. [16] A B E C D Disadvantages: (1) waste space (2) insertion/deletion problem Array Representation: Example

25 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-25 Chapter 5 Trees Lemma 5.3 if a complete binary tree with n nodes is represented sequentially, then for any node with index i, 1  i  n, we have: (1) parent (i) is at  i/2  if i≠1. If i = 1, i is at the root and has no parent. (2) left_child (i) is at 2i if 2i  n. If 2i > n, then i has no left child. (3) right_child (i) is at 2i + 1 if 2i+1  n. If 2i + 1 > n, then i has no right child. Array Representation :Lemma 5.3 A B C G E I D H F 1 2 3 4567 8 9

26 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-26 Chapter 5 Trees E.g. Parent of node 7: parent (7) is at Parent of node 6: parent (6) is at left_child of 5 is at 2i =10, right_child of 5 is at 2i +1 =11 (2 x 8 = 16 > n has no Child) Pf : We prove (2) (3) is an immediate consequence of (2). ( ∵ the numbering of nodes on same level from left to right) (1) follows from (2) and (3). 123456789101112131415 Array Representation :Lemma 5.3 (cont.)

27 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-27 Chapter 5 Trees We prove (2) by induction on i. For i = 1, clearly the left child is at 2 unless 2 > n in which case 1 has no left child. Now assume that for all j, 1  j  i, left_child (j) is at 2j. Then, the two nodes immediately preceding left_child (i+1) in the representation are the right child of i and the left child of i. The left child of i is at 2i. Hence, the left child of i + 1 is at 2i + 2 = 2(i + 1) unless 2(i + 1) > n in which case i + 1 has no left child. ii+1 2i+2 2i2i Array Representation :Lemma 5.3 (cont.) (2) left_child (i) is at 2i if 2i  n. If 2i > n, then i has no left child.

28 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-28 Chapter 5 Trees  Node structure: typedef struct node *tree_pointer; typedef struct node { int data; tree_pointer left_child, right_child; }; Linked Representation left_child data right_child

29 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-29 Chapter 5 Trees  Let L, V, and R stand for moving left, visiting the node, and moving right.  There are six possible combinations of traversal: LVR, LRV, VLR, VRL, RVL, RLV  Adopt convention that we traverse left before right, only 3 traversals remain LVR: inorder, a+b LRV: postorder, ab+ VLR: preorder, +ab  Binary Tree Traversals: Combinations 5.3 Binary Tree Traversals data left_child right_child V L R + a b

30 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-30 Chapter 5 Trees Binary Tree Traversal: Example Traversing (walking through) a tree Inorder traversal BAC a+b infix Preorder traversal ABC +ab prefix Postorder traversal BCA ab+ postfix A B C A B C G F Inorder traversal BA____ Preorder traversal ABC____ Postorder traversal _________ E.g. 2 E.g. 1 + a b

31 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-31 Chapter 5 Trees Binary Tree Traversal: Exercise inorder traversal DIBA____ preorder traversal ABDI____ postorder traversal _________ A B C G I D F E.g. 3

32 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-32 Chapter 5 Trees Inorder traversal (infix expression) A / B * C * D + E Preorder traversal (prefix expression) + * * / A B C D E Postorder traversal (postfix expression) A B / C * D * E + Level order traversal + * E * D / C A B  Arithmetic Expression: A / B * C * D + E Arithmetic Expression using Binary Tree + * A * / E D C B

33 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-33 Chapter 5 Trees Inorder traversal Traverse the left subtree in inorder visit the root Traverse the right subtree in inorder Program 5.1: Inorder traversal Binary Tree Traversal: Inorder + * A * / E D C B A / B * C * D + E P.201

34 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-34 Chapter 5 Trees Binary Tree Traversal: Preorder Preorder traversal visit the root Traverse the left subtree in preorder Traverse the right subtree in preorder Program 5.2: Preorder traversal + * A * / E D C B + * * / A B C D E P.202

35 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-35 Chapter 5 Trees Postorder traversal Traverse the left subtree Traverse the right subtree Visit the root Program 5.3: Postorder traversal Binary Tree Traversal: Postorder + * A * / E D C B A B / C * D * E + P.203

36 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-36 Chapter 5 Trees Iterative Inorder Traversal Level-Order Traversal Traversal without a Stack Binary Tree Traversal: More Topics (skip) + * A * / E D C B Method 1: adding parent field Method 2: §5.5 Threaded binary tree

37 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-37 Chapter 5 Trees 5.4 Additional Binary Tree Operations With the inorder, postorder, or preorder mechanisms, we can implement all needed binary tree functions Copying Binary Trees Program 5.6: Copying a binary tree Testing Equality Program 5.7: Binary tree equivalence p.207

38 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-38 Chapter 5 Trees  The Satisfiability Problem  Prepositional Calculus Expression (Formula)  A variable is an expression.  If x and y are expressions, then ¬x, x  y, x  y are expressions.  Parentheses can be used  Example: x 1  (x 2  ¬x 3 )  The Satisfiability Problem: Is there an assignment to make an expression true? The Satisfiability Problem

39 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-39 Chapter 5 Trees  Example: A  B  C  The Satisfiability Problem: Is there an assignment to make the expression A  B  C true? The Satisfiability Problem: Example V  C A B T B C VT T  F TF TFT A Value assignment

40 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-40 Chapter 5 Trees     x3x3   x1x1 x2x2 x1x1  x3x3  Formula: (x 1  ¬x 2 )  (¬ x 1  x 3 )  ¬x 3  2 n possible combinations for n variables: (T,T,T) (T,T,F) (T,F,T) (T,F,F) (F,T,T) (F,T,F) (F,F,T) (F,F,F)  Note : there are 2 n possible assignments of truth values to (x 1, ……, x n ) ∴ check O(2 n . g), where g is evaluation time The Satisfiability Problem: Complexity

41 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-41 Chapter 5 Trees left_child data value right_child typedef emun {not, and, or, true, false } logical; typedef struct node *tree_pointer; typedef struct node { tree_pointer left_child; logical data; // ¬, ,  short int value; tree_pointer right_child; } ;  Node structures The Satisfiability Problem: Data Structure  C declaration T/F

42 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-42 Chapter 5 Trees 1 0 node structure A  B  C 2 0 Program 5.8 3 0 Program 5.9 LchildDataValueRchild T. False V ^C A B T B C VT T^F TF TFT A The Satisfiability Problem: Evaluating a Formula Value combination Postorder: A B  C 

43 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-43 Chapter 5 Trees for (all 2 n possible truth value combinations) { generate the next combination; replace the variables by their values; evaluate root by traversing it in postorder; if (root->value) { printf( ) ’ return; } printf( “ no satisfiable combination\n ” ); The Satisfiability Problem: Algorithm

44 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-44 Chapter 5 Trees 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->left_child->value && node->right_child->value; break; case or: node->value = node->left_child->value || node->right_child->value; break; case true: node->value = TRUE; break; case false: node->value = FALSE; } The Satisfiability Problem: Program

45 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-45 Chapter 5 Trees  Two many null pointers in current representation of binary trees n: number of nodes number of non-null links: n – 1 total links: 2n null links: 2n  (n  1) = n + 1  Replace these null pointers with some useful “threads”. * 9 nodes 8 non-null link 10 null-link or 10 0-links 5.5 Threaded Binary Trees

46 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-46 Chapter 5 Trees  If left_child of ptr is null, replace it with a pointer to the node that would be visited before ptr in an inorder traversal. i.e. inorder predecessor of ptr  If right_child of ptr is null, replace it with a pointer to the node that would be visited after ptr in an inorder traversal. i.e. inorder successor of ptr  Replace null pointers with “threads”. Threaded BT: Using Null Fields

47 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-47 Chapter 5 Trees A B C G E I D H F dangling Inorder traversal: H, D, I, B, E, A, F, C, G  A Threaded Binary Tree Threaded BT: Example left_child: inorder predecessor right_child: inorder successor Problem: How to create a threaded BT without dangling links?

48 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-48 Chapter 5 Trees Threaded BT: Adding Head Node A B C G E I D H F dangling Inorder traversal: H, D, I, B, E, A, F, C, G Root = Head Node left_child: inorder predecessor right_child: inorder successor

49 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-49 Chapter 5 Trees left_thread left_child data right_child right_thread t f false: child true: thread  Data Structures for Threaded Binary tree Threaded BT: Data Structures typedef struct threaded_tree *threaded_pointer; typedef struct threaded_tree { short int left_thread; threaded_pointer left_child; char data; threaded_pointer right_child; short int right_thread; };  Node structure definition for threaded BT

50 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-50 Chapter 5 Trees  Memory Representation of A Threaded Binary tree f f -- f f A f f C f f B t t E t tF t t G f f D t tI t t H root Threaded BT: Memory Representation

51 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-51 Chapter 5 Trees t f false: child true: thread  An Empty Threaded Binary Tree Threaded BT: Empty left_thread left_child data right_child right_thread

52 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-52 Chapter 5 Trees Insert child as the right child of node parent (1) change parent->right_thread to FALSE (2) set child->left_thread and child->right_thread to TRUE (3) set child->left_child to point to parent (4) set child->right_child to parent->right_child (5) change parent->right_child to point to child  Inserting nodes into Threaded Binary tree Threaded BT: Inserting nodes

53 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-53 Chapter 5 Trees  Examples:Insert a node D as a right child of B. root parent A B C D child Case 1: parent A B C D child (1) (2) (3) root Threaded BT: Inserting Node

54 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-54 Chapter 5 Trees Case 2: root parent A B C D child E FG (1) (3) (4) (2) root parent A B C D child E FG Threaded BT: Inserting Node (cont.)

55 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-55 Chapter 5 Trees Threaded BT: Inserting Node (cont.) void insert_right(thread_pointer parent, thread_pointer child) // Insert child as the right child of parent { child->right_child = parent ->right_child; child->right_thread = parent -> right_thread; child->left_child = parent; child->left_thread = TRUE;// left_child is a thread parent->right_child = child;// attach child to parent parent->right_thread = FALSE; if (! child->right_thread) { temp = insucc(child); // returns the inorder successor of child temp->left_child = child; }  Program 5.12, p. 218

56 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-56 Chapter 5 Trees Def. Min Tree: value of parent  value of children Def. Max Tree: value of parent  value of children 5.6 Heaps 2 843 59107611 ‧‧‧ max

57 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-57 Chapter 5 Trees Def. Min Heap: (1) min tree and (2) complete binary tree Def. Max Heap: (1) max tree and (2) complete binary tree Min Heap vs. Max Heap 2 74 1086  Operations on heaps create an empty heap insert a new node delete a node …  ADT for Max Heap p. 220 insert a new node delete a node

58 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-58 Chapter 5 Trees Insertion into a max heap Max heap E.g.1. Add “5” E.g.2. Add “21” Heaps: Insert a Node 20 152 1410 ∵ complete binary tree 20 155 14102 21 1520 14102

59 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-59 Chapter 5 Trees  Insertion to a Max Heap 2 20 2 15 14 10 initial location of new node 20 15 5 14 10 2 insert 5 into heap Heaps: Insert a Node Program 5.13 p.223 Time complexity: O(log 2 n)

60 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-60 Chapter 5 Trees  Deletion from a Max Heap remove 20 15 2 14 10 2 15 14 10 15 10 2 14 Heaps: Delete a Node Time complexity: O(log 2 n) Program 5.14 p.225

61 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-61 Chapter 5 Trees  Time complexity for different data structures for priority queue  More topics about Heap on Ch. 9: Heap Structures Time complexity for Different DS

62 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-62 Chapter 5 Trees Def. Binary Search Tree, T is a binary tree T = ψ or T≠ψ (1) all keys are distinct (2) keys in left subtree are smaller than the key in the root (3) keys in right subtree are larger than the key in the root (4) the left and right subtree are also b.s.t. E.g. Fig 5.30, p.227 (a) 〤, (b) V, (c) V 5.7 Binary Search Tree 30 5 40 2 60 70 65 80 20 25 12 10 15 22 Not a binary search tree binary search trees

63 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-63 Chapter 5 Trees Searching a binary search tree Recursive Search of a Binary Search Tree p. 227 Program 5.15 ‧‧‧‧ Iterative Search of a Binary Search Tree p. 228 Program 5.16 Ex. Binary Search Tree: Searching

64 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-64 Chapter 5 Trees 30 5 40 2 Insert 80 Insert 35 80 30 5 40 2 35 80 30 5 40 2 Binary Search Tree: Insert Node

65 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-65 Chapter 5 Trees 30 5 2 80 1 2 X T1 T2 1 T1 T2 delete 30 2 80 Binary Search Tree: Deletion  Case 1: One child or Leaf Node

66 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-66 Chapter 5 Trees 40 55 20 10 3050 70 45 52 Before deleting 60 After deleting 60  Case 2: Two children 40 60 20 10 30 50 70 45 55 52 Binary Search Tree: Deletion (cont.)

67 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-67 Chapter 5 Trees 5.8 Selection Trees ( 暫略 )

68 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-68 Chapter 5 Trees General tree K-ary tree: if k is the max degree of any node Node structure: or Waste space 5.9 Forests ……… 5-ary tree A 7-ary tree Data 1 2 ....... K

69 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-69 Chapter 5 Trees Definition: A forest is a set of n  0 disjoint trees A B C D E F G H I Forests: Definition Figure 5.34: Three-tree forest

70 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-70 Chapter 5 Trees  Definition: If T 1, T 2, …, T n : a forest of trees B(T 1, T 2, …, T n ): a binary tree corresponding to this forest we define B(T 1, T 2, …, T n ) by (1)empty, if n = 0 (2)has root equal to root(T 1 ) has left subtree equal to B(T 11, T 12, …, T 1m ) has right subtree equal to B(T 2, T 3, …, T n ) Transform a Forest into a Binary Tree A BC D E F G H I G H I A B C D F E Forest Binary tree B

71 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-71 Chapter 5 Trees To represent any forest as a binary tree. Consider the following forest Step 1: Transform each tree into a binary tree BC A EF D G HJ K Transform a Forest into a Binary Tree (cont.) BC A K EF D G HJ

72 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-72 Chapter 5 Trees Step 2: Regard the other trees as the sibling of the first tree and link their roots together Step 3: Tilt tree diagram 45° clockwise BC A K EF D G HJ B C A K E F D G H J Transform a Forest into a Binary Tree (cont.)

73 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-73 Chapter 5 Trees Preorder - If F is empty, then return - Visit the root of the first tree of F - Traverse the subtrees of the first tree in tree preorder - Traverse the remaining trees of F in tree preorder Forest Traversals: Preorder preorder: A B C D E F G H I G H I A B C D F E A B C D E F G H I Equivalent Preorder is a time-honored concept which might be called dynastic order.  order of succession to the throne.

74 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-74 Chapter 5 Trees Inorder - If F is empty, then return -Traverse the subtrees of the first tree in tree inorder - Visit the root of the first tree - Traverse the remaining trees in tree inorder Forest Traversals: Inorder inorder:B C D A F E H I G G H I A B C D F E A B C D E F G H I Equivalent

75 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-75 Chapter 5 Trees Postorder - If F is empty, then return - Traverse the subtrees of the first tree in tree postorder - Traverse the remaining trees in tree postorder - Visit the root of the first tree Note: Postorder has no corresponding binary tree traversal of a forest G H I A B C D F E A B C D E F G H I Postorder: D C B F I H G E A Postorder: B C D F E H I G A Forest Traversals: Postorder Equivalent

76 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-76 Chapter 5 Trees Manipulation of Algebraic Formula Set Representation Decision Tree A Huffman Decode Tree … Applications of Trees ( 略 )

77 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-77 Chapter 5 Trees Evaluate a Expression: in §3.4, p.116 X = A/B-C+D*E-A*C ------ (Formula, p.116) Postorder => AB/C-DE*+AC*- => Program 3.9 void eval(expression e) (p.122)  Formula => Binary Tree  Postorder Traversal on tree AB/C-DE*+AC*- => To evaluate the Expression/Formula - *+ -* A C /C AB DE Manipulation of Algebraic Formula

78 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-78 Chapter 5 Trees A Huffman Decode Tree Consider the coding schemes for {A,B,C,D,E} Method 1: Method 2: A: 000 B: 001 C: 010 D: 011 E: 100 average length = 3 bits/char encoding: 010 100 011 C E D A: 1 B: 01 C: 001 D: 0001 E: 0000 average length = (1+2+3+4+4)/5 = 2.8 bits/char encoding: 01 0000 001 B E D Method 3: : Huffman coding the most commonly occurring characters are represented by the shortest strings. Assume: A: 30% - 01 B: 5% - 0000 C: 10% - 0001 D: 20% - 001 E: 35% - 1 5% 20% 100% 65% 35% 15% B C D A E 0 0 0 10% 0 1 1 1 1 35% 30% average length = 2*30%+4*5%+4*10%+3*20%+1*35% = 2.15 bits/char encoding: 01 001 0000 A D B

79 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-79 Chapter 5 Trees Consider the “eight coins” problem: Given coins a, b, c, d, e, f, g, h. One is a counterfeit and has a different weight than the others. We want to determine which coin it is, making use of an equal arm balance. Decision Trees a+b+c ? d+e+f a+d ? b+e g ? h > > > >> > > > > > > > = == = = == = == = < <<< a?bc?ab?a g?a h?aa?ba?cb?a aHeLcHfLbHdLgHhLhHgLbLdHcLfHaLeH

80 Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-80 Chapter 5 Trees Procedure EIGHTCOINS (*eight weights are input; the different one is discovered using only 3 comparisons *) read(a, b, c, d, e, f, g, h) Case :a+b+c=d+e+f: If g>h then call COMP(g, h, a) else call COMP(h, g, a) :a+b+c>d+e+f: case :a+d=b+e: call COMP(c, f, a) :a+d>b+e: call COMP(a, e, b) :a+d<b+e: call COMP(b, d, a) end :a+b+c<d+e+f: case :a+d=b+e: call COMP(f, c, a) :a+d>b+e: call COMP(d, b, a) :a+d<b+e: call COMP(e, a, b) end End EIGHTCOINS Decision Trees (cont.) Procedure COMP(x, y, z) If x > y then print (x ‘heavy’) else print (y ‘light’) End COMP.


Download ppt "Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-1 Chapter 5 Trees Introduction to Data Structures CHAPTER 5 Trees 5.1 Introduction 5.2 Binary Trees 5.3."

Similar presentations


Ads by Google