Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees-1 Data Structures with C Chpater-5 Course code: 10CS35

Similar presentations


Presentation on theme: "Trees-1 Data Structures with C Chpater-5 Course code: 10CS35"— Presentation transcript:

1 Trees-1 Data Structures with C Chpater-5 Course code: 10CS35
Prepared by : Sandhya Kumari, M. Chandana, Alpana Dhaiya & N Sushma Department: Computer Science & Engineering 21-Sep-18

2 Introduction: A tree is a finite set of one or more nodes such that: (i) there is a specially designated node called the root; (ii) the remaining nodes are partitioned into n 0 disjoint sets T1, ...,Tn where each of these sets is a tree. T1, ...,Tn are called the subtrees of the root. A tree structure means that the data is organized so that items of information are related by branches.

3 Some basic terminology for trees:
Trees are formed from nodes and edges. Nodes are sometimes called vertices. Edges are sometimes called branches. Nodes may have a number of properties including value and label. Edges are used to relate nodes to each other. In a tree, this relation is called "parenthood." An edge {a,b} between nodes a and b establishes a as the parent of b. Also, b is called a child of a. Although edges are usually drawn as simple lines, they are really directed from parent to child. In tree drawings, this is top-to-bottom.

4 Informal Definition: a tree is a collection of nodes, one of which is distinguished as "root,“ along with a relation ("parenthood") that is shown by edges. Formal Definition: This definition is "recursive" in that it defines tree in terms of itself. The definition is also "constructive" in that it describes how to construct a tree. 1. A single node is a tree. It is "root.” 2. Suppose N is a node and T1, T2, ..., Tk are trees with roots n1, n2, ...,nk, respectively. We can construct a new tree T by making N the parent of the nodes n1, n2, ..., nk. Then, N is the root of T and T1, T2, ..., Tk are subtrees.

5 More terminology A node is either internal or it is a leaf.
A leaf is a node that has no children. Every node in a tree (except root) has exactly one parent. The degree of a node is the number of children it has. The degree of a tree is the maximum degree of all of its nodes.

6 Paths and Levels Definition: A path is a sequence of nodes n1, n2, ..., nk such that node ni is the parent of node ni+1 for all 1 <= i <= k. Definition: The length of a path is the number of edges on the path (one less than the number of nodes). Definition: The descendents of a node are all the nodes that are on some path from the node to any leaf. Definition: The ancestors of a node are all the nodes that are on the path from the node to the root. Definition: The depth of a node is the length of the path from root to the node. The depth of a node is sometimes called its level. Definition: The height of a node is the length of the longest path from the node to a leaf. Definition: The height of a tree is the height of its root.

7 Example: In the example above:
The nodes Y, Z, U, V, and W are leaf nodes. The nodes R, S, T, and X are internal nodes. The degree of node T is 3. The degree of node S is 1. The depth of node X is 2. The depth of node Z is 3. The height of node Z is zero. The height of node S is 2. The height of node R is 3. The height of the tree is the same as the height of its root R. Therefore the height of the tree is 3. The sequence of nodes R,S,X is a path. The sequence of nodes R,X,Y is not a path because the sequence does not satisfy the "parenthood" property (R is not the parent of X).

8 Representation of trees
List representation left child right sibling representation 3. Representation as a degree two tree

9 Binary Trees: Definition: A binary tree is a tree in which each node has degree of exactly 2 and the children of each node are distinguished as "left" and "right." Some of the children of a node may be empty. Formal Definition: A binary tree is: 1. either empty, or 2. it is a node that has a left and a right subtree, each of which is a binary tree. Definition: A full binary tree (FBT) is a binary tree in which each node has exactly 2 non-empty children or exactly two empty children, and all the leaves are on the same level. Definition: A complete binary tree (CBT) is a FBT except, perhaps, that the deepest level may not be completely filled. If not completely filled, it is filled from left-to-right.

10 Binary tree Traversals
Preorder procedure preorder(x){ visit(x) i=1; while( i <= degree() ){ preorder( child(i) ) } Postorder procedure postorder(x){ i=1; while( i <= degree() ){ postorder( child(i) ) } visit(x) Inorder procedure inorder(x){ if( left_child_for(x) ) { inorder( left_child(x) ) } visit(x) if( right_child_for(x) ) { inorder( right_child(x) ) } }

11 Heap A heap is a complete tree with an ordering- relation R holding between each node and its descendant.

12 12/8/13

13 Chapter 5 Trees: Outline
Introduction Representation Of Trees Binary Trees Binary Tree Traversals Additional Binary Tree Operations Threaded Binary Trees Heaps Binary Search Trees Selection Trees Forests 12/8/13

14 Introduction (1/8) A tree structure means that the data are organized so that items of information are related by branches Examples: 12/8/13

15 Every node in the tree is the root of some subtree
Introduction (2/8) Definition (recursively): A tree is a finite set of one or more nodes such that There is a specially designated node called root. The remaining nodes are partitioned into n>=0 disjoint set T1,…,Tn, where each of these sets is a tree. T1,…,Tn are called the subtrees of the root. Every node in the tree is the root of some subtree 12/8/13

16 Introduction (3/8) Some Terminology node: the item of information plus the branches to each node. degree: the number of subtrees of a node degree of a tree: the maximum of the degree of the nodes in the tree. terminal nodes (or leaf): nodes that have degree zero nonterminal nodes: nodes that don’t belong to terminal nodes. children: the roots of the subtrees of a node X are the children of X parent: X is the parent of its children. 12/8/13

17 Some Terminology (cont’d)
Introduction (4/8) Some Terminology (cont’d) siblings: children of the same parent are said to be siblings. Ancestors of a node: all the nodes along the path from the root to that node. The level of a node: defined by letting the root be at level one. If a node is at level l, then it children are at level l+1. Height (or depth): the maximum level of any node in the tree 12/8/13

18 Property: (# edges) = (#nodes) - 1
Introduction (5/8) Example A is the root node B is the parent of D and E C is the sibling of B D and E are the children of B D, E, F, G, I are external nodes, or leaves A, B, C, H are internal nodes The level of E is 3 The height (depth) of the tree is 4 The degree of node B is 2 The degree of the tree is 3 The ancestors of node I is A, C, H The descendants of node C is F, G, H, I Property: (# edges) = (#nodes) - 1 A B C H I D E F G Level 1 2 3 4 12/8/13

19 ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
Introduction (6/8) Representation Of Trees List Representation we can write of Figure 5.2 as a list in which each of the subtrees is also a list ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) The root comes first, followed by a list of sub-trees 12/8/13

20 Representation Of Trees (cont’d)
Introduction (7/8) Representation Of Trees (cont’d) Left Child- Right Sibling Representation 12/8/13

21 Representation Of Trees (cont’d)
Introduction (8/8) Representation Of Trees (cont’d) Representation As A Degree Two Tree 12/8/13

22 Definition (recursive):
Binary Trees (1/9) Binary trees are characterized by the fact that any node can have at most two branches Definition (recursive): 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 subtree and the right subtree Thus the left subtree and the right subtree are distinguished Any tree can be transformed into binary tree by left child-right sibling representation A B A B 12/8/13

23 The abstract data type of binary tree
Binary Trees (2/9) The abstract data type of binary tree 12/8/13

24 Binary Trees (3/9) Two special kinds of binary trees: (a) skewed tree, (b) complete binary tree The all leaf nodes of these trees are on two adjacent levels 12/8/13

25 Properties of binary trees
Lemma 5.1 [Maximum number of nodes]: The maximum number of nodes on level i of a binary tree is 2i-1, i 1. The maximum number of nodes in a binary tree of depth k is 2k-1, k1. Lemma 5.2 [Relation between number of leaf nodes and degree-2 nodes]: For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 is the number of nodes of degree 2, then n0 = n2 + 1. These lemmas allow us to define full and complete binary trees 12/8/13

26 Binary Trees (5/9) Definition: A full binary tree of depth k is a binary tree of death k having 2k-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. From Lemma 5.1, the height of a complete binary tree with n nodes is log2(n+1) 12/8/13

27 Binary tree representations (using array)
Binary Trees (6/9) Binary tree representations (using array) 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 parent(i) is at i /2 if i  If i = 1, i is at the root and has no parent. LeftChild(i) is at 2i if 2i  n. If 2i  n, then i has no left child. RightChild(i) is at 2i+1 if 2i+1  n. If 2i +1  n, then i has no left child A B D C E 1 2 3 4 5 6 7 [1] [2] [3] [4] [5] [6] [7] A B C — D — E Level 1 Level 2 Level 3 12/8/13

28 Binary tree representations (using array)
Binary Trees (7/9) Binary tree representations (using array) Waste spaces: in the worst case, a skewed tree of depth k requires 2k-1 spaces. Of these, only k spaces will be occupied Insertion or deletion of nodes from the middle of a tree requires the movement of potentially many nodes to reflect the change in the level of these nodes 12/8/13

29 Binary tree representations (using link)
Binary Trees (8/9) Binary tree representations (using link) 12/8/13

30 Binary tree representations (using link)
Binary Trees (9/9) Binary tree representations (using link) 12/8/13

31 Binary Tree Traversals (1/9)
How to traverse a tree or visit each node in the tree exactly once? 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), LRV (postorder), VLR (preorder) data right_child left_child L: moving left R: moving right V : visiting node 12/8/13

32 Binary Tree Traversals (2/9)
Arithmetic Expression using binary tree 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 12/8/13

33 Binary Tree Traversals (3/9)
Inorder traversal (LVR) (recursive version) output: A / B * C * D + E ptr L V R 12/8/13

34 Binary Tree Traversals (4/9)
Preorder traversal (VLR) (recursive version) output: + * * / A B C D E V L R 12/8/13

35 Binary Tree Traversals (5/9)
Postorder traversal (LRV) (recursive version) output: A B / C * D * E + L R V 12/8/13

36 Binary Tree Traversals (6/9)
Iterative inorder traversal we use a stack to simulate recursion 5 A 4 / 8 B 11 C 3 * 14 D 2 * 17 E 1 + L V R output: A / B * C * D + E node 12/8/13

37 Binary Tree Traversals (7/9)
Analysis of inorder2 (Non-recursive Inorder traversal) Let n be the number of nodes in the tree Time complexity: O(n) Every node of the tree is placed on and removed from the stack exactly once Space complexity: O(n) equal to the depth of the tree which (skewed tree is the worst case) 12/8/13

38 Binary Tree Traversals (8/9)
Level-order traversal method: We visit the root first, then the root’s left child, followed by the root’s right child. We continue in this manner, visiting the nodes at each new level from the leftmost node to the rightmost nodes This traversal requires a queue to implement 12/8/13

39 Binary Tree Traversals (9/9)
Level-order traversal (using queue) output: + * E * D / C A B 1 + 2 * 17 E 3 * 14 D 4 / 11 C 5 A 8 B FIFO 12/8/13 ptr

40 Additional Binary Tree Operations (1/7)
Copying Binary Trees we can modify the postorder traversal algorithm only slightly to copy the binary tree similar as Program 5.3 12/8/13

41 Additional Binary Tree Operations (2/7)
Testing Equality Binary trees are equivalent if they have the same topology and the information in corresponding nodes is identical V L R the same topology and data as Program 5.6 12/8/13

42 Additional Binary Tree Operations (3/7)
Variables: x1, x2, …, xn can hold only of two possible values, true or false Operators: (and), (or), ¬(not) Propositional Calculus Expression 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 (¬ >  > ) Example: x1  (x2  ¬x3) 12/8/13

43 Additional Binary Tree Operations (4/7)
Satisfiability problem: Is there an assignment to make an expression true? Solution for the Example x1  (x2  ¬x3) : If x1 and x3 are false and x2 is true false  (true  ¬false) = false  true = true For n value of an expression, there are 2n possible combinations of true and false 12/8/13

44 Additional Binary Tree Operations (5/7)
(x1  ¬x2)  (¬ x1  x3)  ¬x3 postorder traversal X3 X1 X2 data value 12/8/13

45 Additional Binary Tree Operations (6/7)
node structure For the purpose of our evaluation algorithm, we assume each node has four fields: We define this node structure in C as: 12/8/13

46 Additional Binary Tree Operations (7/7)
Satisfiability function To evaluate the tree is easily obtained by modifying the original recursive postorder traversal L R V F T node TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE 12/8/13 FALSE TRUE

47 Threaded Binary Trees (1/10)
Threads Do you find any drawback of the above tree? Too 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 Solution: replace these null pointers with some useful “threads” 12/8/13

48 Threaded Binary Trees (2/10)
Rules for constructing the threads If ptr->left_child is null, replace it with a pointer to the node that would be visited before ptr in an inorder traversal If ptr->right_child is null, replace it with a pointer to the node that would be visited after ptr in an inorder traversal 12/8/13

49 Threaded Binary Trees (3/10)
A Threaded Binary Tree root A t: true  thread f: false  child dangling B f C E t F G D dangling H D I B E A F C G inorder traversal: H I 12/8/13

50 Threaded Binary Trees (4/10)
Two additional fields of the node structure, left-thread and right-thread If ptr->left-thread=TRUE, then ptr->left-child contains a thread; Otherwise it contains a pointer to the left child. Similarly for the right-thread 12/8/13

51 Threaded Binary Trees (5/10)
If we don’t want the left pointer of H and the right pointer of G to be dangling pointers, we may create root node and assign them pointing to the root node 12/8/13

52 Threaded Binary Trees (6/10)
Inorder traversal of a threaded binary tree By using of threads we can perform an inorder traversal without making use of a stack (simplifying the task) Now, we can follow the thread of any node, ptr, to the “next” node of inorder traversal If ptr->right_thread = TRUE, the inorder successor of ptr is ptr- >right_child by definition of the threads Otherwise we obtain the inorder successor of ptr by following a path of left-child links from the right-child of ptr until we reach a node with left_thread = TRUE 12/8/13

53 Threaded Binary Trees (7/10)
Finding the inorder successor (next node) of a node threaded_pointer insucc(threaded_pointer tree){ threaded_pointer temp; temp = tree->right_child; if (!tree->right_thread) while (!temp->left_thread) temp = temp->left_child; return temp; } tree temp Inorder 12/8/13

54 Threaded Binary Trees (8/10)
Inorder traversal of a threaded binary tree void tinorder(threaded_pointer tree){ /* traverse the threaded binary tree inorder */ threaded_pointer temp = tree; for (;;) { temp = insucc(temp); if (temp==tree) break; printf(“%3c”,temp->data); } output: H D I B E A F C G tree Time Complexity: O(n) 12/8/13

55 Threaded Binary Trees (9/10)
Inserting A Node Into A Threaded Binary Tree Insert child as the right child of node parent change parent->right_thread to FALSE set child->left_thread and child->right_thread to TRUE set child->left_child to point to parent set child->right_child to parent->right_child change parent->right_child to point to child 12/8/13

56 Threaded Binary Trees (10/10)
Right insertion in a threaded binary tree void insert_right(thread_pointer parent, threaded_pointer child){ /* insert child as the right child of parent in a threaded binary tree */ 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){ temp = insucc(child); temp->left_child = child; } root A parent B C X child temp parent A child B X C D First Case Second Case E F 12/8/13 successor

57 The heap abstract data type
Heaps (1/6) The heap abstract data type Definition: A max(min) tree is a tree in which the key value in each node is no smaller (larger) than the key values in its children. A max (min) heap is a complete binary tree that is also a max (min) tree Basic Operations: creation of an empty heap insertion of a new elemrnt into a heap deletion of the largest element from the heap 12/8/13

58 The examples of max heaps and min heaps
Property: The root of max heap (min heap) contains the largest (smallest) element 12/8/13

59 Abstract data type of Max Heap
Heaps (3/6) Abstract data type of Max Heap 12/8/13

60 Queue in Chapter 3: FIFO Priority queues machine service: factory:
Heaps (4/6) Queue in Chapter 3: FIFO Priority queues Heaps are frequently used to implement priority queues delete the element with highest (lowest) priority insert the element with arbitrary priority Heaps is the only way to implement priority queue machine service: amount of time (min heap) amount of payment (max heap) factory: time tag 12/8/13

61 Insertion Into A Max Heap
Heaps (5/6) Insertion Into A Max Heap Analysis of insert_max_heap The complexity of the insertion function is O(log2 n) insert 21 5 *n= 6 5 i= 3 7 1 3 6 [1] 21 20 parent sink [2] [3] item upheap 15 20 5 2 [4] [5] [6] [7] 14 10 2 5 12/8/13

62 Deletion from a max heap
Heaps (6/6) Deletion from a max heap After deletion, the heap is still a complete binary tree Analysis of delete_max_heap The complexity of the insertion function is O(log2 n) < parent = 4 1 2 *n= 5 4 child = 4 8 2 [1] 20 15 [2] [3] 14 15 2 item.key = 20 [4] [5] temp.key = 10 14 10 10 12/8/13

63 Binary Search Trees (1/8)
Why do binary search trees need? Heap is not suited for applications in which arbitrary elements are to be deleted from the element list a min (max) element is deleted O(log2n) deletion of an arbitrary element O(n) search for an arbitrary element O(n) Definition of binary search tree: Every element has a unique key The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the root of subtree The left and right subtrees are also binary search trees 12/8/13

64 Binary Search Trees (2/8)
Example: (b) and (c) are binary search trees medium smaller larger 12/8/13

65 Binary Search Trees (3/8)
44 32 65 88 28 17 80 76 97 82 54 29 Search(25) Search(76) 12/8/13

66 Binary Search Trees (4/8)
Searching a binary search tree O(h) 12/8/13

67 Binary Search Trees (5/8)
Inserting into a binary search tree An empty tree 12/8/13

68 Binary Search Trees (6/8)
Deletion from a binary search tree Three cases should be considered case 1. leaf  delete case 2. one child  delete and change the pointer to this child case 3. two child  either the smallest element in the right subtree or the largest element in the left subtree 12/8/13

69 Binary Search Trees (7/8)
Height of a binary search tree The height of a binary search tree with n elements can become as large as n. It can be shown that when insertions and deletions are made at random, the height of the binary search tree is O(log2n) on the average. Search trees with a worst-case height of O(log2n) are called balance search trees 12/8/13

70 Binary Search Trees (8/8)
Time Complexity Searching, insertion, removal O(h), where h is the height of the tree Worst case - skewed binary tree O(n), where n is the # of internal nodes Prevent worst case rebalancing scheme AVL, 2-3, and Red-black tree 12/8/13

71 There are two kinds of selection trees: winner trees and loser trees
Problem: suppose we have k order sequences, called runs, that are to be merged into a single ordered sequence Solution: straightforward : k-1 comparison selection tree : log2k+1 There are two kinds of selection trees: winner trees and loser trees 12/8/13

72 Definition: (Winner tree)
Selection Trees (2/6) Definition: (Winner tree) a selection tree is the binary tree where each node represents the smaller of its two children root node is the smallest node in the tree a winner is the record with smaller key Rules: tournament : between sibling nodes put X in the parent node  X tree where X = winner or loser 12/8/13

73 Winner Tree sequential allocation scheme (complete binary tree)
Selection Trees (3/6) Winner Tree sequential allocation scheme (complete binary tree) Each node represents the smaller of its two children ordered sequence 12/8/13

74 Analysis of merging runs using winner trees
Selection Trees (4/6) Analysis of merging runs using winner trees # of levels: log2K  +1  restructure time: O(log2K) merge time: O(nlog2K) setup time: O(K) Slight modification: tree of loser consider the parent node only (vs. sibling nodes) 12/8/13

75 After one record has been output
Selection Trees (5/6) After one record has been output 6 6 6 6 15 12/8/13

76 Tree of losers can be conducted by Winner tree
Selection Trees (6/6) Tree of losers can be conducted by Winner tree 6 8 9 17 10 20 9 90 12/8/13

77 Transforming a forest into a binary tree
Forests (1/4) Definition: A forest is a set of n  0 disjoint trees Transforming a forest into a binary tree Definition: If T1,…,Tn is a forest of trees, then the binary tree corresponding to this forest, denoted by B(T1,…,Tn ): is empty, if n = 0 has root equal to root(T1); has left subtree equal to B(T11,T12,…,T1m); and has right subtree equal to B(T2,T3,…,Tn) where T11,T12,…,T1m are the subtrees of root (T1) 12/8/13

78 Rotate the tree clockwise by 45 degrees
Forests (2/4) Rotate the tree clockwise by 45 degrees A Leftmost child A E G B E B C D H I F C F G Right sibling D H I 12/8/13

79 Forest traversals Forest preorder traversal Forest inorder traversal
Forests (3/4) Forest traversals Forest preorder traversal 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 tree of F in preorder. Forest inorder traversal If F is empty, then return Traverse the subtrees of the first tree in tree inorder Visit the root of the first tree of F Traverse the remaining tree of F in inorder Forest postorder traversal Traverse the subtrees of the first tree in tree postorder Traverse the remaining tree of F in postorder 12/8/13

80 Forests (4/4) preorder: A B C D E F G H I inorder: B C A E D G H F I A E, D, G, H, F, I B, C preorder: A B C (D E F G H I) inorder: B C A (E D G H F I) A B D C E F, G, H, I 12/8/13

81 Two operations considered here
Set Representation(1/13) S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5} Two operations considered here Disjoint set union S1  S2={0,6,7,8,1,4,9} Find(i): Find the set containing the element i  S3, 8  S1 2 4 5 6 7 8 1 9 3 Si  Sj =  12/8/13

82 Set Representation(2/13)
Union and Find Operations Make one of trees a subtree of the other 4 1 9 6 7 8 4 9 6 7 8 1 Possible representation for S1 union S2 12/8/13

83 Set Representation(3/13)
6 7 8 4 1 9 2 5 3 12/8/13 *Figure 5.41:Data Representation of S1S2and S3 (p.240)

84 Set Representation(4/13)
Array Representation for Set i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] parent - 4 3 2 int find1(int i) { for(; parent[i] >= 0; i = parent[i]); return i; } void union1(int i, int j) { parent[i] = j; Program 5.18: Initial attempt at union-find function (p.241) 12/8/13

85 Set Representation(5/13)
union(0,1), find(0) union(1,2), find(0) . union(n-2,n-1),find(0) union operation O(n) n-1 find operation O(n2) n-2 degenerate tree *Figure 5.43:Degenerate tree (p.242) 12/8/13

86 Set Representation(6/13)
weighting rule for union(i,j): if # of nodes in i < # in j then j the parent of i 12/8/13

87 Set Representation(7/13)
Modified Union Operation void union2(int i, int j) { int temp = parent[i] + parent[j]; if (parent[i] > parent[j]) { parent[i] = j; /*make j the new root*/ parent[j] = temp; } else { parent[j] = i;/* make i the new root*/ parent[i] = temp; Keep a count in the root of tree If the number of nodes in tree i is less than the number in tree j, then make j the parent of i; otherwise make i the parent of j. 12/8/13

88 Set Representation(8/13)
Figure 5.45:Trees achieving worst case bound (p.245) 12/8/13  log28+1

89 Set Representation(9/13)
The definition of Ackermann’s function used here is : P=0 q=0 and p >= 1 A ( p, q) = P>=1 and p = 1 p>=1 and q >= 2 12/8/13

90 Set Representation(10/13)
Modified Find(i) Operation Int find2(int i) { int root, trail, lead; for (root=i;parent[root]>=0;oot=parent[root]) ; for (trail=i; trail!=root; trail=lead) { lead = parent[trail]; parent[trail]= root; } return root; If j is a node on the path from i to its root then make j a child of the root 12/8/13

91 Set Representation(11/13)
6 4 1 2 4 7 1 2 3 5 6 5 3 7 find(7) find(7) find(7) find(7) find(7) find(7) find(7) find(7) go up reset 12 moves (vs. 24 moves) 12/8/13

92 Set Representation(12/13)
Applications Find equivalence class i  j Find Si and Sj such that i  Si and j  Sj (two finds) Si = Sj do nothing Si  Sj union(Si , Sj) example 0  4, 3  1, 6  10, 8  9, 7  4, 6  8, 3  5, 2  11, 11  0 {0, 2, 4, 7, 11}, {1, 3, 5}, {6, 8, 9, 10} 12/8/13

93 Set Representation(13/13)
12/8/13

94 Counting Binary trees(1/10)
Distinct Binary Trees : If n=0 or n=1, there is only one binary tree. If n=2 and n=3, 12/8/13

95 Counting Binary trees(2/10)
Stack Permutations preorder: A B C D E F G H I inorder: B C A E D G H F I A A D D, E, F, G, H, I B, C B A C E F D, E, F, G, H, I B G I 12/8/13 H C

96 Counting Binary trees(3/10)
Figure5.49(c) with the node numbering of Figure 5.50.Its preorder permutation is 1,2…,9, and its inorder permutation is 2,3,1,5,4, 7,8,6,9. 1 4 2 3 5 6 7 9 12/8/13 8

97 Counting Binary trees(4/10)
If we start with the numbers1,2,3, then the possible permutations obtainable by a stack are: (1,2,3) (1,3,2) (2,1,3) (2,3,1) (3,2,1) Obtaining(3,1,2) is impossible. 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 12/8/13

98 Counting Binary trees(5/10)
Matrix Multiplication Suppose that we wish to compute the product of n matrices: M1 * M * Mn Since matrix multiplication is associative, we can perform these multiplications in any order. We would like to know how many different ways we can perform these multiplications . For example, If n =3, there are two possibilities: (M1*M2)*M3 M1*(M2*M3) 12/8/13

99 Counting Binary trees(6/10)
Let be the number of different ways to compute the product of n matrices. Then , and Let be the product The product we wish to compute is by computing any one of the products The number of distinct ways to obtain are and ,respectively. Therefore, letting =1, we have: 12/8/13

100 Counting Binary trees(7/10)
Now instead let be the number of distinct binary trees with n nodes. Again an expression for in terms of n is what we want. Than we see that is the sum of all the possible binary trees formed in the following way: a root and two subtrees with and nodes, for This explanation says that and bn bi bn-i-1 12/8/13

101 Counting Binary trees(8/10)
Number of Distinct Binary Trees: To obtain number of distinct binary trees with n nodes, we must solve the recurrence of Eq.(5.5).To begin we let: (5.6) Which is the generating function for the number of binary trees. Next observe that by the recurrence relation we get the identity: Using the formula to solve quadratics and the fact (Eq. (5.5)) that B(0) = = 1 ,we get 12/8/13

102 Counting Binary trees(9/10)
Number of Distinct Binary Trees: We can use the binomial theorem to expand to obtain: (5.7) 12/8/13

103 Counting Binary trees(10/10)
Number of Distinct Binary Trees: Comparing Eqs.(5.6) and (5.7) we see that , which is the coeffcient of in B(x), is : Some simplification yields the more compact form which is approximately 12/8/13

104 Thank You..


Download ppt "Trees-1 Data Structures with C Chpater-5 Course code: 10CS35"

Similar presentations


Ads by Google