Title: Heaps (Priority Queues)
1Heaps (Priority Queues) A heap is a data
structure which supports efficient implementation
of three operations insert, findMin, and
deleteMin. (Such a heap is a min-heap, to be
more precise.) Two obvious implementations are
based on an array (assuming the size n is known)
and on a linked list. The comparisons of their
relative time complexity are as follows
insert findMin
deleteMin Sorted array O(n)
O(1) O(1)
(from large (to move (find at end)
(delete the end) to small)
elements) Linked list O(1)
O(1) O(n) (append)
(a pointer to Min) (reset Min pointer)
2- A simpler but related problem Find the smallest
and the second smallest elements in an array (of
size n) - A straightforward method
- Find the smallest using n 1 comparisons swap
it to the end of the array, find the next
smallest in n 2 comparisons, for a total of 2n
3 comparisons. - A method that saves results of some earlier
comparisons - (1) Divide the array into two halves, find the
smallest elements of each half, call them min1
and min2, respectively - (2) Compare min1 and min2, to find the
smallest - (3) If min1 is the smallest, compare min2 with
the remaining elements of the first half to
determine the second smallest similarly for the
case if min2 is the smallest. - The number of comparisons is (n 1) (?n/2? 1)
(?3n/2? 2.
3The second method can be depicted in the
following figure
min
min1
min2
H1
H2
First half of array
Second half of array
In the figure, a link connects a smaller element
to a larger one going downward for example, min
? min1 and min ? min2, and min1 ? each element in
the first half of the array, similarly for min2.
This organization facilitates finding the
smallest and second smallest elements.
4- A binary tree data structure for Min-heaps
- A binary tree is called a left-complete binary
tree if - the tree is full at each level except possibly at
the maximum level (depth), where the tree is full
at level i means there are 2i nodes at that level
(recall the root is at level 0) and - at the trees maximum level h, if there are fewer
than 2h nodes then the missing nodes are on the
right side of the tree.
If there are n nodes in a left-complete binary
tree of depth h, then 2h ? n ? 2h1 1. Thus, h
? lg n lt (h 1). In particular, h O(lg n).
Missing on the right side
5A binary tree satisfies the (min-)heap-order
property if the value at each is less than or
equal to the value at each of the child nodes (if
exist). A binary min-heap is a left-complete
binary tree that also satisfies the heap-order
property.
13
Note that there is no definite order between
values in sibling nodes also, it is obvious that
the minimum value is at the root.
21
16
24
31
19
68
65
26
32
A binary min-heap
6- Two basic operations on binary min-heaps
- Suppose the value at a node is decreased which
causes violation to the heap-order property
(because the value is smaller than that of its
parents). The operation to restore the heap
property is named percolateUp (siftUp). - Suppose the value at a node is increased which
becomes larger than either (or both) of the
value of the childrens. The operation to
restore the heap order is named percolateDown
(siftDown). - Both operations can be completed in time
proportional to the tree height, thus, of time
complexity O(lg n) where n is the total number of
nodes. These operations are crucial in
supporting the heap operations insert and
deleteMin.
7The percolateUp (siftUp) operation
percolateUp
13
13
21
35
21
12
25
37
43
12
25
37
43
35
The heap order violated at node valued 12
12
percolateUp
21
13
Note that the time of each step (each level) is
constant, i.e., compare with parent and swap
25
37
43
35
8The percolateDown (siftDown) operation
percolateDown
43
21
21
35
35
43
Who is smaller ?
37
25
45
63
37
25
45
63
Who is smaller ?
The heap order violated at node valued 43
21
Each step of percolateDown finds the smaller of
the two children (if exist) then swap it with the
violating node
35
25
percolate-Down
37
45
63
43
9Implementation of the heap operations insert()
insert a node next to the last node, call
percolateUp from it, treating the new node as a
violation. deleteMin() delete the root node,
move the last node to the root, then call
percolateDown from the root. findMin() return
the roots value. Finally, a binary heap can be
implemented using an array storing the heap
elements from the root towards the leaves, level
by level and from left to right within each
level. The left-completeness property guarantees
that there are no holes in the array. Also, if
we use array indexes 1..n to store n elements,
the parents index (if exists) of node i is
?i/2?, the left childs index is 2i, the right
child 2i 1 (if exist). The time complexity of
deleteMin and insert both are O(lg n) the time
complexity of findMin is O(1).
10An array implementation of a binary heap
1
Number the nodes (starting at 1) by levels, from
top to bottom and left to right within level
2
3
4
5
6
Parent to children (index i to 2i, 2i1)
1 2 3 4 5 6
. . . . .
Level 2 nodes
Level 1 nodes
Root
11// BinaryHeap class // // CONSTRUCTION with
optional capacity (that defaults to 100) // //
PUBLIC OPERATIONS
// void insert(x) --gt Insert x //
Comparable deleteMin()--gt Return and remove
smallest item // Comparable findMin() --gt Return
smallest item // boolean isEmpty() --gt Return
true if empty else false // boolean isFull()
--gt Return true if full else false // void
makeEmpty() --gt Remove all items //
ERRORS
// Throws Overflow if capacity
exceeded / Implements a binary heap.
Note that all "matching" is based on the
compareTo method. _at_author Mark Allen Weiss
/
12public class BinaryHeap /
Construct the binary heap. / public
BinaryHeap( ) this(
DEFAULT_CAPACITY ) /
Construct the binary heap. _at_param capacity
the capacity of the binary heap. /
public BinaryHeap( int capacity )
currentSize 0 array new Comparable
capacity 1 private static final
int DEFAULT_CAPACITY 100 private int
currentSize // Number of elements in heap
private Comparable array // The heap array
13 / Insert into the priority queue,
maintaining heap order. Duplicates are
allowed. _at_param x the item to insert.
_at_exception Overflow if container is full.
/ public void insert( Comparable x ) throws
Overflow if( isFull( ) )
throw new Overflow( ) // Percolate
up int hole currentSize
for( hole gt 1 x.compareTo(arrayhole/2) lt 0
hole / 2) arrayhole arrayhole /
2 array hole x
14 / Find the smallest item in the
priority queue. _at_return the smallest item,
or null, if empty. / public Comparable
findMin( ) if( isEmpty( ) )
return null return array 1
/ Remove the smallest item from
the priority queue. _at_return the smallest
item, or null, if empty. / public
Comparable deleteMin( ) if(
isEmpty( ) return null
Comparable minItem findMin( ) array 1
array currentSize--
percolateDown( 1 ) return minItem
15 / Internal method to percolate down
in the heap. _at_param hole the index at
which the percolate begins. / private
void percolateDown(int hole) int
child Comparable tmp arrayhole for( hole
2 lt currentSize hole child)
child hole 2 if(child ! currentSize
arraychild1.compareTo(arraychild) lt
0) child if( arraychild.compareTo(tmp
) lt 0 ) arrayhole arraychild
else break arrayhole tmp
16Convert an array T1..n into a heap (known as
heapify) A top-down method repeatedly call
insert() by inserting Ti into a heap T1..(i
1), for i 2 to n.
21
12
9
9
7
12
9
21
9
21
12
13
12
9
12
13
7
13
7
13
7
21
7
21
13
Initially, T1 by itself ia a heap
Insert 12 into T1..1, making T1..2 a heap
Insert 9 into T1..2, making T1..3 a heap
Insert 13 into T1..3, making T1..4 a heap
Insert 7 into T1..4, making T1..5 a heap
The total time complexity of top-down heapify is
O(n lg n).
17A bottom-up heapify method
21
For i ?n/2? down to 1 / percolate down
Ti, making the subtree rooted at i a heap
/ call percolateDown(i)
12
9
13
7
n 5, start at node ?n/2?, the node of the
highest index that has any children
Complexity analysis Node1 travels down h levels
during percolateDown, nodes 2 and 3 each (h 1)
levels, nodes 4 through 7 each (h 2) levels,
etc. The total number of levels traveled is ? h
2(h 1) 4 (h 2) 2h 1(h (h 1))
2h 1(1 2(1/2) 3(1/2)2 4(1/2)3 ) ? 2n,
because 2h 1 lt n/2 since h ? lg n, and the
infinite series 1 2x 3x2 1/(1 x)2 when
x lt 1. Thus, the time complexity of heapify is
O(n).