![Page 1: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/1.jpg)
Priority queues,binary heaps,and heapsort
(6.9, 21.1 – 21.5)
![Page 2: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/2.jpg)
Priority queue
A priority queue is an ADT supporting three operations:● insert: add an element● findMin: return the smallest element● deleteMin: remove the smallest element
Allows you to maintain a set of elements while always knowing the smallest one
Java: PriorityQueue<E> class defining add, element and remove
![Page 3: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/3.jpg)
Implementing a priority queue
Several possible ways:● An unsorted array?● A sorted array?● A binary search tree?● A balanced binary search tree?
A nicer way: a binary heap
![Page 4: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/4.jpg)
The heap property
A tree satisfies the heap property if the value of each node is less than (or equal to) the value of its children:
What can we say about the root node?
8
18 29
37 32 74 89
20 28 39 66
Root node is thesmallest –
can do findMinin O(1) time
![Page 5: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/5.jpg)
Binary heap
A binary heap is a complete binary tree that satisfies the heap property:
Note: it is not a binary search tree!
Complete means that all levels except the bottom one are full, and the bottom level is filled from left to right (see diagram)
8
18 29
37 26 76 32 74 89
20 28 39 66
![Page 6: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/6.jpg)
Binary heap invariant
The binary heap invariant:● The tree must be complete● It must have the heap property (each node is less
than or equal to its children)
Remember, all our operations must preserve this invariant
(Why this invariant? See later!)
![Page 7: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/7.jpg)
Adding an element to a binary heap
Step 1: insert the element at the next empty position in the tree
This might break the heap invariant!
In this case, 12 is less than 66, its parent.
8
18 29
37 26 76 32 74 89
20 28 39 66
12
![Page 8: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/8.jpg)
Adding an element to a binary heap
Step 2: if the new element is less than its parent, swap it with its parent
The invariant is still broken, since 12 is less than 29, its new parent
8
18 29
37 26 76 32 74 89
20 28 39 12
66
![Page 9: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/9.jpg)
Adding an element to a binary heap
Repeat step 2 until the new element is greater than or equal to its parent.
Now 12 is in its right place, and the invariant is restored. (Think about why this algorithm restores the invariant.)
8
18 12
37 26 76 32 74 89
20 28 39 29
66
![Page 10: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/10.jpg)
Why this works
At every step, the heap property almost holds except that the new element might be less than its parent
After swapping the element and its parent, still only the new element can be in the wrong place (why?)
8
18 29
37 26 76 32 74 89
20 28 39 12
66
![Page 11: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/11.jpg)
Removing the minimum element
Step 1: replace the root element with the last element in the heap
The invariant is broken, because 66 is greater than its children
66
18 12
37 26 76 32 74 89
20 28 39 29
![Page 12: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/12.jpg)
Removing the minimum element
Step 2: if the moved element is greater than its children, swap it with its least child
(Why not its greatest child?)
12
18 66
37 26 76 32 74 89
20 28 39 29
![Page 13: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/13.jpg)
Removing the minimum element
Step 3: repeat until the moved element is less than or equal to its children
12
18 29
37 26 76 32 74 89
20 28 39 66
![Page 14: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/14.jpg)
Sifting
Two useful operations in implementing a heap
Sift up: if an element might be less than its parent, i.e. “too low” (used in insert)● Repeatedly swap the element with its parent
Sift down: if an element might be greater than its children, i.e. “too high” (used in deleteMin)● Repeatedly swap the element with its least child
![Page 15: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/15.jpg)
Summary so far
Binary heap: complete binary tree where every node is less than or equal to its children
Minimum is root node
Insert: add to end of tree and sift up (called percolate up in book)
Delete minimum: swap with final element and sift down (called percolate down in book)
![Page 16: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/16.jpg)
Binary heaps are arrays!
A binary heap is really implemented using an array: 8
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
8 18 29 20 28 39 66 37 26 76 32 74 89
![Page 17: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/17.jpg)
Child positions
8
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
8 18 29 20 28 39 66 37 26 76 32 74 89
Pare
nt
L. Child
R. C
hil d
The left child of node iis at index 2i + 1
in the array...
...the right childis at index 2i + 2
![Page 18: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/18.jpg)
Child positions
8
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
8 18 29 20 28 39 66 37 26 76 32 74 89
The left child of node iis at index 2i + 1
in the array...
...the right childis at index 2i + 2
Pare
nt
L. Child
R. C
hil d
![Page 19: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/19.jpg)
Child positions
8
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
8 18 29 20 28 39 66 37 26 76 32 74 89
The left child of node iis at index 2i + 1
in the array...
...the right childis at index 2i + 2
Pare
nt
L. Child
R. C
hil d
![Page 20: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/20.jpg)
Child positions
8
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
8 18 29 20 28 39 66 37 26 76 32 74 89
The left child of node iis at index 2i + 1
in the array...
...the right childis at index 2i + 2
Pare
nt
L. Child
R. C
hil d
![Page 21: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/21.jpg)
Child positions
8
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
8 18 29 20 28 39 66 37 26 76 32 74 89
The left child of node iis at index 2i + 1
in the array...
...the right childis at index 2i + 2
Pare
nt
L. Child
R. C
hil d
![Page 22: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/22.jpg)
Parent position
8
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
8 18 29 20 28 39 66 37 26 76 32 74 89
The parent of node iis at index (i-1)/2
Pare
nt
Child
![Page 23: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/23.jpg)
Why the binary heap invariant
The binary heap invariant: the tree is complete and satisfies the heap property● Because of the heap property, we can find the
minimum element in O(1) time● Because the tree is complete, we can represent a
heap of n items with an array of size n
![Page 24: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/24.jpg)
Warning
We are using 0-based arrays, the obvious choice in Java
The book, for some reason, uses 1-based arrays (and later switches to 0-based arrays)!
In a heap implemented using a 1-based array:● the left child of index i is index 2i● the right child is index 2i+1● the parent is index i/2
Be careful when doing the lab!
![Page 25: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/25.jpg)
Reminder: inserting into a binary heap
To insert an element into a binary heap:● Add the new element at the end of the heap● Sift the element up: while the element is less than
its parent, swap it with its parent
We can just as well implement this using the array representation! Just need to use index calculations instead of following left/right/parent links.
![Page 26: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/26.jpg)
Inserting into a binary heap
Step 1: add the new element to the end of the array, set child to its index
6
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
6 18 29 20 28 39 66 37 26 76 32 74 89
13
8
Child
8
![Page 27: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/27.jpg)
Inserting into a binary heap
Step 2: compute parent = (child-1)/26
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
6 18 29 20 28 39 66 37 26 76 32 74 89
13
8
Child
Pare
nt
8
![Page 28: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/28.jpg)
Inserting into a binary heap
Step 3: if array[parent] > array[child], swap them 6
18 29
37 26 76 32 74 89
20 28 39 8
0 1 2 7 8 9 10 11 123 4 5 6
6 18 29 20 28 39 8 37 26 76 32 74 89
13
66
Child
Pare
nt
66
![Page 29: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/29.jpg)
Inserting into a binary heap
Step 4: set child = parent, parent = (child 1) / 2– , and repeat
6
18 29
37 26 76 32 74 89
20 28 39 8
0 1 2 7 8 9 10 11 123 4 5 6
6 18 29 20 28 39 8 37 26 76 32 74 89
13
66
66
Ch
ild
Pare
nt
![Page 30: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/30.jpg)
Inserting into a binary heap
Step 4: set child = parent, parent = (child 1) / 2– , and repeat
6
18 8
37 26 76 32 74 89
20 28 39 29
0 1 2 7 8 9 10 11 123 4 5 6
6 18 8 20 28 39 29 37 26 76 32 74 89
13
66
66
Ch
ild
Pare
nt
![Page 31: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/31.jpg)
The insertion algorithm
Insert x at the end of the array, let child be its index and parent = (child-1)/2
While child > 0 and array[parent] > array[child]:● Swap array[parent] and array[child]● Set child = parent and parent = (child-1)/2
Does many swap operations! Moves the new element many times in the heap.
Just the insertion algorithm from before, but using an array to represent the tree!
![Page 32: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/32.jpg)
An optimisation from the book
Insert x null at the end of the array, let child be its index and parent = (child-1)/2
While child > 0 and array[parent] > array[child] array[parent] > x:● Swap array[parent] and array[child]
Set array[child] = array[parent]● Set child = parent and parent = (child-1)/2
Finally, set array[parent] = x
Makes a space for x and then puts it there
array[child] wasalways x before
![Page 33: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/33.jpg)
Optimised insertion
Step 1: increase the size of the array, set child to the final index
6
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
6 18 29 20 28 39 66 37 26 76 32 74 89
13
Child
![Page 34: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/34.jpg)
Optimised insertion
Step 2: compute parent = (child-1)/26
18 29
37 26 76 32 74 89
20 28 39 66
0 1 2 7 8 9 10 11 123 4 5 6
6 18 29 20 28 39 66 37 26 76 32 74 89
13
Child
Pare
nt
![Page 35: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/35.jpg)
Optimised insertion
Step 3: if array[parent] > x (8 here), move parent downwards
6
18 29
37 26 76 32 74 89
20 28 39
0 1 2 7 8 9 10 11 123 4 5 6
6 18 29 20 28 39 37 26 76 32 74 89
13
66
Child
Pare
nt
66
![Page 36: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/36.jpg)
Optimised insertion
Step 4: set child = parent, parent = (child 1) / 2– , and repeat
6
18 29
37 26 76 32 74 89
20 28 39
0 1 2 7 8 9 10 11 123 4 5 6
6 18 29 20 28 39 37 26 76 32 74 89
13
66
66
Ch
ild
Pare
nt
![Page 37: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/37.jpg)
Optimised insertion
Step 4: set child = parent, parent = (child 1) / 2– , and repeat
6
18
37 26 76 32 74 89
20 28 39 29
0 1 2 7 8 9 10 11 123 4 5 6
6 18 20 28 39 29 37 26 76 32 74 89
13
66
66
Ch
ild
Pare
nt
![Page 38: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/38.jpg)
Optimised insertion
Step 4: set child = parent, parent = (child 1) / 2– , and repeat
6
18
37 26 76 32 74 89
20 28 39 29
0 1 2 7 8 9 10 11 123 4 5 6
6 18 20 28 39 29 37 26 76 32 74 89
13
66
66
Child
Pare
nt
![Page 39: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/39.jpg)
Optimised insertion
Step 5: write x into position child
6
18
37 26 76 32 74 89
20 28 39 29
0 1 2 7 8 9 10 11 123 4 5 6
6 18 20 28 39 29 37 26 76 32 74 89
13
66
66
Child
Pare
nt
8
8
![Page 40: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/40.jpg)
Complexity
The depth of the tree is O(log n)
So O(log n) swaps for insert and deleteMin
So O(1) findMin, O(log n) insert, O(log n) deleteMin
![Page 41: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/41.jpg)
An extra operation: decreaseKey
What if you want to decrease the value of an element? (This pops up in some algorithms, and lab 2)
Step 1: alter the element. Might break the heap invariant, because the element might be smaller than its parent. So...
Step 2: sift the element up
However, you need to know the index of the element to sift it up! Solution: maintain a multimap from elements to their indices, and update it whenever you modify the heap
![Page 42: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/42.jpg)
Heapsort
It's quite easy to sort a list using a heap:● add all the list elements to an empty heap● repeatedly find and remove the smallest element
from the heap, and add it to the result list
(this is a kind of selection sort)
However, this algorithm is not in-place. Heapsort uses the same idea, but without allocating any extra memory.
![Page 43: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/43.jpg)
Heapsort
It's quite easy to sort a list using a heap:● add all the list elements to an empty heap● repeatedly find and remove the smallest element
from the heap, and add it to the result list
(this is a kind of selection sort)
However, this algorithm is not in-place. Heapsort uses the same idea, but without allocating any extra memory.
![Page 44: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/44.jpg)
Heapsort, in-place
We will build a max heap, a heap where you can find and delete the maximum element instead of the minimum● Simple change to heap operations
First turn array into a max heap, in-place
Now, we have a heap; how to turn it into a sorted array?● Swap the maximum (first) element with the last element● Reduce the heap's size by 1, so the heap no longer includes the maximum
element● Sift the first element down
This has the effect of deleting the biggest element and putting it at the end of the array – at every stage, the beginning of the array is a heap and the end contains sorted data
![Page 45: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/45.jpg)
Trace of heapsort
89
76 74
37 32 39 66
20 26 18 28 29 6
First build a heap (not shown)
![Page 46: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/46.jpg)
Trace of heapsort
89
76 74
37 32 39 66
20 26 18 28 29 6
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 47: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/47.jpg)
Trace of heapsort
6
76 74
37 32 39 66
20 26 18 28 29 89
Step 2: sift first element down
![Page 48: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/48.jpg)
Trace of heapsort
76
6 74
37 32 39 66
20 26 18 28 29 89
Step 2: sift first element down
![Page 49: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/49.jpg)
Trace of heapsort
76
37 74
6 32 39 66
20 26 18 28 29 89
Step 2: sift first element down
![Page 50: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/50.jpg)
Trace of heapsort
76
37 74
26 32 39 66
20 6 18 28 29 89
Step 2: sift first element down
![Page 51: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/51.jpg)
Trace of heapsort
76
37 74
26 32 39 66
20 6 18 28 29 89
![Page 52: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/52.jpg)
Trace of heapsort
76
37 74
26 32 39 66
20 6 18 28 29 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 53: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/53.jpg)
Trace of heapsort
29
37 74
26 32 39 66
20 6 18 28 76 89
Step 2: sift first element down
![Page 54: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/54.jpg)
Trace of heapsort
74
37 29
26 32 39 66
20 6 18 28 76 89
Step 2: sift first element down
![Page 55: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/55.jpg)
Trace of heapsort
74
37 66
26 32 39 29
20 6 18 28 76 89
Step 2: sift first element down
![Page 56: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/56.jpg)
Trace of heapsort
74
37 66
26 32 39 29
20 6 18 28 76 89
![Page 57: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/57.jpg)
Trace of heapsort
74
37 66
26 32 39 29
20 6 18 28 76 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 58: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/58.jpg)
Trace of heapsort
28
37 66
26 32 39 29
20 6 18 74 76 89
Step 2: sift first element down
![Page 59: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/59.jpg)
Trace of heapsort
66
37 28
26 32 39 29
20 6 18 74 76 89
Step 2: sift first element down
![Page 60: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/60.jpg)
Trace of heapsort
66
37 39
26 32 28 29
20 6 18 74 76 89
Step 2: sift first element down
![Page 61: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/61.jpg)
Trace of heapsort
66
37 39
26 32 28 29
20 6 18 74 76 89
![Page 62: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/62.jpg)
Trace of heapsort
66
37 39
26 32 28 29
20 6 18 74 76 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 63: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/63.jpg)
Trace of heapsort
18
37 39
26 32 28 29
20 6 66 74 76 89
Step 2: sift first element down
![Page 64: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/64.jpg)
Trace of heapsort
39
37 18
26 32 28 29
20 6 66 74 76 89
Step 2: sift first element down
![Page 65: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/65.jpg)
Trace of heapsort
39
37 29
26 32 28 18
20 6 66 74 76 89
Step 2: sift first element down
![Page 66: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/66.jpg)
Trace of heapsort
39
37 29
26 32 28 18
20 6 66 74 76 89
![Page 67: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/67.jpg)
Trace of heapsort
39
37 29
26 32 28 18
20 6 66 74 76 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 68: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/68.jpg)
Trace of heapsort
6
37 29
26 32 28 18
20 39 66 74 76 89
Step 2: sift first element down
![Page 69: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/69.jpg)
Trace of heapsort
37
6 29
26 32 28 18
20 39 66 74 76 89
Step 2: sift first element down
![Page 70: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/70.jpg)
Trace of heapsort
37
32 29
26 6 28 18
20 39 66 74 76 89
Step 2: sift first element down
![Page 71: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/71.jpg)
Trace of heapsort
37
32 29
26 6 28 18
20 39 66 74 76 89
![Page 72: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/72.jpg)
Trace of heapsort
37
32 29
26 6 28 18
20 39 66 74 76 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 73: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/73.jpg)
Trace of heapsort
20
32 29
26 6 28 18
37 39 66 74 76 89
Step 2: sift first element down
![Page 74: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/74.jpg)
Trace of heapsort
32
20 29
26 6 28 18
37 39 66 74 76 89
Step 2: sift first element down
![Page 75: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/75.jpg)
Trace of heapsort
32
26 29
20 6 28 18
37 39 66 74 76 89
Step 2: sift first element down
![Page 76: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/76.jpg)
Trace of heapsort
32
26 29
20 6 28 18
37 39 66 74 76 89
![Page 77: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/77.jpg)
Trace of heapsort
32
26 29
20 6 28 18
37 39 66 74 76 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 78: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/78.jpg)
Trace of heapsort
18
26 29
20 6 28 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 79: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/79.jpg)
Trace of heapsort
29
26 18
20 6 28 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 80: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/80.jpg)
Trace of heapsort
29
26 28
20 6 18 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 81: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/81.jpg)
Trace of heapsort
29
26 28
20 6 18 32
37 39 66 74 76 89
![Page 82: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/82.jpg)
Trace of heapsort
29
26 28
20 6 18 32
37 39 66 74 76 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 83: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/83.jpg)
Trace of heapsort
18
26 28
20 6 29 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 84: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/84.jpg)
Trace of heapsort
28
26 18
20 6 29 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 85: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/85.jpg)
Trace of heapsort
28
26 18
20 6 29 32
37 39 66 74 76 89
![Page 86: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/86.jpg)
Trace of heapsort
28
26 18
20 6 29 32
37 39 66 74 76 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 87: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/87.jpg)
Trace of heapsort
6
26 18
20 28 29 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 88: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/88.jpg)
Trace of heapsort
26
6 18
20 28 29 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 89: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/89.jpg)
Trace of heapsort
26
20 18
6 28 29 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 90: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/90.jpg)
Trace of heapsort
26
20 18
6 28 29 32
37 39 66 74 76 89
![Page 91: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/91.jpg)
Trace of heapsort
26
20 18
6 28 29 32
37 39 66 74 76 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 92: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/92.jpg)
Trace of heapsort
6
20 18
26 28 29 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 93: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/93.jpg)
Trace of heapsort
20
6 18
26 28 29 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 94: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/94.jpg)
Trace of heapsort
20
6 18
26 28 29 32
37 39 66 74 76 89
![Page 95: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/95.jpg)
Trace of heapsort
20
6 18
26 28 29 32
37 39 66 74 76 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 96: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/96.jpg)
Trace of heapsort
18
6 20
26 28 29 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 97: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/97.jpg)
Trace of heapsort
18
6 20
26 28 29 32
37 39 66 74 76 89
![Page 98: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/98.jpg)
Trace of heapsort
18
6 20
26 28 29 32
37 39 66 74 76 89
Step 1: swap maximum and last element; decrease size of heap by 1
![Page 99: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/99.jpg)
Trace of heapsort
6
18 20
26 28 29 32
37 39 66 74 76 89
Step 2: sift first element down
![Page 100: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/100.jpg)
Trace of heapsort
6
18 20
26 28 29 32
37 39 66 74 76 89
Done!
![Page 101: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/101.jpg)
Building a heap
The first element is already a one-element heap● Sift element two up – now elements one and two form a
valid heap● Sift element three up – now the first three elements form a
valid heap● …and so on
Each sift is O(log n) – so total O(n log n)
In code:
for (int i = 1; i < n; i++) siftUp(i);
![Page 102: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/102.jpg)
Building a heap
Better approach: instead of looping forwards through the array and sifting up, loop backwards and sift down:for (int i = n / 2; i > 0; i--) siftDown(i-1);
Gives O(n) instead of O(n log n) complexity! (See book 21.3)
![Page 103: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/103.jpg)
Complexity of heapsort
Building the heap: O(n)
Then does n deleteMins, each O(log n) complexity
Total: O(n log n)
![Page 104: Priority queues, binary heaps, and heapsort (6.9, 21.1 ... › edu › year › 2013 › course › DIT960_Data_stru… · Binary heap A binary heap is a complete binary tree that](https://reader033.vdocument.in/reader033/viewer/2022060506/5f1ebd8098cc3b45d95e5395/html5/thumbnails/104.jpg)
Summary
Priority queues: insert, findMin, deleteMin
Binary heaps: O(log n) insert, O(1) findMin, O(log n) deleteMin● A binary tree with the heap property, represented as an array
Heapsort: build a max heap, repeatedly remove last element and place at end of array● Can be done in-place, O(n log n)
In fact, heaps were originally invented for heapsort!