DS-chapter5(Leftist Heap,Skew H,binomial queue)

DS-chapter5(Leftist Heap,Skew H,binomial queue) - CHAPTER 5...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 5 PRIORITY QUEUES (HEAPS) —— delete the element with the highest \ lowest priority
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5.6 Leftist Heaps 1. Structure Property: 0 0 0 1 0 1 0 1 0 1* 0 1 0 It’s a leftist heap ( very unbalanced) It’s not a leftist heap A structural property, an ordering property H Definition R the null path length Npl(X) is the length of the shortest path from any node X to a node without two children. The Npl of a node with zero or one child is 0. Npl (NULL)= -1 the leftist heaps: 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.
Background image of page 2
( Theorem R : a leftist tree with r nodes on the right path must have at least 2 r -1 nodes. Induction: If r = 1 ,there must be at least one tree node. Suppose that the theorem is true for 1,2,…. .r. Consider : a leftist tree with r+1 node on the right path, the root has a right subtree with r nodes on the right path, a left subtree with at least r nodes on the right path. left subtree right subtree root r+1 node on the right path right subtree r node on the right path right subtree at least r node on the right path This tree has at least 2 r -1 nodes This tree has at least 2 r -1 nodes Leftist Heap A leftist tree of N nodes has a right path containing at most log( N+1 ) nodes
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
PriorityQueue Initialize( void ); ElementType FindMin( PriorityQueue H ); int IsEmpty(priorityQueue H); PriorityQueue Merge(PriorityQueue H1, PriorityQueue H2); PriorityQueue Insert1(ElementType X, PriorityQueue H); PriorityQueue DeleteMin1(PriorityQueue H) Leftist Heap 2. Leftist Heap Operations: Struct TreeNode { ElementType Element; PriorityQueue Left; PriorityQueue Right; int Npl; }
Background image of page 4
Merge routine Leftist Heap PriorityQueue Merge ( PriorityQueue H1, PriorityQueue H2) { if (H1 == NULL ) return H2; if (H2 == NULL) return H1; if (H1 ->Element < H2 ->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, H1->Npl is already 0 */ else {
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

DS-chapter5(Leftist Heap,Skew H,binomial queue) - CHAPTER 5...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online