Heaps © Dave Bockus
Total Order A Proper Mathematical Definition If a and b are members of a totally ordered set, we may write a < b if a <= b and a b. The binary relation < is then transitive (a < b and b < c implies a < c) and trichotomous (one and only one of a < b, b < a and a = b is true). In fact, we can define a total order to be a transitive trichotomous binary relation <, and then define a <= b to mean a < b or a = b http://www.wikipedia.com/wiki/Total_order
My simplified definition Def: Total Order, Given a sequence k1…kn it has the property of total order if it satisfies: a) Trichotomy - for any ki, kj exactly one of the following is true: i) ki < kj ii) ki = kj iii) ki > kj b) Transitivity - If (ki < kj and kj < kl) then ki < kl. Where ki <= kj is defined as ki < kj or ki = kj.
Creating a Heap from a CBT 1 7 1 7 2 4 2 4 6 1 1 5 7 7 5 6 3 2 4 1 6 7 5
Sift-Up Aliases The Idea Sift-down Percolate up Percolate down Assume all nodes in tree have the heap property w.r.t. there children, except for current node N. Compare N to TL and TR Swap N and root of TL or TR, which ever is greater. Recurse down until N is greater then TL and TR
Sift-Up Cont... if (k < max (kL, kR)) if (kL kR) swap(kL,k) else swap(kR,k) k kl kr TL TR Once an exchange takes place the subtree must be checked for the heap property. so we recurse down the tree.
Sift-Up Cont... Given T pointing to the root of a tree (subtree) where k is the key, & subtrees TL & TR with keys kL & kR which are heaps. N = T 1) k = key(N); TL = Lt_subtree; TR = Rt_subtree; kL = key(TL); kR = key(TR) 2) if (k kL && k kR) exit 3) if (kL > kR) exchange keys of N and TL N = TL else exchange keys of N and TR N = TR goto 2
Heap Sort Algorithm 1) Take the keys k1…kn & put them in a CBT 2) Use siftup to turn the CBT into a heap 3) Repeat the following until heap is empty a) Remove the root & put in an o/p queue b) Remove right most leaf at deepest level & put its' key on the root. c) Apply siftup to make the CBT into a heap again
Heap Sort Example CBT with 6 integers 3 9 8 1 4 2 CBT to Heap 3 9 8 2 3 9 8 1 4 2 3 9 8 2 4 1 9 4 8 2 3 1 CBT to Heap
Heap Sort Example Cont…. 1 9 4 8 2 3 1 1 4 3 1 3 3 2 2 8 2 Output Queue 9 8 4 3 2 1
Heap Sort Animations Heap Sort Animation
Heaps as Linked Structures Peaps Author: Paul Picazo (ppicazo@gmail.com) & Dr. Paul Hriljac Date Submitted: 4/30/2008 https://www.cpp.edu/~ftang/courses/CS241/notes/Building_Heaps_With_Pointers.pdf
General Implementation Efficiency Insert and Remove will run in 2lgN time. Slight increase over array, but negligible. Explanation to follow
Locating Nodes in a Linked Heap A relationship exists between the node numbering in a CBT and the position in a tree.
Node Position Using standard heap naming conventions, one can see that all even nodes are left children and odd right children. Observe that the total number of nodes in a tree where each level is full is expressed as 2n+1-1 Each level has exactly 2n nodes. As one descends the tree from the root, each level incrementally adds another 2n nodes. If we have 15 nodes it implies: 20 + 21 + 22 +23.
Node Position
Basic DFS Consider a root node with full Right and Left subtrees, where the total number of nodes is T. With n levels. Thus, T = 2n -1 = 1 + 2n-1 + 2n-1 As we descend the tree we eliminate half of the tree on each level. A node then numbered using heap conventions will be located from the root based on its position within the tree.
Example Consider node 14. In Binary 1110 Using the convention left=0, right=1. Which follows from the basic definition of a binary tree. Eliminate root so we are left with 110. Descending the tree then will locate the target node.
Insert We know current number of nodes in the tree. Add 1 node to next sequential number. Start at root descend the tree to insert location. Sift-up using path back up the tree. 2lgN (down then up)
Remove Remove root. Find last node in CBT, (lg n) Move node to root Sift-down (lg n)
Heapify a random tree Post Order recursive traversal. On visit, apply heap exchange on current sub-tree. O(n). Allows Heapsort to then run in 2(NlgN) mainly due to the overhead of finding the last node in the CBT.
End