Lecture 8: Heaps, Heap-Sort


Heaps

  1. Heap-Order Property
  2. Complete Binary Tree Property

Heaps (cont.)



Height of a Heap

.


Height of a Heap (cont.)


Implementation of a Heap

     
    public class HeapSimplePriorityQueue implements SimplePriorityQueue {
        BinaryTree T;
        Position last;
        Comparator comparator;
        …
    }


Implementation of a Heap (cont.)



Finding the Insertion Position

    Algorithm findInsertionPosition(w)
    Input: Last position w
    Output: Insertion position z if root is external
        z ¬root
    else
        u ¬last = w
        while (u is neither root nor a left child)
            u ¬ parent of u
        if u is a left child
            u ¬ sibling of u
       while u is not external
            u ¬ left child of u
        z ¬u
    return z

Up-Heap Bubble

    Algorithm upHeapBubble(z)
        z.setElement(key-element pair to be inserted)
        done¬ false
        while not done
            if z = root
                done ¬ true
            else
                u ¬ parent of z
            if key(z) < key(u)
                swap key-element pairs between u and z
            else
                done ¬ true
            z ¬ u  

Insertion into a Heap

     
     
     

Insertion into a Heap (2)

     
     

Insertion into a Heap (3)

     
     

Insertion into a Heap (4)

     

 Lafore Heap Applet


Down-Heap Bubbling

    Algorithm downHeapBubble r ¬ root
    done ¬ false
    while not done
        if both children of r are external
            done¬ true
        else if right child of r is external
            s ¬ left child of r
        else
            s ¬ child of r with minimum key
        if key(s) < key(r)
            swap key-element pairs between r and s
            r¬s
        else
            done¬ true

    Reverse Algorithm of findInsertionPosition
      Algorithm findLastPosition(w)
    Input w: old last position, about to become insertion position
    Output last: new last position removeAboveExternal(right child of w)
    if w = root
        return w
    while w is neither root nor a right child
        w ¬ parent of w
    if w is a right child
        w ¬ sibling of w
    while right child of w is not external
        w ¬ right child of w
    return w  

Removal from a Heap

     
     

Removal from a Heap (2)

     


Removal from a Heap (3)



Removal from a Heap (4)


Class HeapSimplePriorityQueue

Summary of Costs, Heap-Based Priority Queue

     
    Method Unsorted Sequence Sorted Sequence Heap
    size, isEmpty O(1) O(1) O(1)
    minElement, minKey Q(n) O(1) O(1)
    insertItem O(1) O(n) O(log n)
    removeMinElement Q(n) O(1) O(log n)


Height of heap = h = O(log n)

upHeapBubble Î O(h) = O(log n)

downHeapBubble ÎO(h) = O(log n)

findInsertionPosition ÎO(2h) = O(2log n) = O(logn)

findLastPosition ÎO(2h) = O(2log n) = O(log n)


Heap Sort



Bottom-Up Heap Construction
NOTE:  The splitting of sequences is merely a conceptual construct to help explain the recursion.  In practice, the elements of the sequence are simply inserted in order.


Bottom-Up Heap Construction Example

Original sequence in the example (n=15)

14,9,25,16,15,5,4,12,8,11,6,7,27,23,20

Remove first key

14,{9,25,16,15,5,4,12,8,11,6,7,27,23,20}

Enter next level in recursion (n=7)

14,{9,[25,16,15,5,4,12],8,[11,6,7,27,23,20]}

Enter next level in recursion (n=3)

14,{9,[25,(16,15),5,(4,12)],8,[11,(6,7),27,(23,20)]}

Trivial binary trees, n=1

16 15 4 12 6 7 23 20


Bottom-Up Heap Construction


Bottom-Up Heap Construction (cont.)


Bottom-Up Heap Construction (cont.)


Bottom-Up Heap Construction (cont.)


Analysis of Bottom-Up Heap Construction

The cost of insertion at height h is O(h) in the worst-case because the cost is dominated by the swapping operations in down-heap bubbling.

One worst-case realization is when each node when inserted performs down-heap bubbling from its inorder successor node. (Right, then repeat left until an external node is reached).
 


Analysis of Bottom-Up Heap Construction (cont).

Every edge in the tree is used at most once for an inorder successor down-heap bubble. This assures that the number of swaps is bounded above by the number of edges.

Recall that for a tree, n represents the total number of nodes, i.e. n = ne + ni

The number of edges is e = ne + ni –1
 

But since in a heap, n equals the number of key-element pairs, i.e.,

n = ni   and   ne = ni + 1,

we therefore have

e = 2n,

and the complexity of bottom-up heap construction is thus

O(e) = O(2n) = O(n).


Analysis of Bottom-Up Heap Construction (cont.)


Sequence-based Implementation of a Binary Tree


 



Sequence-based Implementation of a Binary Tree (cont.)

Best case

Worst case

Summary





Implications for heaps:

1. Heap-order property implies that a sequence-based heap is relatively efficient space-wise (N=O(n)).
2. The algorithms findInsertionPosition and findLastPosition are O(1).