Professional Documents
Culture Documents
Topics
• Priority Queues
• Heaps
• Heapsort
2
Priority Queue
A data structure implementing a set S of elements, each
associated with a key, supporting the following
operations:
insert(S, x) : insert element x into set S
max(S) : return element of S with largest key
extract_max(S) : return element of S with largest key
and remove it from S
increase_key(S, x, k) : increase the value of element x’ s key to
new value k
(assumed to be as large as current value)
3
Background: Binary Trees
root
Has a root at the topmost level
Each node has zero, one or two Parent(x)
children
A node that has no child is called a
leaf x
For a node x, we denote the left leaf leaf
child, right child and of x as
left(x), right(x) and the parent left(x)right(x)
parent(x), respectively.
leaf
Height (Depth) of a Binary
Tree
The number of edges on the longest path from the
root to a leaf.
Height = 4
Background: Complete Binary
Trees
A complete binary tree is the tree
Where a node can have 0 (for the leaves) or 2 children and
All leaves are at the same depth
2 4
1
2
16 3
14 10 1 2 3 4 5 6 7 8 9
5 6 7
4 10
8 7 9 3 1614 10
8 9 8 7 9 3 2 4 1
10
2 4 1 8
(Binary) Heap
Heaps are “almost complete binary trees”
All levels are full except possibly the lowest level
If the lowest level is not full, then nodes must be packed to the left
2 5 2 5
4 3 6 1 3 6
Heap-order property: the value at each node is less than or equal to the
values at both its descendants --- Min Heap
10
2No4pointers
1 required! Heig ht of a binary heap is O(lg
5
Heap Operations
build_max_heap : produce a max-heap from an
unordered array
14
Insertion
Algorithm
1. Add the new element to the next available position at the lowest
level
2. Restore the min-heap property if violated
General strategy is percolate up (or bubble up): if the parent of the
element is larger than the element, then interchange the parent and
child.
1 1 1
2 5 2 5 2 2.5
swap
4 3 6 4 3 6 2.5 4 3 6 5
9 8
A heap!
17 16 14 10
20 18
17
Max_heapify (Example)
Node 10 is the left child of node 5 but is drawn to the right for
18
convenience
Max_heapify (Example)
19
Max_heapify (Example)
Time=? O(log n)
20
Max_Heapify Pseudocode
l = left(i)
r = right(i)
if (l <= heap-size(A) and A[l] > A[i])
then largest = lelse largest = i
if (r <= heap-size(A) and A[r] > A[largest]) then
largest = r
if largest = i
then exchange A[i] and A[largest]
Max_Heapify(A, largest)
21
Build_Max_Heap(A)
Converts A[1…n] to a max heap
Build_Max_Heap(A):
for i=n/2 downto 1
do Max_Heapify(A, i)
Build_Max_Heap(A):
for i=n/2 downto 1
do
Max_Heapify(
A, i)
Build_Max_Heap(A):
for i=n/2 downto 1
do
Max_Heapify(
A, i)
Total amount of
work in the for
loop can be
summed as:
n/4 (1 c) + n/8 (2
c) + n/16 (3 c)
Build-Max-Heap Demo
25
Build-Max-Heap Demo
26
Build-Max-Heap
27
deleteMin: First Attempt
Algorithm
1. Delete the root.
2. Compare the two children of the root
3. Make the lesser of the two the root.
4. An empty spot is created.
5. Bring the lesser of the two children of the empty spot to the empty
spot.
6. A new empty spot is created.
7. Continue
Example for First Attempt
1
2 5 2 5
4 3 6 4 3 6
2 1
5 3 5
4 3 6 4 6
32
Heap-Sort
Sorting Strategy:
33
Heap-Sort
Sorting Strategy:
34
Heap-Sort
Sorting Strategy:
35
Heap-Sort
Sorting Strategy:
Max_heapify(A,1)
37
Heap-Sort Demo
38
Heap-Sort Demo
39
Heap-Sort Demo
40
Heap-Sort
Running time:
after n iterations the Heap is empty
every iteration involves a swap and a max_heapify
operation; hence it takes O(log n) time
41
Priority Queue: Motivating
Example
3 jobs have been submitted to a printer in the order A, B, C.
Sizes: Job A – 100 pages
Job B – 10 pages
Job C -- 1 page
Linked list
Insert in O(1)
Find the minimum element in O(n), thus deleteMin is O(n)
Binary search tree (AVL tree, to be covered later)
Insert in O(log n)
Delete in O(log n)
Search tree is an overkill as it does many other operations
Eerr, neither fit quite well…
References
MIT courseware
Introduction to algorithms by Thomas H. Cormen