Title: Heaps,heapsort and priority queue
1Heaps,heapsort and priority queue
- Binhai Zhu
- Computer Science Department, Montana State
University -
2Definition
- A heap is a binary tree with the following
properties - (1) The value of each node is not less than
the values stored in each of its children. //heap
property - (2) The tree is perfectly balanced and leaves
in the last level are all in the leftmost
positions.
3Example
16
14
10
3
7
9
8
1
4
2
4Example
16
14
10
3
7
9
8
1
4
2
10
8
7
9
1
2
3
4
5
6
i
16
14
10
8
7
9
1
2
3
4
Ai
5Definition
A---array lengthA---number of elements in the
array heap-sizeA---number of elements in the
heap stored in the array A root of the
tree/heap---A1
Q. Given index i, how do we find its
parent/children?
10
8
7
9
1
2
3
4
5
6
i
16
14
10
8
7
9
1
2
3
4
Ai
6Example
Q. Given index i, how do we find its
parent/children?
16
14
10
3
7
9
8
1
4
2
10
8
7
9
1
2
3
4
5
6
i
16
14
10
8
7
9
1
2
3
4
Ai
7Example
Q. Given index i, how do we find its
parent/children?
parent(i)
16
14
10
3
7
9
8
1
4
2
10
8
7
9
1
2
3
4
5
6
i
16
14
10
8
7
9
1
2
3
4
Ai
8Example
Q. Given index i, how do we find its
parent/children?
parent(i) return i/2
16
14
10
3
7
9
8
1
4
2
10
8
7
9
1
2
3
4
5
6
i
16
14
10
8
7
9
1
2
3
4
Ai
9Example
Q. Given index i, how do we find its
parent/children?
parent(i) return i/2
16
left(i)
14
10
3
7
9
8
1
4
2
10
8
7
9
1
2
3
4
5
6
i
16
14
10
8
7
9
1
2
3
4
Ai
10Example
Q. Given index i, how do we find its
parent/children?
parent(i) return i/2
16
left(i) return 2i
14
10
3
7
9
8
1
4
2
10
8
7
9
1
2
3
4
5
6
i
16
14
10
8
7
9
1
2
3
4
Ai
11Example
Q. Given index i, how do we find its
parent/children?
parent(i) return i/2
16
left(i) return 2i
14
10
right(i) return 2i1
3
7
9
8
1
4
2
10
8
7
9
1
2
3
4
5
6
i
16
14
10
8
7
9
1
2
3
4
Ai
12Heap property
16
14
10
3
7
9
8
1
4
2
10
8
7
9
1
2
3
4
5
6
i
16
14
10
8
7
9
1
2
3
4
Ai
13Heap property Aparent(i)Ai
16
14
10
3
7
9
8
1
4
2
10
8
7
9
1
2
3
4
5
6
i
16
14
10
8
7
9
1
2
3
4
Ai
14The height of a node is the number of edges on
the longest simple downward path from the node to
a leaf.The height of a tree is the height of
its root.Take-home exercise An n-element heap
has height log n . Prove it!
16
14
10
3
7
9
8
1
4
2
15Now we cover a fundamental procedure regarding
heaps the heapify procedure.Condition binary
trees rooted at left(i) and right(i) are heaps,
but at Ai the heap property is violated i.e.,
Ai maybe smaller than its children.
16
4
10
3
7
9
14
8
1
2
16Now we cover a fundamental procedure regarding
heaps the heapify procedure.Condition binary
trees rooted at left(i) and right(i) are heaps,
but at Ai the heap property is violated i.e.,
Ai maybe smaller than its children.
16
X
4
10
3
7
9
14
8
1
2
17- Heapify(A,i)
- l ? left (i), r ? right(i)
- If l heap-sizeA and Al gt Ai
- then largest ? l
- else largest ? i //find the largest index
among Ai,Aleft(i),Aright(i) - If r heap-sizeA and Ar gt Alargest
- then largest ? r
- If largest ? i
- then exchange Ai ? Alargest
- Heapify(A,largest)
16
X
4
10
3
7
9
14
8
1
2
18- Heapify(A,i)
- l ? left (i), r ? right(i)
- If l heap-sizeA and Al gt Ai
- then largest ? l
- else largest ? i //find the largest index
among Ai,Aleft(i),Aright(i) - If r heap-sizeA and Ar gt Alargest
- then largest ? r
- If largest ? i
- then exchange Ai ? Alargest
- Heapify(A,largest)
16
14
10
X
3
7
9
4
8
1
2
19- Heapify(A,i)
- l ? left (i), r ? right(i)
- If l heap-sizeA and Al gt Ai
- then largest ? l
- else largest ? i //find the largest index
among Ai,Aleft(i),Aright(i) - If r heap-sizeA and Ar gt Alargest
- then largest ? r
- If largest ? i
- then exchange Ai ? Alargest
- Heapify(A,largest)
- Runnting time O(log n)
16
14
10
3
7
9
8
1
2
4
20We can use Heapify to convert an unorganized
array into a heap. Build-Heap(A) 1.heap-sizeA ?
lengthA 2.for i ? lengthA/2 downto 1 3
do Heapify(A,i)
7
10
8
7
9
1
2
3
4
5
6
i
1
2
3
4
16
14
10
8
9
Ai
7
21Q Where do we start?
4
1
3
10
9
16
2
14
8
7
10
8
7
9
1
2
3
4
5
6
i
4
14
10
8
9
1
2
3
Ai
16
7
22Q Where do we start? i5
4
1
3
i5
10
9
16
2
14
8
7
10
8
7
9
1
2
3
4
5
6
i
4
14
10
8
9
1
2
3
Ai
16
7
23i4
4
1
3
i4
10
9
16
2
14
8
7
10
8
7
9
1
2
3
4
5
6
i
4
14
10
8
9
1
2
3
Old Ai
16
7
24i4, after heapify
4
1
3
i4
10
9
16
14
8
7
2
10
8
7
9
1
2
3
4
5
6
i
4
14
10
8
9
1
2
3
Old Ai
16
7
25i3
4
1
i3
3
10
9
16
14
8
7
2
10
8
7
9
1
2
3
4
5
6
i
4
Old Ai
14
10
8
9
1
2
3
16
7
26i3, after Heapify
4
1
i3
10
3
9
16
14
8
7
2
10
8
7
9
1
2
3
4
5
6
i
4
14
10
8
9
1
2
3
Old Ai
16
7
27i2
4
i2
1
10
3
9
16
14
8
7
2
10
8
7
9
1
2
3
4
5
6
i
4
14
10
8
9
1
2
3
Old Ai
16
7
28i2, run Heapify
4
i2
16
10
3
9
1
14
8
7
2
10
8
7
9
1
2
3
4
5
6
i
4
14
10
8
9
1
2
3
Old Ai
16
7
29i2, after Heapify
4
i2
16
10
7
3
9
14
8
2
1
10
8
7
9
1
2
3
4
5
6
i
4
14
10
8
9
1
2
3
Old Ai
16
7
30i1
i1
4
16
10
7
3
9
14
8
2
1
10
8
7
9
1
2
3
4
5
6
i
4
14
10
8
9
1
2
3
Old Ai
16
7
31i1
i1, after Heapify
16
14
10
7
3
9
8
2
1
4
10
8
7
9
1
2
3
4
5
6
i
4
14
10
8
9
1
2
3
Old Ai
16
7
32i1
i1, after Heapify
16
14
10
7
3
9
8
2
1
4
10
8
7
9
1
2
3
4
5
6
i
14
8
7
New Ai
16
10
9
1
2
3
4
33Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
16
14
10
3
7
9
8
4
1
2
34Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
16
14
10
3
7
9
8
4
1
2
i10
35Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
1
14
10
heap-size9
3
7
9
8
4
16
2
i10
36Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
After Heapify(A,1)
14
10
8
heap-size9
3
7
9
4
16
1
2
i10
37Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
14
10
8
3
7
9
4
16
1
2
i9
38Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
1
10
8
heap-size8
3
7
9
4
16
14
2
i9
39Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
After Heapify(A,1)
10
9
8
heap-size8
3
1
7
4
16
14
2
i9
40Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
10
9
8
3
1
7
4
16
14
2
i8
41Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
2
9
8
heap-size7
3
1
7
4
16
14
10
i8
42Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
After Heapify(A,1)
9
8
3
heap-size7
1
7
2
4
16
14
10
i8
43Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
After Heapify(A,1)
9
8
3
heap-size7
1
7
2
4
16
14
10
i8
44Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
2
i2
1
3
8
9
7
4
After a few rounds
16
14
10
45Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
After Heapify(A,1)
1
i2
2
3
heap-size1
8
9
7
4
16
14
10
46Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
1
2
3
8
9
7
4
The array is sorted!
16
14
10
47Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
1
2
3
8
9
7
4
Running time?
16
14
10
48Heapsort(A)
- Build-Heap(A)
- for ilengthA down to 2
- exchange A1 and Ai
- heap-sizeAheap-sizeA-1
- Heapify(A,1)
1
2
3
8
9
7
Running time? O(n log n)!
4
16
14
10
49Priority Queue
- A priority queue is a data structure for
maintaining a set S of elements, each with an
associated value called key. //think of
scheduling a set of jobs - It supports the following operations
- Insert(S,x)
- Max(S)
- Delete-Max(S) //Of course Min is the symmetric
case, depending on real application
50Heap-Delete-Max(A)
- If heap-sizeA lt 1
- then print heap underflow
- max ? A1
- A1 ? Aheap-sizeA
- heap-sizeA ? heap-sizeA-1
- Heapify(A,1)
- return max
51Heap-Delete-Max(A)
- If heap-sizeA lt 1
- then print heap underflow
- max ? A1
- A1 ? Aheap-sizeA
- heap-sizeA ? heap-sizeA-1
- Heapify(A,1)
- return max
Running time?
52Heap-Insert(A,key)
- heap-sizeA ? heap-sizeA 1
- i ? heap-sizeA
- While i gt 1 and Aparent(i) lt key
- do Ai ? Aparent(i)
- i ? parent(i)
- Ai ? key
53Heap-Insert(A,key)
- heap-sizeA ? heap-sizeA 1
- i ? heap-sizeA
- While i gt 1 and Aparent(i) lt key
- do Ai ? Aparent(i)
- i ? parent(i)
- Ai ? key
16
14
10
3
7
9
8
4
1
2
15
54Heap-Insert(A,key)
- heap-sizeA ? heap-sizeA 1
- i ? heap-sizeA
- While i gt 1 and Aparent(i) lt key
- do Ai ? Aparent(i)
- i ? parent(i)
- Ai ? key
16
14
10
3
9
15
8
4
1
2
7
55Heap-Insert(A,key)
- heap-sizeA ? heap-sizeA 1
- i ? heap-sizeA
- While i gt 1 and Aparent(i) lt key
- do Ai ? Aparent(i)
- i ? parent(i)
- Ai ? key
16
15
10
3
14
9
8
4
1
2
7
Running time?
56Heap-Insert(A,key)
- heap-sizeA ? heap-sizeA 1
- i ? heap-sizeA
- While i gt 1 and Aparent(i) lt key
- do Ai ? Aparent(i)
- i ? parent(i)
- Ai ? key
16
15
10
3
14
9
8
Question What if we have to increase the key 15
to 18?
4
1
2
7