Heaps
Heaps
1
RECALL QUEUES
FIFO: First-In, First-Out
2
QUEUES THAT ALLOW LINE JUMPING
Need a new ADT
Operations: Insert an Item,
Remove the “Best” Item
6 2
15 23
insert deleteMin
12 18
45 3 7
3
PRIORITY QUEUE ADT
1. PQueue data : collection of data with priority
2. PQueue operations
insert
deleteMin
4
APPLICATIONS OF THE PRIORITY
QUEUE
Select print jobs in order of decreasing length
Forward packets on routers in order of urgency
Anything greedy
5
POTENTIAL IMPLEMENTATIONS
insert deleteMin
Unsorted list (Array) O(1) O(n)
Unsorted list (Linked-
O(1) O(n)
List)
Sorted list (Array) O(n) O(1)*
7
BINARY HEAP PROPERTIES
1. Structure Property
2. Ordering Property
8
BRIEF INTERLUDE: SOME DEFINITIONS:
A Perfect binary tree – A binary tree with all leaf
nodes at the same depth. All internal nodes
have 2 children.
height h
11 2h+1 – 1 nodes
2h – 1 non-leaves
5 21 2h leaves
2 9 16 25
1 3 7 10 13 19 22 30
9
HEAP STRUCTURE PROPERTY
A binary heap is a complete binary tree.
Complete binary tree – binary tree that
is completely filled, with the possible
exception of the bottom level, which is
filled left to right.
Examples:
10
REPRESENTING COMPLETE
BINARY TREES IN AN ARRAY
1 A From node i:
2 3
B C
4 5 6
F
7
G left child:
D E
8 9 10 11 12 right child:
H I J K L
parent:
10
10
20 80
20 80
40 60 85 99
30 15
50 700
not a heap
12
HEAP OPERATIONS
findMin:
insert(val): percolate up.
10
20 80
40 60 85 99
50 700 65
13
HEAP – INSERT(VAL)
Basic Idea:
1. Put val at “next” leaf position
2. Percolate up by repeatedly exchanging
node until no longer needed
14
INSERT: PERCOLATE UP
10
20 80
40 60 85 99
50 700 65 15
10
15 80
40 20 85 99
50 700 65 60
15
INSERT CODE (OPTIMIZED)
void insert(Object o) { int percolateUp(int hole,
Object val) {
assert(!isFull());
while (hole > 1 &&
size++; val < Heap[hole/2])
newPos = Heap[hole] = Heap[hole/2];
hole /= 2;
percolateUp(size,o); }
Heap[newPos] = o; return hole;
}
}
16
HEAP – DELETEMIN
Basic Idea:
1. Remove root (that is always the min!)
2. Put “last” leaf node at root
3. Find smallest child of node
4. Swap node with its smallest child if needed.
5. Repeat steps 3 & 4 until no swaps needed.
17
DELETEMIN: PERCOLATE DOWN
10
20 15
40 60 85 99
50 700 65
15
20 65
40 60 85 99
50 700 18
DELETEMIN CODE (OPTIMIZED)
Object deleteMin() { int percolateDown(int hole,
assert(!isEmpty()); Object val) {
while (2*hole <= size) {
returnVal = Heap[1];
left = 2*hole;
size--; right = left + 1;
newPos = if (right ≤ size &&
percolateDown(1, Heap[right] < Heap[left])
target = right;
Heap[size+1]); else
Heap[newPos] = target = left;
Heap[size + 1];
if (Heap[target] < val) {
return returnVal;
Heap[hole] = Heap[target];
} hole = target;
}
else
break;
19
}
return hole;
}
BUILDING A HEAP
12 5 11 3 10 6 9 4 8 1 7 2
20
BUILDING A HEAP
Adding the items one at a time is O(n log n)
in the worst case
21
WORKING ON HEAPS
What are the two properties of a heap?
StructureProperty
Order Property
22
BUILDHEAP: FLOYD’S METHOD
12 5 11 3 10 6 9 4 8 1 7 2
12
5 11
3 10 6 9
23
4 8 1 7 2
BUILDHEAP PSEUDOCODE
runtime:
24
BUILDHEAP: FLOYD’S METHOD
12
5 11
3 10 6 9
4 8 1 7 2
25
BUILDHEAP:
12 FLOYD’S METHOD
5 11
3 10 2 9
4 8 1 7 6
26
BUILDHEAP:
12 FLOYD’S METHOD12
5 11 5 11
3 10 2 9 3 1 2 9
4 8 1 7 6 4 8 10 7 6
27
BUILDHEAP:
12 FLOYD’S METHOD12
5 11 5 11
3 10 2 9 3 1 2 9
4 8 1 7 6 4 8 10 7 6
12
5 2
3 1 6 9
28
4 8 10 7 11
BUILDHEAP:
12 FLOYD’S METHOD12
5 11 5 11
3 10 2 9 3 1 2 9
4 8 1 7 6 4 8 10 7 6
12 12
5 2 1 2
3 1 6 9 3 5 6 9
29
4 8 10 7 11 4 8 10 7 11
FINALLY…
3 2
4 5 6 9
12 8 10 7 11
30
PROBLEMS
Build Heap Using the following sequences
show the necessary steps 15, 7, 12, 28,
36, 1, 37, 13, 4, 25, 3, 9, 27, 2, 5, 16
31