Download presentation
Presentation is loading. Please wait.
1
Heaps © Dave Bockus
2
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
3
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.
4
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
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
6
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.
7
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
8
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
9
Heap Sort Example CBT with 6 integers 3 9 8 1 4 2 CBT to Heap 3 9 8 2
3 9 8 2 4 1 9 4 8 2 3 1 CBT to Heap
10
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
11
Heap Sort Animations Heap Sort Animation
12
Heaps as Linked Structures
Peaps Author: Paul Picazo & Dr. Paul Hriljac Date Submitted: 4/30/2008
13
General Implementation Efficiency
Insert and Remove will run in 2lgN time. Slight increase over array, but negligible. Explanation to follow
14
Locating Nodes in a Linked Heap
A relationship exists between the node numbering in a CBT and the position in a tree.
15
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:
16
Node Position
17
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.
18
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.
19
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)
20
Remove Remove root. Find last node in CBT, (lg n) Move node to root
Sift-down (lg n)
21
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.
22
End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.