Min Max Heaps

download Min Max Heaps

of 22

Transcript of Min Max Heaps

  • 8/12/2019 Min Max Heaps

    1/22

    Min-Max Heaps

    A double-ended priority queue is a datastructure 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 aboveoperations.

  • 8/12/2019 Min Max Heaps

    2/22

    Min-Max Heap (Cont.)

    Definition: A min-max heap is a completebinary tree such that if it is not empty,each element has a data member called

    key. Alternating levels of this tree are minlevels and max levels, respectively. Theroot is on a min level. Let x be any node ina min-max heap. If x is on a min (max) levelthen the element in x has the minimum

    (maximum) key from among all elements inthe subtree with root x. A node on a min(max) level is called a min (max) node.

  • 8/12/2019 Min Max Heaps

    3/22

    Figure 9.1: A 12-elementMin-Max Heap

    7

    30

    45 50

    9

    30 20

    70

    10

    12

    15

    40

    min

    min

    max

    max

  • 8/12/2019 Min Max Heaps

    4/22

    Min-Max Heap (Cont.)

    The min-max heap stores in a one-dimension arrayh.

    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 smallerthan all keys in nodes that are both on max levelsand on the path from j to root. Only need tocheck nodes on min levels.

    When inserting 80 into this min-max heap, since80 > 10, and 10 is on the min level, we are assuredthat 80 is larger than all keys in the nodes thatare both on min levels and on the path from j to

    the root. Only need to check nodes on max levels.

  • 8/12/2019 Min Max Heaps

    5/22

    Insert to Min-Max Heap

    7

    30

    45 50

    9

    30 20

    70

    10

    12

    15

    40

    min

    min

    max

    maxj

  • 8/12/2019 Min Max Heaps

    6/22

    Min-Max Heap AfterInserting Key 5

    5

    30

    45 50

    9

    30 20

    70

    7

    12

    15

    40

    min

    min

    max

    max10

  • 8/12/2019 Min Max Heaps

    7/22

    Min-Max Heap AfterInserting Key 80

    7

    30

    45 50

    9

    30 20

    70

    10

    12

    15

    80

    min

    min

    max

    max40

  • 8/12/2019 Min Max Heaps

    8/22

    Program 9.3 Insertion IntoA Min-Max Heap

    templ tevoidMinMaxHeap::Insert(const Element& x)// inset x into the min-max heap

    { if(n==MaxSize) {MinMaxFull(); return;}n++;

    intp =n/2; // p is the parent of the new nodeif(!p) {h[1] = x; return;} // insert into an empty heapswitch(level(p)) {c seMIN:if(x.key < h[p].key) { // follow min levelsh[n] = h[p];VerifyMin(p, x);

    }else{ VerifyMax(n, x); } // follow max levelsre k;c seMAX:if(x.key > h[p].key) { // follow max levelsh[n] = h[p];VerifyMax(p, x);

    }else{ VerifyMin(n, x); } // follow min levelsre k;}

    }

  • 8/12/2019 Min Max Heaps

    9/22

    Program 9.4 Searching For The CorrectMax Node For Insertion

    template

    void MinMaxHeap::VerifyMax(int i, const Elemetn& 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)

  • 8/12/2019 Min Max Heaps

    10/22

    Deletion of the MinElement

    30

    45 50

    9

    30 20

    70

    10 15

    40

    min

    min

    max

    max

    12

  • 8/12/2019 Min Max Heaps

    11/22

    Deletion of the MinElement (Cont.)

    When delete the smallest key from the min-max heap, theroot 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 intothe root.

    The root has at least one child. Now the smallest key in themin-max heap is in one of the children or grandchildren of theroot. Assume node k has the smallest key, then followingconditions 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 hasno 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 theroot. Let p the parent of k. If x.key > h[p].key, then h[p] and x areto be interchanged.

  • 8/12/2019 Min Max Heaps

    12/22

    Min-Max Heap AfterDeleting Min Element

    9

    30

    45 50 30 20

    70

    10 15

    40

    min

    min

    max

    max

    12

  • 8/12/2019 Min Max Heaps

    13/22

    Deaps

    A deap is a double-ended heap thatsupports the double-ended priority

    operations of insert, delet-min, anddelete-max. Similar to min-max heap but deap is

    faster on these operations by a

    constant factor, and the algorithmsare simpler.

  • 8/12/2019 Min Max Heaps

    14/22

    Deaps (Cont.)

    Definition: A deap is a complete binary tree thatis either empty or satisfies the followingproperties:

    (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. Ifsuch a j does not exist, then let j be the nodein the right subtree that corresponds to theparent of i. The key in node i is less than orequal to that of j.

  • 8/12/2019 Min Max Heaps

    15/22

    A Deap Example

    10

    15 19

    8

    9 30

    5

    25 40

    45

    20

  • 8/12/2019 Min Max Heaps

    16/22

    Deap (Cont.)

    From Deaps definition, its obvious that for an n-elementdeap, the min element is the root of min heap and the maxelement 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 tomin, 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 correspondingnode in the max heap is .

    Then j defined in property (4) of definition is given by

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

    1log22

    i

    i

    ;2 1log2

    i

    ij

  • 8/12/2019 Min Max Heaps

    17/22

    Figure 9.7 Insertion Into ADeap

    10

    15 19

    8

    9 30

    5

    25 40

    45

    20 j

    i

  • 8/12/2019 Min Max Heaps

    18/22

    Figure 9.8: Deap StructureAfter Insertion of 4

    5

    15 10

    8

    9 30

    4

    25 40

    45

    20 19

  • 8/12/2019 Min Max Heaps

    19/22

    Figure 9.8: Deap StructureAfter Insertion of 30

    10

    15 19

    8

    9 30

    5

    30 40

    45

    20 25

  • 8/12/2019 Min Max Heaps

    20/22

    Program 9.7: Inserting IntoA Deap

    templ tevoidDeap::Insert(const Element& x) {//Insert x into the deapintI;if(n==MaxSize) {DeapFull(); return;}

    n++;if(n==1) {d[2]=x; return;} // insert into an empty deapintp = n+1; // p is the new last position of the deapswitch(MaxHead(p)) {c seTRUE: // p is a position in the max heapi = MinPartner(p);if(x.key < d[i].key) {

    d[p] = d[i];MinInsert(i, x);

    }elseMaxInsert(p, x);re k;c seFALSE: // p is a position in the min heapi = MaxPartner(p);if(x.key > d[i].key) {

    d[p] = d[i];MaxInsert(i, x);

    }elseMinInsert(p, x);}}

    O(log n)

  • 8/12/2019 Min Max Heaps

    21/22

    Deletion of Min Element

    Suppose we want to remove the minimum element from thedeap. 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 maxpartner. If , exchange them.

  • 8/12/2019 Min Max Heaps

    22/22

    Deap Structure AfterDeletion of Min Element

    10

    15 19

    9

    20 30

    8

    25 40

    45