Sei sulla pagina 1di 22

Min-Max Heaps

A double-ended priority queue is a data structure that supports the following operations:
inserting an element with an arbitrary key deleting an element with the largest key deleting an element with the smallest key

A Min-Max Heap supports all of the above operations.

Min-Max Heap (Cont.)


Definition: A min-max heap is a complete binary tree such that if it is not empty, each element has a data member called key. Alternating levels of this tree are min levels and max levels, respectively. The root is on a min level. Let x be any node in a min-max heap. If x is on a min (max) level then the element in x has the minimum (maximum) key from among all elements in the subtree with root x. A node on a min (max) level is called a min (max) node.

Figure 9.1: A 12-element Min-Max Heap


7 min

70 30 9 10

40 15

max min max

45

50

30

20

12

Min-Max Heap (Cont.)


The min-max heap stores in a one-dimension array h. Insert a key 5 into this min-max heap. Initially key 5 is inserted at j. Now since 5 < 10 (which is js parent), 5 is guaranteed to be smaller than all keys in nodes that are both on max levels and on the path from j to root. Only need to check nodes on min levels. When inserting 80 into this min-max heap, since 80 > 10, and 10 is on the min level, we are assured that 80 is larger than all keys in the nodes that are both on min levels and on the path from j to the root. Only need to check nodes on max levels.

Insert to Min-Max Heap


7 min

70 30 9 10

40 15 j

max min max

45

50

30

20

12

Min-Max Heap After Inserting Key 5


5 min

70 30 9 7

40 15 10

max min max

45

50

30

20

12

Min-Max Heap After Inserting Key 80


7 min

70 30 9 10

80 15 40

max min max

45

50

30

20

12

Program 9.3 Insertion Into A Min-Max Heap


template <class KeyType>

void MinMaxHeap<KeyType>::Insert(const Element<KeyType>& x)


// inset x into the min-max heap { if (n==MaxSize) {MinMaxFull(); return;} n++; int p =n/2; // p is the parent of the new node if(!p) {h[1] = x; return;} // insert into an empty heap switch(level(p)) { case MIN: if (x.key < h[p].key) { // follow min levels h[n] = h[p]; VerifyMin(p, x); } else { VerifyMax(n, x); } // follow max levels break; case MAX: if (x.key > h[p].key) { // follow max levels h[n] = h[p]; VerifyMax(p, x); } else { VerifyMin(n, x); } // follow min levels break; } }

Program 9.4 Searching For The Correct Max Node For Insertion
template <class KeyType> void MinMaxHeap<KeyType>::VerifyMax(int i, const Elemetn<KeyType>& x) // Follow max nodes from the max node I to the root and insert x at proper place { for (int gp = i/4; // grandparent of i gp && (x.key > h[gp].key); gp /= 4) O(log n) { // move h[gp] to h[i] h[i] = h[gp]; i = gp; } h[i] = x; // x is to be inserted into node i }

Deletion of the Min Element


12 min

70 30 9 10

40 15

max min max

45

50

30

20

Deletion of the Min Element (Cont.)


When delete the smallest key from the min-max heap, the root has the smallest key (key 7). So the root is deleted. The last element with key 12 is also deleted from the minmax heap and then reinsert into the min-max heap. Two steps to follow:

The root has no children. In this case x is to be inserted into the root. The root has at least one child. Now the smallest key in the min-max heap is in one of the children or grandchildren of the root. Assume node k has the smallest key, then following conditions must be considered:

x.key h[k].key. x may be inserted into the root. x.key >h[k].key and k is a child of the root. Since k is a max node, it has not descendents with key larger than h[k].key. So, node k has no descendents with key larger than x.key. So the element h[k] may be moved to the root, and x can be inserted into node k. x.key> h[k] and k is a grandchild of the root. h[k] is moved to the root. Let p the parent of k. If x.key > h[p].key, then h[p] and x are to be interchanged.

Min-Max Heap After Deleting Min Element


9 min

70 30 12 10

40 15

max min max

45

50

30

20

Deaps
A deap is a double-ended heap that supports the double-ended priority operations of insert, delet-min, and delete-max. Similar to min-max heap but deap is faster on these operations by a constant factor, and the algorithms are simpler.

Deaps (Cont.)
Definition: A deap is a complete binary tree that is either empty or satisfies the following properties: (1) The root contains no element (2) The left subtree is a min heap. (3) The right subtree is a max heap. (4) If the right subtree is not empty, then let i be any node in the left subtree. Let j be the corresponding node in the right subtree. If such a j does not exist, then let j be the node in the right subtree that corresponds to the parent of i. The key in node i is less than or equal to that of j.

A Deap Example

5 10 8 25

45 40

15

19

30

20

Deap (Cont.)
From Deaps definition, its obvious that for an n-element deap, the min element is the root of min heap and the max element is the root of the max heap. If n = 1, then the min and max elements are the same and are in the root of the min heap. Since deap is a complete binary tree, it may be stored as an implicit data structure in a one-dimension array similar to min, max, min-max heaps. In the case of deap, the position 1 of the array is not used. For an n-element deap, it occupied n+1 element of an array. If i is a node in the min heap of the deap, its corresponding node in the max heap is i 2 log 2 i 1. Then j defined in property (4) of definition is given by

j i 2 log 2 i 1 ;

if (j > n+1) j /= 2;

Figure 9.7 Insertion Into A Deap

5 10 8 25

45 40 j

15

19
i

30

20

Figure 9.8: Deap Structure After Insertion of 4

4 5 8 25

45 40

15

10

30

20

19

Figure 9.8: Deap Structure After Insertion of 30

5 10 8 30

45 40

15

19

30

20

25

Program 9.7: Inserting Into A Deap


template <class KeyType> void Deap<KeyType>::Insert(const Element<KeyType>& x) { //Insert x into the deap int I; if (n==MaxSize) {DeapFull(); return;} n++; if (n==1) {d[2]=x; return;} // insert into an empty deap int p = n+1; // p is the new last position of the deap switch(MaxHead(p)) { case TRUE: // p is a position in the max heap i = MinPartner(p); if (x.key < d[i].key) { O(log d[p] = d[i]; MinInsert(i, x); } else MaxInsert(p, x); break; case FALSE: // p is a position in the min heap i = MaxPartner(p); if (x.key > d[i].key) { d[p] = d[i]; MaxInsert(i, x); } else MinInsert(p, x); } }

n)

Deletion of Min Element


Suppose we want to remove the minimum element from the deap.
We first place the last element into a temporary element t. Vacancy created by the deletion of the min element is filled by moving from position 2 to a leaf. Each move is preceded by a step that moves the smaller of the elements in the children of the current node up. Then move to the position previously occupied by the moved element. Repeat the process until we move the empty node to a leaf position. Compare the key put in the temporary element with the max partner. If <, no exchange is needed. The temporary element is inserted into the empty leaf node. If >, exchange them.

Deap Structure After Deletion of Min Element

8 10 9 25

45 40

15

19

20

30

Potrebbero piacerti anche