Priority Queues: Binary Heaps
Priority Queues: Binary Heaps
PRIORITY QUEUES
HEAPS
FULL BINARY TREE
Every non-leaf node has two children
All the leaves are on the same level
root 0
3 2
4 5
9
PROPERTIES OF BINARY HEAP
13
BASIC HEAP OPERATIONS: INSERT(X)
Maintain the complete binary tree property and fix any problem with
the partially ordered tree property
Create a leaf at the next available position
Repeat
Locate parent
if heap order property not satisfied
Swap with parent
else
Stop
Insert x into its final location
14
PERCOLATE UP – INSERT A
NODE
A hole is created at the bottom of the tree, in the
next available position.
10
12
15 17 18 23
20 19 34
15
PERCOLATE UP
Insert 20
6
10
12
15 17 18 23
21 19 34 20
16
PERCOLATE UP
Insert 16
6
10
12
15 18 23
21 19 34 17
17
PERCOLATE UP
6
10
12
15 16 18 23
21 19 34 17
14
14
14
19
BASIC HEAP OPERATIONS: DELETEMIN()
Replace root with the last leaf ( last element in the array
representation
This maintains the complete binary tree property but
may violate the partially ordered tree property
Repeat
Find the smaller child of the “hole”
If Partially Ordered Tree not satisfied
Swap hole and smaller child
else
Stop
20
DELETE A NODE -
PERCOLATE DOWN –
Delete minimum i-e 6
6
10
12
15 16 18 23
21 19 34 17
21
PERCOLATE DOWN
Last element - 17. The hole at the root.
10
16
15 16 18 23
21 19 34 17
16
15 16 18 23
21 19 34
23
PERCOLATE DOWN
10
15
17 16
16 18 23
21 19 34
24
PERCOLATE DOWN
10
15
16
17 16 18 23
21 19 34
13 14 16 19 21 19 68 65 26 32 31
26
DELETEMIN() EXAMPLE (CONT’D)
31
31
31
27
OTHER HEAP OPERATIONS
1. DecreaseKey(p,d)
increase the priority of element p in
the heap with a positive value d.
percolate up.
2. IncreaseKey(p,d)
decrease the priority of element p in
the heap with a positive value d.
percolate down.
28
OTHER HEAP OPERATIONS
3. Remove(p)
a. Assigning the highest priority to p - percolate p
up to the root.
b. Deleting the element in the root and filling the
hole by percolating down and trying to insert the
last element in the queue.
4. BuildHeap
input N elements
place them into an empty heap through successive
inserts. The worst case running time is O(NlogN).
29
BUILD HEAP - O(N)
Given an array of elements to be
inserted in the heap,
150
80 40
70 110
30 10
31
EXAMPLE (CONT)
After processing Level 2
150
80 40
Level 2 20 10
50 110
32
EXAMPLE (CONT)
After processing Level 1
150
Level 1 10 40
50 110
20 60
20 40
50 110
30 60
34
TIME COMPLEXITY - HEAP
Find max/min: O(1)
8 19
12 27 20
15 25 43
node 4 8 19 12 15 25 27 20 43
npl 1 0 1 1 0 0 0 0 0
Example: Null Path Length
4
8 0 19 1
What is the npl of the
right child of 8?
12 1 27 0 20 0
node 4 8 19 12 15 25 27 20 43
npl 1 0 1 1 0 0 0 0 0
Example: Null Path Length
•node 4 violates leftist heap property
4 fix by swapping children
8 0 19 1
12 1 27 0 20 0
15 0 25 0 43 0
node 4 8 19 12 15 25 27 20 43
npl 1 0 1 1 0 0 0 0 0
Leftist Heap
4
19 1 8 0
27 0 20 0 12 1
43 0 15 0 25 0
node 4 8 19 12 15 25 27 20 43
npl 1 0 1 1 0 0 0 0 0
MERGING IN LEFTIST HEAP
merge(h1,h2):
Input: two leftist heaps h1 and h2
Output: leftist heaps resulting from the merge of h1 and h2
Algorithm:
// make sure that the root of h1 is ot larger than the root of h2
if h1.key > h2.key then swap(h1,h2)
// now, root of h1 is the root of the merged heap; merge the rest of h1 with h2
if h1.right=nil then
h1.right <-- h2
else
h1.right <-- merge( h1.right, h2 )
// make sure the leftist property is preserved: npl of left >= npl of right
if h1.left.npl < h1.right.npl then
swap( h1.left, h1.right )
// adjust npl of the root - since npl of right child is never greater than left, it is 1+npl of right
child
h1.npl <-- h1.right.npl + 1
return h1
Merging Leftist Heaps
Consider two leftist heaps …
4 6
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
6
4
27 20 12 14
43 15 25
6
4
27 20 12 14
43 15 25
7
6
4
19 8 8 7
y
27 20 12 14
null
43 15 25
7
6
4
19 8 8 7
27 20 12 14
43 15 25
8
7
6
4
19 Refers to node 8 8 7 x
27 20 14 8
43 12
8
15 25
7
6
4
19 Refers to node 8 8 7
27 20 14 8
43 12
8
15 25
7
6
4
19 Refers to node 8 8 7
27 20 14 8
43 12
15 25
7
6
4
Return node 7
Merging Leftist Heaps
4 6
19 Refers to node 8 8 7
27 20 14 8
43 12
15 25
7
6
4
19 Refers to node 8 8 7
27 20 14 8
43 12
15 25
7
6
4
19 Refers to node 8 8 7
27 20 14 8
43 12
15 25
6
4
Return node 6
Merging Leftist Heaps
4
19 6
27 20 8 7
43 14 8
12
15 25
6
4
19 6
27 20 8 7
43 14 8
12
15 25
6
4
19 6
27 20 8 7
43 14 8
12
15 25
4 6
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
6
4
27 20 12 14
43 15 25
6
4
27 20 12 14
43 15 25
7
6
4
19 8 8 7
y
27 20 12 14
null
43 15 25
7
6
4
19 8 8 7
27 20 12 14
43 15 25
8
7
6
4
19 Refers to node 8 8 7 x
27 20 14 8
43 12
8
15 25
7
6
4
19 Refers to node 8 8 7
27 20 14 8
43 12
8
15 25
7
6
4
19 Refers to node 8 8 7
27 20 14 8
43 12
15 25
7
6
4
Return node 7
Merging Skew Heaps
4 6
19 Refers to node 8 8 7
27 20 14 8
43 12
15 25
7
6
4
19 Refers to node 8 7 8
27 20 8 14
43 12
15 25
7
6
4
19 Refers to node 8 7 8
27 20 8 14
43 12
15 25
6
4
Return node 6
Merging Skew Heaps
4
19 6
27 20 7 8
8 14
43
12
15 25
6
4
7 8 27 20
8 14 43
12
15 25
6
4
7 8 27 20
8 14 43
12
15 25