Chapter 9
Heap Structures
Ji-Hoon Kang
jhkang@cs.chungnam.ac.kr
Dept. of Computer Science
Chungnam National University
Double-ended priority queue
1. Insert an element with abitrary key.
2. Delete an element with the largest key.
3. Delete an element with the smallest key.
Ji-Hoon Kang Data Structure II (2003 Spring) 9-2
MIN-MAX HEAPS
A Complete binary tree such that if it is not empty, each element
has a field called key.
Alternating levels of this tree are min levels and max levels,
respectively.
The root is on an min level.
Let x be a node in a min-max heap.
If x is on a min level then the element in x has the minimum key from
among all elements in the subtree with root x.
We call this node a min node.
Similarly, if x is on a max level, then the element in x has the maximum key
from among all elements in the subtree with root x.
We call this node a max node.
Ji-Hoon Kang Data Structure II (2003 Spring) 9-3
MIN-MAX HEAPS
7
min
70 40 max
30 9 10 15 min
45 50 30 20 12 max
Ji-Hoon Kang Data Structure II (2003 Spring) 9-4
Insetion
i) Key=5
min
7
70 40 max
30 9 10 15 min
45 50 30 20 12 5 max
5 Min
70 40 max
30 9 7 15 min
45 50 30 20 12 10 max
Ji-Hoon Kang Data Structure II (2003 Spring) 9-5
Insetion
ii) Key=80
min
7
70 40 max
30 9 10 15 min
45 50 30 20 12 80 max
7 min
70 80 max
30 9 10 15 min
45 50 30 20 12 40 max
Analysis of Insert : O(log n)
Ji-Hoon Kang Data Structure II (2003 Spring) 9-6
Deletion of MIN
The root has no children.
(ie. Empty after deletion )
The root has at least one child.
The smallest key is in one of the children or grand-children of the root.
Let this be node k.
(a) item.key ≤ heap[k].key
There is no element in heap with key smaller than
item.key.
10
70 40 item.key = 10
k=7
55 56 30 20
Ji-Hoon Kang Data Structure II (2003 Spring) 9-7
Deletion of MIN
(b) item.key > heap[k].key and k is a child of the root.
Then k is a max node.
So, k has no descendants with key larger than heap[k].key.
40
item.key = 40
70 30 k=3
55 56 30 30
In this case, all the descendant of k have the key 30.
40 30
70 30 70 40
55 56 55 56
k has no child.
Ji-Hoon Kang Data Structure II (2003 Spring) 9-8
Deletion of MIN
(c) item.key > heap[k].key and k is a grandchild of the root.
Let p be the parent of k. (ie p = [ k/2 ] )
i. item.key ≤ heap [p].key
12
70 40 item.key = 12
k=5
30 9 10 15 p=2
45 50 30 20
9
70 40 item.key = 12
k = 11
30 12 10 15 Now, repeat the process
45 50 30 20
Ji-Hoon Kang Data Structure II (2003 Spring) 9-9
Deletion of MIN
ii. item.key > heap[p].key
80
70 40 item.key = 80
k=5
30 9 10 15 p=2
45 50 30 20
9
80 40 item.key = 70
k = 11
30 70 10 15 Now, repeat the process
45 50 30 20
Analysis of Delete MIN : O(log n).
Ji-Hoon Kang Data Structure II (2003 Spring) 9 - 10
Deaps
Double-ended heap
supports the double ended priority queue operations:
insert
delete min
delete max
O(log(n)) for each operation
n is the size of a deap.
Ji-Hoon Kang Data Structure II (2003 Spring) 9 - 11
Definition of DEAPs
A deap is a complete binary tree that is either empty or satisfies
the following properties:
1. The root contains no element.
2. The left subtree is a min-heap.
3. The right subtree is a max-heap.
4. If the right subtree is not empty, then let i be any node in the left subtree.
Let j be the corresponding node in the right subtree.
If such a j does not exist, then let j be the node in the right subtree that
corresponds to the parent of i.
The key in node i is less than or equal to the key in j.
overall overall
minimum maximum
5 45
10 8 25 40
15 19 9 30 20
Ji-Hoon Kang Data Structure II (2003 Spring) 9 - 12
DEAPs
How to compute j :
Level leftmost # of nodes
node # at the level j = i + 2 k-2
1 20 21-1 2 k-1 ≤ i n ) j /= 2;
The number of
the last node
Ji-Hoon Kang Data Structure II (2003 Spring) 9 - 13
Insertion to A Deap
1. On a max side
5 45 5 45
10 8 25 40 10 8 25 40
15 19 9 30 20 4 15 19 9 30 20 30
i=9 j = 13
4 45 5 45
5 8 25 40 10 8 30 40
15 10 9 30 20 19 15 19 9 30 20 25
Ji-Hoon Kang Data Structure II (2003 Spring) 9 - 14
Insertion to A Deap
2. On a MIN side.
5 45
10 8 25 40
15 19 3
i = ( n + 2 [ log2 n ] - 1 ) / 2
n = 10 = ( 10 + 23-1 ) / 2
=7
3 45
10 5 25 40
15 10 8
Ji-Hoon Kang Data Structure II (2003 Spring) 9 - 15
Insertion to A Deap
5 45
10 8 25 40
15 50 i = ( 9 - 23-1 ) / 2 = 6
n=9
5 50
10 8 45 40
12 25
Analysis of Insert : O(log n)
Ji-Hoon Kang Data Structure II (2003 Spring) 9 - 16
Deletion of Min
5 65
10 8 45 35
15 19 9 30 40
8 65
10 9 45 35
15 19 40 30
Ji-Hoon Kang Data Structure II (2003 Spring) 9 - 17
Deletion of Min
Modified Insert
8 65
10 9 45 40
15 19 35 30
Analysis of Delete MIN : O(log n)
Delete MAX
It is performed in a similar manner.
Ji-Hoon Kang Data Structure II (2003 Spring) 9 - 18