1 / 22

Min-Max Heaps

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.

ulf
Download Presentation

Min-Max Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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.

  3. Figure 9.1: A 12-element Min-Max Heap 7 min 70 40 max 30 9 10 15 min 45 50 30 20 12 max

  4. 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 j’s 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.

  5. Insert to Min-Max Heap 7 min 70 40 max 30 9 10 15 min 45 50 30 20 12 j max

  6. Min-Max Heap After Inserting Key 5 5 min 70 40 max 30 9 7 15 min 10 45 50 30 20 12 max

  7. Min-Max Heap After Inserting Key 80 7 min 70 80 max 30 9 10 15 min 40 45 50 30 20 12 max

  8. 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; } }

  9. 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) { // move h[gp] to h[i] h[i] = h[gp]; i = gp; } h[i] = x; // x is to be inserted into node i } O(logn)

  10. Deletion of the Min Element 12 min 70 40 max 30 9 10 15 min 45 50 30 20 max

  11. 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 min-max 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.

  12. Min-Max Heap After Deleting Min Element 9 min 70 40 max 30 10 15 12 min 45 50 30 20 max

  13. 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.

  14. 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.

  15. A Deap Example 5 45 10 8 25 40 20 15 19 9 30

  16. Deap (Cont.) • From Deap’s definition, it’s 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 . • Then j defined in property (4) of definition is given by if (j > n+1) j /= 2;

  17. Figure 9.7 Insertion Into A Deap 5 45 10 8 25 40 20 15 19 9 30 j i

  18. Figure 9.8: Deap Structure After Insertion of 4 4 45 5 8 25 40 20 15 10 9 30 19

  19. Figure 9.8: Deap Structure After Insertion of 30 5 45 10 8 30 40 20 15 19 9 30 25

  20. 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) { 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); } } O(log n)

  21. 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.

  22. Deap Structure After Deletion of Min Element 8 45 10 9 25 40 15 19 20 30

More Related