Heaps,heapsort and priority queue - PowerPoint PPT Presentation

About This Presentation
Title:

Heaps,heapsort and priority queue

Description:

A heap is a binary tree with the following properties: ... heap property ... Now we cover a fundamental procedure regarding heaps: the heapify procedure. ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 57
Provided by: csMon
Category:

less

Transcript and Presenter's Notes

Title: Heaps,heapsort and priority queue


1
Heaps,heapsort and priority queue
  • Binhai Zhu
  • Computer Science Department, Montana State
    University

2
Definition
  • 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.

3
Example
16
14
10
3
7
9
8
1
4
2
4
Example
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
5
Definition
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
6
Example
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
7
Example
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
8
Example
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
9
Example
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
10
Example
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
11
Example
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
12
Heap 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
13
Heap 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
14
The 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
15
Now 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
16
Now 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
20
We 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
21
Q 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
22
Q 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
23
i4
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
24
i4, 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
25
i3
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
26
i3, 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
27
i2
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
28
i2, 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
29
i2, 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
30
i1
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
31
i1
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
32
i1
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
33
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

16
14
10
3
7
9
8
4
1
2
34
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

16
14
10
3
7
9
8
4
1
2
i10
35
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

1
14
10
heap-size9
3
7
9
8
4
16
2
i10
36
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

After Heapify(A,1)
14
10
8
heap-size9
3
7
9
4
16
1
2
i10
37
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

14
10
8
3
7
9
4
16
1
2
i9
38
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

1
10
8
heap-size8
3
7
9
4
16
14
2
i9
39
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

After Heapify(A,1)
10
9
8
heap-size8
3
1
7
4
16
14
2
i9
40
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

10
9
8
3
1
7
4
16
14
2
i8
41
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

2
9
8
heap-size7
3
1
7
4
16
14
10
i8
42
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

After Heapify(A,1)
9
8
3
heap-size7
1
7
2
4
16
14
10
i8
43
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

After Heapify(A,1)
9
8
3
heap-size7
1
7
2
4
16
14
10
i8
44
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

2
i2
1
3
8
9
7
4
After a few rounds
16
14
10
45
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

After Heapify(A,1)
1
i2
2
3
heap-size1
8
9
7
4
16
14
10
46
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

1
2
3
8
9
7
4
The array is sorted!
16
14
10
47
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

1
2
3
8
9
7
4
Running time?
16
14
10
48
Heapsort(A)
  1. Build-Heap(A)
  2. for ilengthA down to 2
  3. exchange A1 and Ai
  4. heap-sizeAheap-sizeA-1
  5. Heapify(A,1)

1
2
3
8
9
7
Running time? O(n log n)!
4
16
14
10
49
Priority 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

50
Heap-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

51
Heap-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?
52
Heap-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

53
Heap-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
54
Heap-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
55
Heap-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?
56
Heap-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
Write a Comment
User Comments (0)
About PowerShow.com