[WPI] [cs2223] [cs2223 text] [News] [Syllabus] [Classes]
A heap is a binary tree with three key properties.
Here is a diagram of a heap.
The first heap property means than each node can have one leaf, two leaves, or two branches, but not one branch or one branch and one leaf. In English, the first two conditions mean that all leaves are confined to the left side of the bottom level - level 0 - of the tree or the right side of the next level up - level 1. If we add nodes to a heap, we fill from left to right in the bottom level of the tree.
The third property of the heap allows for duplicate values, but children can never have greater values than their parents.
Unlike the binary search tree, in the heap, the value of the left child can by greater than, less than, or equal to the value of the right child. Number the nodes in rows.
Note that the numbering is 1->12 not 0->11. Then it is easy to match parents and children using the indices of the notes.
parent(node (i)) = node(i/2)
left_child(node(i)) = node(2i)
right_child(node(i)) = node(2i+1)
These relationships assume integer mathematics.
Note also that in a heap with n nodes, n/2 is the last node which has children.
The heap conditions make it easy to store heaps in arrays. If a heap has been stored as a tree using linked lists or other technologies, it can be stured in an array using a queueing process. This same process is also used for printing trees.
Begin at the root, node n = 1, and recursively:
Put the index of the n-th node's left child (if there is one) into the queue
Put the index of the n-th node's right child (if there is one) into the queue
Print the value of n-th node, or print it
Dequeue the top value in the queue and call it n. Stop if the queue is empty.
We discussed operations performed on heaps. Note that most of them use the heap properties to simplify the algorithms.
Suppose a value in a node is increased. The change can have no effect on the node's children but it can destroy the heap relationship with the node's parent. Recursively swap the node's value with it's parent's until the heap relationship is restored. The upward recursion stops whenever the parent's value is not smaller at the tree's root (n/2 < 1).
Becuase the height of the tree is the floor (lowest integral value) of lgn, percolation is of order O(lgn).
Suppose a value in a node is decreased. The change can have no effect on the node's parent but it can destroy the heap relationship with the node's children. Recursively swap the node's value with the largest of it's children until the heap relationship is restored. The downward recursion stops whenever neither child's value is smaller or whenever the bottom of the tree is reached (2n > size or 2n+1 > size).
Becuase the height of the tree is the floor (lowest integral value) of lgn, sifting is of order O(lgn).
To insert a value into a heap, put it into the first open space in the heap, which is at the end of the array (n = size+1). Since this value could be greater than it's parent, use percolation to restore the heap propery. The actual insertion step is of order O(1) so the order of the insertion algorithm is the same as the order of percolation, O(lgn). Filling a heap by sequentially inserting values is of order O(nlgn).
To deleta a value from a heap, swap it with the last value in the heap (n = size+1) and reduce the heap size by one. Since the swap almost certainly destroys the heap property, restore it using sifting or percolation, depending on the value swapped into the node. Since performing both percolation and sifting at the node costs only one more comparison than doing only the "correct" operation, it is usuall to write the deletion algorithm so it does both after the swap. The order of deletion is the same as the order of percolation and sifting, O(lgn). Don't forget the special case when the last element is the one being deleted!
The easiest way to turn an array into a heap is just to restore the heap conditions in the array. This can be done by starting at the bottom and sequentially sifting each element in the array. Sifting is not done at leaves so the process can begin at the element n/2, where n is the array size. Restoration is of order is of order O(nlgn).
Heaps are easy to build and maintain, but itis hard to find specific values in the heap. For example, the maximum value is always at the root, but the minimum value can be in any leaf, which is a problem because a heap of size n has about n/2 leaves! The heapsort algorithm rearranges the heap arary to put the values in increasing sequential order - the smallest is in the first place. This heapsort algorithm is used recursively.
Swap the root, the largest value in the array, with the last (smaller) element in the array.
Freeze the largest value in it's new place at the end of the array (don't allow it to change)
Sift the heap beginning at the root to restore the heap properties. Ignore frozen values
Stop when only two values are unfrozen.
To make the array back into a heap, just sort it again. This works because the second sort results in an array with decreasing values and such an array is always a heap (because decreasing orders assures that each node has a greater value than any of its children, which come later in the array). The heapsort algorithm is of order O(nlgn).
[cs2223 text] [News] [Syllabus] [Classes] |