Title: Heaps and Priority Queues
1Heaps and Priority Queues
2Priority Queues
- New Structure
- Produce smallest/largest quickly
- Sorted list might be a bit too much
- Goals
- Quickly add new things
- Quickly find max/min (Not at same time!)
- But Why?
3Queue of Qs
(Thats Q from the James Bond movies)
4Queue if Qs
Perhaps we need to find the smallest in this
queue of Qs.
5Alternatives
We might search for the smallest one
Or we might keep them in order
6Priority Queues
Perhaps we wish to find the smallest document,
and print it first.
A better example is a queue of documents waiting
to be printed.
This is known as the Shortest Job First
scheduling algorithm
7Priority Queues
The small jobs take less time to print. So its a
better policy to print them first
Take for example a printer queue.
90 mins.
20 mins.
5 secs.
5 mins.
Simply printing in order can cause bottle necks
maybe we need to pump up throughput!
8Poor Technique 1 Unordered Linked List
We could also adopt some rules to make the list
fast
1
Insertion is at the head of the list, ( O (1) ).
2
Deletion requires us to search for the
smallest, ( O (N) ).
So insertion is fast, and deletion is slow
Deletion is what we do when we remove the
smallest element
9Poor Technique 2Ordered Linked List
We can tinker with our previous solution to try
and make it faster. Lets sort the elements on
insertion.
Theres a tradeoff between insertion and deletion
speed
1
Insertion is in order ( O (N) )
2
Deletion simply removes the smallest, ( O
(1) )
So deletion is fast, and insertion is slow
10Better Technique 3BST?
A linked list either required O(N) for either
insertion or deletion. Could we instead use a BST
to get some log(N) behavior for either of the
activities?
As we insert into our BST, we place the smallest
to the left.
What could go wrong?
11Technique 3 over time
A linked list either required O(N) for either
insertion or deletion. Could we instead use a BST
to get some log(N) behavior for either of the
activities?
Over time, what will happen?
BST becomes right heavy--more like a linked
list. Our log (N) insertion/deletion starts to
decay.
12Better Technique 4Balanced BST
- Good
- Add, Delete in O(log n)
- Min/Max O(log n) (Go left or right to end)
- Bad
- Time spent in rotations, balancing
- We only care about Max and Min
- Too much wasted effort
13Data Structure Heap
Vocabulary Terms binary heap, Queuing Use
used to implement a priority queue. Sorting
Use Heapsort uses a heap. Without
qualification, the term heap refers to binary
heap. So what is a heap? A heap is binary
tree The binary tree is completely
filled there are no gaps, except for the
bottom level. A picture might help. . .
14Completeness
Complete
Incomplete
A
A
C
C
B
B
D
E
F
G
D
E
F
G
H
I
J
H
I
J
"Complete" is a stronger property than a BST
since it does not permit gaps in any rows -
followsleft to right in its "completeness"
This missing node makes the tree "incomplete".
15Heap Rules
- Different Kind of Tree
- Two Types
- Min Heap For every node X with parent P, the key
value in P is smaller than or equal to X - Max Heap For every node X with parent P, the key
value in P is larger than or equal to X - Max/Min value always at the tree root node
16Basic Heap Properties
- Structure Property
- organize the data as a complete binary tree
- enables operation in log N time (tree will always
be log N tall!) - Order Property
- In a heap, for every node X with parent P, the
key value in P is smaller than or equal to X - Forces the minimum value to be the trees root
- Tree Operations
- may disturb the structure or the order or both
properties - must restore those properties
17Heap Array Implementation
- Heaps are essentially binary trees
- The requirements and properties allow us to use a
very clever implementationimplement a binary
tree using an Array!!! - Consider a typical array
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
18Heap Array Implementation
00
Ignore this one
01
19Heap Array Implementation
00
Ignore this one
The lines are not real! I have drawn them in to
help show "tree-like" nature of this arrangement!
01
20Heap Array Implementation
- Observe math facts regarding indices
Note The parent of any node i is given by i/2
(integer division)
01
Note The right child of any node i is given by
2i1
Note The left child of any node i is given by 2i
21Why not use arrays for all trees?
- Advantages of conventional dynamic tree
implementation - Can always add one more node easily
- Can move whole chunks of tree by simply
manipulating pointers. Important for balanced
tree algorithms - Doesn't lose efficiency if tree not complete
- Typical tree usage does not call for complete
trees!
- Advantages of array implementation of complete
tree - Space efficient A node only holds data, no
pointers - Quick access to "next" available spot if size is
maintained. O(1). - Can find a nodes parent without lots of extra
pointers and manipulation
22Array Implementation
- For any element at array position i
- left child is at ( 2i )
- right child is at ( 2i1 )
- parent is at (int) (i / 2)
1
A
3
2
C
B
4
5
6
7
D
E
F
G
Dynamic behavior of arrays achieved by
reallocating larger array blocks
8
9
10
H
I
J
0
1
3
4
5
6
7
8
9
2
10
11
12
13
Can just skip this one
23Priority Queue Basic Behavior
- Priority Queues basic behavior
- insert(item)
- "item" should be a data that does have an order
property (like integers, strings (alphabetic
ordering), etc) - min()
- returns the minimum value without changing the
priority queue - removeMin()
- delete and return the smallest item in the
structure - isEmpty()
- makeEmpty()
24Priority Queue Implementation
- An excellent choice is a heap.
- The heap was originally developed as part of a
sorting algorithm called Heapsort - Later, we will show how a heap can be used to
perform the Heapsort
25Our Heap API
- A Heap should implement the following
- void insert(Comparable c)
- Comparable removeMin()
- boolean isEmpty()
- Internally several other methods may/will be
needed - upheap - bubbles a small value up as far asit
should be (like after an insertion to a leaf
position) - downHeap - allows a large value to drop asfar as
it should (like after a removeMin hasforced a
large value to be swapped to the root)
26Remember
- The heap is going to be implemented in an array.
-
- The array has a size.
- The heap also has a size.
- which may be less than or equal to the array
size. - Example array might have size 100, but weve
added only 30 elements to our heap.
27Insertion
- For the moment assume that the heap exists and
that the array is big enough to allow the heap to
expand by at least one element
28Insertion
size 26
2
26
10
18
21
29
27
57
20
47
37
69
69
29
58
A correct heap to start with...
86
95
85
90
31
94
55
98
71
79
91
29Insertion
size 27
2
Increment the size, andplace the 25in that slot
26
10
18
21
29
27
57
20
47
37
69
69
29
58
86
25
95
85
90
31
94
55
98
71
79
91
Insert 25 and call upHeap!
30Insertion
size 27
2
26
10
18
21
29
27
57
20
47
37
69
69
29
58
while(i gt 1 Ai lt Ai/2)
swap(Ai,Ai/2) i i/2
86
25
95
85
90
31
94
55
98
71
79
91
Insert 25
31Insertion
size 27
2
26
10
18
21
29
27
57
20
47
37
69
25
29
58
while(i gt 1 Ai lt Ai/2)
swap(Ai,Ai/2) i i/2
86
69
95
85
90
31
94
55
98
71
79
91
Insert 25
32Insertion
size 27
2
26
10
18
21
25
27
57
20
47
37
69
29
29
58
while(i gt 1 Ai lt Ai/2)
swap(Ai,Ai/2) i i/2
86
69
95
85
90
31
94
55
98
71
79
91
Insert 25
33Insertion
size 27
2
25
10
18
21
26
27
57
20
47
37
69
29
29
58
while(i gt 1 Ai lt Ai/2)
swap(Ai,Ai/2) i i/2
86
69
95
85
90
31
94
55
98
71
79
91
Insert 25
34Insertion
size 27
2
25
10
18
21
26
27
57
20
47
37
69
29
29
58
86
69
95
85
90
31
94
55
98
71
79
91
35Question?
- Will insert work starting with an empty heap?
36removeMin
- We must take the value from the root node and
return it to the user. - Then we must remove the node.
- Easy array implementation
- Take the last element in the heap and put it in
root - Then call downHeap!
37downHeap
- downHeap is called on a node. It checks that the
heap property is being maintained. If not, it
fixes it.
If a fix was made, then downHeap calls itself
recursively on the value that was swapped down
i
27
Details such as checking to make sure elements 2i
and 2i1 are in the heap are omitted
2i1
2i
42
36
38removeMin
size 27
2
25
10
18
21
26
27
57
20
47
37
69
29
29
58
86
69
95
85
90
31
94
55
98
71
79
91
39removeMin
returnValue 2
size 27
2
25
10
18
21
26
27
57
20
47
37
69
29
29
58
86
69
95
85
90
31
94
55
98
71
79
91
40removeMin
returnValue 2
size 27
69
25
10
18
21
26
27
57
20
47
37
69
29
29
58
86
69
95
85
90
31
94
55
98
71
79
91
41removeMin
returnValue 2
size 26
69
Decrement the size
25
10
18
21
26
27
57
20
47
37
69
29
29
58
86
69
95
85
90
31
94
55
98
71
79
91
42removeMin
returnValue 2
size 26
69
25
10
18
21
26
27
57
20
47
37
69
29
29
58
86
95
85
90
31
94
55
98
71
79
91
43removeMin
returnValue 2
size 26
69
25
10
18
21
26
27
57
20
47
37
69
29
29
58
86
95
85
90
31
94
55
98
71
79
91
downHeap
44removeMin
returnValue 2
size 26
10
25
69
18
21
26
27
57
20
47
37
69
29
29
58
86
95
85
90
31
94
55
98
71
79
91
downHeap
45removeMin
returnValue 2
size 26
10
25
69
18
21
26
27
57
20
47
37
69
29
29
58
86
95
85
90
31
94
55
98
71
79
91
downHeap
46removeMin
returnValue 2
size 26
10
25
18
69
21
26
27
57
20
47
37
69
29
29
58
86
95
85
90
31
94
55
98
71
79
91
downHeap
47removeMin
returnValue 2
size 26
10
25
18
69
21
26
27
57
20
47
37
69
29
29
58
86
95
85
90
31
94
55
98
71
79
91
downHeap
48removeMin
returnValue 2
size 26
10
25
18
20
21
26
27
57
69
47
37
69
29
29
58
86
95
85
90
31
94
55
98
71
79
91
downHeap
49removeMin
returnValue 2
size 26
10
25
18
20
21
26
27
57
69
47
37
69
29
29
58
86
95
85
90
31
94
55
98
71
79
91
downHeap
50removeMin
returnValue 2
size 26
10
25
18
20
21
26
27
57
31
47
37
69
29
29
58
86
95
85
90
69
94
55
98
71
79
91
downHeap
51removeMin
returnValue 2
size 26
10
25
18
20
21
26
27
57
31
47
37
69
29
29
58
86
95
85
90
69
94
55
98
71
79
91
52removeMin
size 26
10
25
18
20
21
26
27
57
31
47
37
69
29
29
58
86
95
85
90
69
94
55
98
71
79
91
53Details
- isEmpty
- Simply tests size
- makeEmpty
- if this is wanted,
- can just set size to zero.
- At this point our heap can easily implement a
priority queue given that we start with an empty
queue and add and remove elements as necessary. - If we were given an array of numbers it might be
useful to turn the array into a heap "in-place"
54BuildHeap
- Given an array of numbers how do we convert it
into a heap. - We could iterate through the array and insert
each number into heap but this requires us to
have space big enough for twice the quantity of
numbers we have. - We want to convert in place.
- Note We will assume that element 0 is not used.
- Don't confuse this with downHeap. BuildHeap will
use downHeap and is used to create the Heap
55BuildHeap
- Assume that we have the number of elements in a
variable called size - Iterate index from element arraySize/2 down to
index 1 - downHeap(index)
56BuildHeap
size 26
87
68
43
6
77
33
9
11
19
99
6
23
89
2
14
22
1
5
27
35
7
42
12
71
3
67
57BuildHeap
size 26
87
68
43
6
77
33
9
13
11
19
99
6
23
89
2
14
Start at element arraySize/2
22
1
5
27
35
7
42
12
71
3
67
58BuildHeap
size 26
87
68
43
6
77
33
9
13
11
19
99
6
23
89
2
14
22
1
5
27
35
7
42
12
71
3
67
downHeap
59BuildHeap
size 26
87
68
43
6
77
33
9
13
11
19
99
6
23
22
2
14
89
1
5
27
35
7
42
12
71
3
67
downHeap
60BuildHeap
size 26
87
68
43
6
77
33
9
13
11
19
99
6
23
22
2
14
89
1
5
27
35
7
42
12
71
3
67
downHeap
61BuildHeap
size 26
87
68
43
6
77
33
9
13
11
19
99
6
3
22
2
14
89
1
5
27
35
7
42
12
71
23
67
downHeap
62BuildHeap
size 26
87
68
43
6
77
33
9
13
11
19
99
6
3
22
2
14
89
1
5
27
35
7
42
12
71
23
67
downHeap
63BuildHeap
size 26
87
68
43
6
77
33
9
13
11
19
99
6
3
22
2
14
89
1
5
27
35
7
42
12
71
23
67
downHeap
64BuildHeap
size 26
87
68
43
6
77
33
9
13
11
19
7
6
3
22
2
14
89
1
5
27
35
99
42
12
71
23
67
downHeap
65BuildHeap
size 26
87
68
43
6
77
33
9
13
11
19
7
6
3
22
2
14
89
1
5
27
35
99
42
12
71
23
67
downHeap
66BuildHeap
size 26
87
68
43
6
77
33
9
13
11
19
7
6
3
22
2
14
89
1
5
27
35
99
42
12
71
23
67
downHeap
67BuildHeap
size 26
87
68
43
6
77
33
9
13
1
19
7
6
3
22
2
14
89
11
5
27
35
99
42
12
71
23
67
downHeap
68BuildHeap
size 26
87
68
43
6
77
33
9
13
1
19
7
6
3
22
2
14
89
11
5
27
35
99
42
12
71
23
67
downHeap
69BuildHeap
size 26
87
68
43
6
77
33
2
13
1
19
7
6
3
22
9
14
89
11
5
27
35
99
42
12
71
23
67
downHeap
70BuildHeap
size 26
87
68
43
6
77
33
2
13
1
19
7
6
3
22
9
14
89
11
5
27
35
99
42
12
71
23
67
downHeap
71BuildHeap
size 26
87
68
43
6
77
3
2
13
1
19
7
6
33
22
9
14
89
11
5
27
35
99
42
12
71
23
67
downHeap
72BuildHeap
size 26
87
68
43
6
77
3
2
13
1
19
7
6
33
22
9
14
89
11
5
27
35
99
42
12
71
23
67
downHeap
73BuildHeap
size 26
87
68
43
6
77
3
2
13
1
19
7
6
23
22
9
14
89
11
5
27
35
99
42
12
71
33
67
downHeap
74BuildHeap
size 26
87
68
43
6
77
3
2
13
1
19
7
6
23
22
9
14
89
11
5
27
35
99
42
12
71
33
67
downHeap
75BuildHeap
size 26
87
68
43
6
6
3
2
13
1
19
7
77
23
22
9
14
89
11
5
27
35
99
42
12
71
33
67
downHeap
76BuildHeap
size 26
87
68
43
6
6
3
2
13
1
19
7
77
23
22
9
14
89
11
5
27
35
99
42
12
71
33
67
downHeap
77BuildHeap
size 26
87
68
43
6
6
3
2
13
1
19
7
12
23
22
9
14
89
11
5
27
35
99
42
77
71
33
67
downHeap
78BuildHeap
size 26
87
68
43
6
6
3
2
13
1
19
7
12
23
22
9
14
89
11
5
27
35
99
42
77
71
33
67
downHeap
79BuildHeap
size 26
87
68
43
1
6
3
2
13
6
19
7
12
23
22
9
14
89
11
5
27
35
99
42
77
71
33
67
downHeap
80BuildHeap
size 26
87
68
43
1
6
3
2
13
6
19
7
12
23
22
9
14
89
11
5
27
35
99
42
77
71
33
67
downHeap
81BuildHeap
size 26
87
68
43
1
6
3
2
13
5
19
7
12
23
22
9
14
89
11
6
27
35
99
42
77
71
33
67
downHeap
82BuildHeap
size 26
87
68
43
1
6
3
2
13
5
19
7
12
23
22
9
14
89
11
6
27
35
99
42
77
71
33
67
downHeap
83BuildHeap
size 26
87
2
43
1
6
3
68
13
5
19
7
12
23
22
9
14
89
11
6
27
35
99
42
77
71
33
67
downHeap
84BuildHeap
size 26
87
2
43
1
6
3
68
13
5
19
7
12
23
22
9
14
89
11
6
27
35
99
42
77
71
33
67
downHeap
85BuildHeap
size 26
87
2
43
1
6
3
9
13
5
19
7
12
23
22
68
14
89
11
6
27
35
99
42
77
71
33
67
downHeap
86BuildHeap
size 26
87
2
43
1
6
3
9
13
5
19
7
12
23
22
68
14
89
11
6
27
35
99
42
77
71
33
67
downHeap
87BuildHeap
size 26
87
2
1
43
6
3
9
13
5
19
7
12
23
22
68
14
89
11
6
27
35
99
42
77
71
33
67
downHeap
88BuildHeap
size 26
87
2
1
43
6
3
9
13
5
19
7
12
23
22
68
14
89
11
6
27
35
99
42
77
71
33
67
downHeap
89BuildHeap
size 26
87
2
1
5
6
3
9
13
43
19
7
12
23
22
68
14
89
11
6
27
35
99
42
77
71
33
67
downHeap
90BuildHeap
size 26
87
2
1
5
6
3
9
13
43
19
7
12
23
22
68
14
89
11
6
27
35
99
42
77
71
33
67
downHeap
91BuildHeap
size 26
87
2
1
5
6
3
9
13
6
19
7
12
23
22
68
14
89
11
43
27
35
99
42
77
71
33
67
downHeap
92BuildHeap
size 26
87
2
1
5
6
3
9
13
6
19
7
12
23
22
68
14
89
11
43
27
35
99
42
77
71
33
67
downHeap
93BuildHeap
size 26
1
2
87
5
6
3
9
13
6
19
7
12
23
22
68
14
89
11
43
27
35
99
42
77
71
33
67
downHeap
94BuildHeap
size 26
1
2
87
5
6
3
9
13
6
19
7
12
23
22
68
14
89
11
43
27
35
99
42
77
71
33
67
downHeap
95BuildHeap
size 26
1
2
5
87
6
3
9
13
6
19
7
12
23
22
68
14
89
11
43
27
35
99
42
77
71
33
67
downHeap
96BuildHeap
size 26
1
2
5
87
6
3
9
13
6
19
7
12
23
22
68
14
89
11
43
27
35
99
42
77
71
33
67
downHeap
97BuildHeap
size 26
1
2
5
6
6
3
9
13
87
19
7
12
23
22
68
14
89
11
43
27
35
99
42
77
71
33
67
downHeap
98BuildHeap
size 26
1
2
5
5
6
3
9
13
87
19
7
12
23
22
68
14
89
11
43
27
35
99
42
77
71
33
67
downHeap
99BuildHeap
size 26
1
2
5
5
6
3
9
13
11
19
7
12
23
22
68
14
89
87
43
27
35
99
42
77
71
33
67
downHeap
100BuildHeap
size 26
1
2
5
5
6
3
9
13
11
19
7
12
23
22
68
14
89
87
43
27
35
99
42
77
71
33
67
101What else can we do?
- Use the heap for sorting.
- Heapsort!
102Basic Idea
- Given an unsorted array we use BuildHeap to
convert it into a heap - While heap is not empty
- removeMin - remember that replaces the top
element with the last element of the
heap/array and then heapifies - The heap (logical concept) is one smaller but the
array (physical concept) hasn't changed - Put the item just removed in the element just
after the logical end of the heap - At conclusion the array is sorted
103HeapSortafter BuildHeap
size 26
1
2
5
5
6
3
9
11
19
7
12
23
22
68
14
89
87
43
27
35
99
42
77
71
33
67
104HeapSort
size 26
1
removeMin
2
5
5
6
3
9
11
19
7
12
23
22
68
14
89
87
43
27
35
99
42
77
71
33
67
105HeapSort
size 26
89
removeMin
2
5
5
6
3
9
11
19
7
12
23
22
68
14
89
87
43
27
35
99
42
77
71
33
67
106HeapSort
size 25
2
removeMin 1
3
5
5
6
22
9
11
19
7
12
23
89
68
14
Not in heap now
87
43
27
35
99
42
77
71
33
67
107HeapSort
size 25
2
3
5
5
6
22
9
11
19
7
12
23
89
68
14
1
87
43
27
35
99
42
77
71
33
67
heap is 1 element smaller, smallest element is at
end of array
108HeapSort
size 25
2
3
5
5
6
22
9
11
19
7
12
23
89
68
14
1
87
43
27
35
99
42
77
71
33
67
Now do it again!
109HeapSort
size 24
3
9
5
5
6
22
14
11
19
7
12
23
89
68
67
1
87
43
27
35
99
42
77
71
33
2
110HeapSort
size 23
5
9
5
11
6
22
14
33
19
7
12
23
89
68
67
1
87
43
27
35
99
42
77
71
3
2
111HeapSort
size 22
5
9
6
11
7
22
14
33
19
42
12
23
89
68
67
1
87
43
27
35
99
71
77
5
3
2
112HeapSort
size 21
6
9
7
11
7
22
14
33
19
42
12
23
89
68
67
1
87
43
27
35
99
71
5
5
3
2
113HeapSort
size 20
7
9
7
11
12
22
14
33
19
42
71
23
89
68
67
1
87
43
27
35
99
6
5
5
3
2
114HeapSort
size 19
7
9
11
19
12
22
14
33
27
42
71
23
89
68
67
1
87
43
99
35
7
6
5
5
3
2
115HeapSort
size 18
9
14
11
19
12
22
35
33
27
42
71
23
89
68
67
1
87
43
99
7
7
6
5
5
3
2
116HeapSort
size 17
11
14
12
19
42
22
35
33
27
99
71
23
89
68
67
1
87
43
9
7
7
6
5
5
3
2
117HeapSort
size 16
12
14
19
27
42
22
35
33
43
99
71
23
89
68
67
1
87
11
9
7
7
6
5
5
3
2
118HeapSort
size 15
14
22
19
27
42
23
35
33
43
99
71
87
89
68
67
1
12
11
9
7
7
6
5
5
3
2
119HeapSort
size 14
19
22
27
33
42
23
35
67
43
99
71
87
89
68
14
1
12
11
9
7
7
6
5
5
3
2
120HeapSort
size 13
22
23
27
33
42
68
35
67
43
99
71
87
89
19
14
1
12
11
9
7
7
6
5
5
3
2
121HeapSort
size 12
23
35
27
33
42
68
89
67
43
99
71
87
22
19
14
1
12
11
9
7
7
6
5
5
3
2
122HeapSort
size 11
27
35
33
43
42
68
89
67
87
99
71
23
22
19
14
1
12
11
9
7
7
6
5
5
3
2
123HeapSort
size 10
33
35
42
43
71
68
89
67
87
99
27
23
22
19
14
1
12
11
9
7
7
6
5
5
3
2
124HeapSort
size 9
35
68
42
43
71
99
89
67
87
33
27
23
22
19
14
1
12
11
9
7
7
6
5
5
3
2
125HeapSort
size 8
42
68
43
67
71
99
89
87
35
33
27
23
22
19
14
1
12
11
9
7
7
6
5
5
3
2
126HeapSort
size 7
43
68
67
87
71
99
89
42
35
33
27
23
22
19
14
1
12
11
9
7
7
6
5
5
3
2
127HeapSort
size 6
67
68
71
87
89
99
43
42
35
33
27
23
22
19
14
1
12
11
9
7
7
6
5
5
3
2
128HeapSort
size 5
68
99
71
67
43
87
89
42
35
33
27
23
22
19
14
12
1
11
9
7
7
6
5
5
3
2
129HeapSort
size 4
71
99
87
89
68
67
43
42
35
33
27
23
22
19
14
1
12
11
9
7
7
6
5
5
3
2
130HeapSort
size 3
87
99
89
71
68
67
43
42
35
33
27
23
22
19
14
1
12
11
9
7
7
6
5
5
3
2
131HeapSort
size 2
89
87
99
71
68
67
43
42
35
33
27
23
22
19
14
1
12
11
9
7
7
6
5
5
3
2
132HeapSort
size 1
99
87
89
71
68
67
43
42
35
33
27
23
22
19
14
1
12
11
9
7
7
6
5
5
3
2