Heaps
Heaps
Sometimes: we do not specify the priority // for example when implementing heap
This method will insert new item into the priority queue. We have to specify
the data we want to insert and the priority associated with the given data
getHighestPriorityElement()
Returns the element with highest priority: we have to reconstruct the heap
Max heap: returns maximum element
Min heap: returns minimum element
peek()
Returns the element with highest priority: the structure of the heap
does not change !!!
Sorting
20
3.) Min heap the parent is always smaller than the values of the
children
Max heap the parent is always greater
So: the root node will be the smallest/ greatest value in the heap
// O(1) access !!!
Binary heap: maximum heap
Binary heap: maximum heap
0
1 2
3 4 5 6
7 8
Represent heap as array
We assign indexes to every
node in the heap !!! 100 0
19 1
~ the index will be the index
in a one dimensional array 36 2
0
17 3
1 2
3 4
25 5
3 4 5 6 1 6
2 7
7 8
7 8
Represent heap as array
parent node
i 100 0
19 1
2i+1 2i+2 36 2
0
left right 17 3
1 2
child child 3 4
25 5
3 4 5 6 1 6
2 7
7 8
7 8
0
1
2
3
4
5
6
7
8
Building a heap:
Insert: 23
0
1
2
3
4
5
6
7
8
Building a heap
23
23 0
1
2
3
4
5
6
7
8
Building a heap
Insert: 5
23
23 0
1
2
3
4
5
6
7
8
Building a heap
23
5
23 0
5 1
2
3
4
5
6
7
8
Building a heap
Insert: 100
23
5
23 0
5 1
2
3
4
5
6
7
8
Building a heap
23
5 100
23 0
5 1
100 2
3
4
5
6
7
8
Building a heap
23
5 100
23 0
5 1
100 2
3
4
5
6
7
8
Building a heap
100
5 23
100 0
5 1
23 2
3
4
5
6
7
8
Building a heap
Insert: 2
100
5 23
100 0
5 1
23 2
3
4
5
6
7
8
Building a heap
100
5 23
100 0
5 1
2
23 2
2 3
4
5
6
7
8
Building a heap
Insert: 210
100
5 23
100 0
5 1
2
23 2
2 3
4
5
6
7
8
Building a heap
100
5 23
100 0
5 1
2 210
23 2
2 3
210 4
5
6
7
8
Building a heap
100
5 23
100 0
5 1
2 210
23 2
2 3
210 4
5
6
7
8
Building a heap
100
210 23
100 0
210 1
2 5
23 2
2 3
5 4
5
6
7
8
Building a heap
100
210 23
100 0
210 1
2 5
23 2
2 3
5 4
5
6
7
8
Building a heap
210
100 23
210 0
100 1
2 5
23 2
2 3
5 4
5
6
7
8
Building a heap
210 0
100 1
210
23 2
2 3
100 23 5 4
5
2 5 6
7
8
210
100 23
2 5
Deleting an item: we just get rid of the item we want to delete. OK, but there will be
a „hole” in the tree. So we put the last item there, and make
sure the heap properties are valid // with reconstructions !!!
210
deleteNode(210);
100 23
2 5
Deleting an item: we just get rid of the item we want to delete. OK, but there will be
a „hole” in the tree. So we put the last item there, and make
sure the heap properties are valid // with reconstructions !!!
210
100 23
2 5
Deleting an item: we just get rid of the item we want to delete. OK, but there will be
a „hole” in the tree. So we put the last item there, and make
sure the heap properties are valid // with reconstructions !!!
100 23
2 5
Deleting an item: we just get rid of the item we want to delete. OK, but there will be
a „hole” in the tree. So we put the last item there, and make
sure the heap properties are valid // with reconstructions !!!
100 23
2 5
100 23
100 23
100
5 23
100
5 23
So: we have managed to get rid of the root node and to make some
reconstructions in order to end up with a valid heap again !!!
Deleting an item: we just get rid of the item we want to delete. OK, but there will be
a „hole” in the tree. So we put the last item there, and make
sure the heap properties are valid // with reconstructions !!!
100
5 23
So: we have managed to get rid of the root node and to make some
reconstructions in order to end up with a valid heap again !!!
Operation: deleting the root node O(1) + reconstruction O(logN) = O(logN) !!!
Heapsort
210
100 23
2 5
100 23
2 5
210
100 23
2 5
Sorted order:
5
100 23
2 210
Sorted order:
5
After swapping with the root:
- we consider the last item to be
100 23 sorted: no longer part of the tree !!!
- check whether it is a valid heap or not
2 210
100 23
2 210
5 23
2 210
5 23
2 210
5 23
100 210
5 23
100 210
5 2
100 210
5 2
100 210
5 23
100 210
5 23
100 210
2 23
100 210
2 23
100 210
5 23
100 210
5 23
100 210
5 23
100 210
5 23
100 210
O(N*logN)
Running time
Insert new item: we can insert at the next available place, so increment the array
index and insert it O(1) fast
BUT we have to make sure the heap properties are met ...
it may take O(logN) time
O(log N) Why? Because a node has at most log N parents so at most log N swaps
2 are needed 2 2
Running time