# §6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.

## Presentation on theme: "§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property."— Presentation transcript:

§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property – the same Structure Property – binary tree, but unbalanced 1/10 Discussion 11: How fast can we merge two heaps if we simply use the original heap structure?

§6 Leftist Heaps 【 Definition 】 The null path length, Npl(X), of any node X is the length of the shortest path from X to a node without two children. Define Npl(NULL) = –1. Note: Npl(X) = min { Npl(C) + 1 for all C as children of X } Note: Npl(X) = min { Npl(C) + 1 for all C as children of X } 【 Definition 】 The leftist heap property is that for every node X in the heap, the null path length of the left child is at least as large as that of the right child. 0 00 10 1 00 10 10 1  The tree is biased to get deep toward the left. 2/10

§6 Leftist Heaps 【 Theorem 】 A leftist tree with r nodes on the right path must have at least 2 r – 1 nodes. Proof: By induction on p. 162. Trouble makers: Insert and Merge Note: Insertion is merely a special case of merging. 3/10 Discussion 12: How long is the right path of a leftist tree of N nodes? What does this conclusion mean to us?

§6 Leftist Heaps struct TreeNode { ElementType Element; PriorityQueue Left; PriorityQueue Right; int Npl; } ;  Declaration:  Merge (recursive version): 3 10 2114 23 8 17 26 6 12 1824 33 7 3718 H1H1 H2H2 Step 1 : Merge( H 1 ->Right, H 2 ) 8 17 26 6 12 1824 33 7 18 37 Step 2 : Attach( H 2, H 1 ->Right ) 3 10 2114 238 17 26 6 12 1824 33 7 18 37 Step 3 : Swap(H 1 ->Right, H 1 ->Left ) if necessary 4/10

§6 Leftist Heaps PriorityQueue Merge ( PriorityQueue H1, PriorityQueue H2 ) { if ( H1 == NULL ) return H2; if ( H2 == NULL ) return H1; if ( H1->Element Element ) return Merge1( H1, H2 ); else return Merge1( H2, H1 ); } static PriorityQueue Merge1( PriorityQueue H1, PriorityQueue H2 ) { if ( H1->Left == NULL ) /* single node */ H1->Left = H2;/* H1->Right is already NULL and H1->Npl is already 0 */ else { H1->Right = Merge( H1->Right, H2 ); /* Step 1 & 2 */ if ( H1->Left->Npl Right->Npl ) SwapChildren( H1 );/* Step 3 */ H1->Npl = H1->Right->Npl + 1; } /* end else */ return H1; } What if Npl is NOT updated? T p = O(log N) 5/10

§6 Leftist Heaps  Merge (iterative version): 3 10 2114 23 8 17 26 6 12 1824 33 7 3718 H1H1 H2H2 Step 1 : Sort the right paths without changing their left children 3 10 2114 23 6 12 1824 33 7 37 188 17 26 Step 2 : Swap children if necessary 3 10 2114 23 6 12 1824 33 7 37 18 8 17 26  DeleteMin: Step 1 : Delete the root Step 2 : Merge the two subtrees T p = O(log N) Home work: p.181 5.16 Merge two given leftist heaps p.183 5.22 BuildHeap in O(N) 6/10

§7 Skew Heaps -- a simple version of the leftist heaps Target : Any M consecutive operations take at most O(M log N) time.  Merge: Always swap the left and right children except that the largest of all the nodes on the right paths does not have its children swapped. No Npl. Not really a special case, but a natural stop in the recursions. 3 10 2114 23 8 17 26 6 12 1824 33 7 3718 H1H1 H2H2 3 10 2114 23 6 12 1824 33 7 37 26 8 17 18 This is NOT always the case. 7/10

3 10 2114 23 6 12 1824 3318 7 37 26 8 17 §7 Skew Heaps 【 Example 】 Insert 15 15 10 21 3 6 12 1824 3318 7 37 26 8 17 14 23 15  Merge (iterative version): 3 10 2114 23 8 17 26 6 12 1824 33 7 3718 H1H1 H2H2 3 10 2114 23 6 12 1824 33 7 37 26 8 17 18 8/10

§7 Skew Heaps Note:  Skew heaps have the advantage that no extra space is required to maintain path lengths and no tests are required to determine when to swap children.  It is an open problem to determine precisely the expected right path length of both leftist and skew heaps. Note:  Skew heaps have the advantage that no extra space is required to maintain path lengths and no tests are required to determine when to swap children.  It is an open problem to determine precisely the expected right path length of both leftist and skew heaps. Home work: p.183 5.24 Insertions 9/10

Research Project 6 Heap Merging (25) This project requires you to implement Merge operation on ordinary heaps, leftist heaps, and skew heaps. You are to analyze and compare the performances of a sequence of merges on these heap structures. Detailed requirements can be downloaded from http://acm.zju.edu.cn/dsaa/ 10/10

Download ppt "§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property."

Similar presentations