![Page 1: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/1.jpg)
CS165: Priority Queues, Heaps
CS165 - Priority Queues 1
Sudipto Ghosh, Wim Bohm
![Page 2: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/2.jpg)
Priority Queues
n Characteristicsq Items are associated with a Comparable value: priorityq Provide access to one element at a time - the one with
the highest priority
n offer(E e) and add(E e) – inserts the element into the priority queue based on the priority order
n remove() and poll() – removes the head of the queue (which is the highest priority) and returns it
CS165 - Priority Queues 2
![Page 3: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/3.jpg)
PQ – Linked List Implementation
n Reference-based implementationq Sorted in descending order
n Highest priority value is at the beginning of the linked listn remove() returns the item that pqHead references and
changes pqHead to reference the next item.n offer(E e) must traverse the list to find the correct
position for insertion.
9699.2 95.8 3
pqHead …
CS165 - Priority Queues 3
![Page 4: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/4.jpg)
Complete tree definitionn Complete binary tree of height h
q zero or more rightmost leaves not present at level h
n A binary tree T of height h is complete ifq All nodes at level h – 2 and above have
two children each, andq When a node at level h – 1 has children,
all nodes to its left at the same level have two children each, and
q When a node at level h - 1 has one child, it is a left child
q So the leaves at level h go from left to right
4CS165 - Priority Queues
h-2:
h-1:
h:
![Page 5: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/5.jpg)
Complete Binary Tree
5
Level-by-level numbering of a complete binary tree, NOTE 0 based!
0:Jane
1:Bob 2:Tom
3:Alan 4:Ellen 5:Nancy
What is the parentchild index relationship?
CS165 - Priority Queues
left child i: at 2*i+1
right child i: at 2*(i+1)
parent i: at (i-1)/2
There are no “holes” (missing nodes in the complete binary tree),so we can store a complete binary tree in an array!!
![Page 6: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/6.jpg)
Heap - Definitionn A maximum heap (maxheap) is a complete
binary tree that satisfies the following:q Nodes are (key,value) pairsq It has the heap property:
n Its root contains a key greater or equal to the keys of its children
n Its left and right sub-trees are also maxheaps
n A size 1 heap is just one leaf.
q A minheap has the root less or equal children, and left and right sub trees are also minheaps
CS165 - Priority Queues 6
![Page 7: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/7.jpg)
maxHeap Property Implications
n Implications of the heap property:q The root holds the maximum value (global property)q Values in descending order on every path from root to
leaf
n A Heap is NOT a binary search tree, as in a BST the nodes in the right sub tree of the root are larger than the root
CS165 - Priority Queues 7
![Page 8: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/8.jpg)
Examples
Satisfies heap property AND Complete
Satisfies heap property BUTNot complete
Does not satisfy heapproperty ANDNot complete
50
2520
10 15 5
30
255
10
15
20
30
2015
10 5 25
CS165 - Priority Queues 8
![Page 9: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/9.jpg)
Array(List) Implementation
50
2520
10 15 5
50202510155
0
1
2
3
4
5
CS165 - Priority Queues 9
![Page 10: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/10.jpg)
Array(List) Implementation
n Traversal:q Root at position 0q Left child of position i at position 2*i+1q Right child of position i at position 2*(i+1)q Parent of position i at position (i-1)/2
(don’t forget: int arithmetic truncates)
CS165 - Priority Queues 10
![Page 11: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/11.jpg)
Heap Operations - heapInsertn Step 1: put a new value into first open position
(maintaining completeness), i.e. at the end
n But now we potentially violated the heap property, so:
n Step 2: bubble values up
q Re-enforcing the heap property
q Swap with parent, if key of new value > key of parent, until in the right place.
q The heap property holds for the tree below the new value, when swapping up
CS165 - Priority Queues 11
![Page 12: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/12.jpg)
Swapping up
n Swapping up enforces heap property for sub tree below the new, inserted value:
n if (new > x) swap(x,new)x>y, thereforenew > y
CS165 - Priority Queues 12
xnewy
newx y
![Page 13: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/13.jpg)
Insertion into a heap (Insert 15)
9
65
3 2 15
Insert 15
CS165 - Priority Queues 13
bubble up
![Page 14: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/14.jpg)
Insertion into a heap (Insert 15)
9
6
5
3 2
CS165 - Priority Queues 14
15
bubble up
![Page 15: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/15.jpg)
Insertion into a heap (Insert 15)
15
6
5
3 2
CS165 - Priority Queues 15
9
![Page 16: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/16.jpg)
Heap operations – heapDelete
n Step 1: remove value at root (Why?)n Step 2: substitute with rightmost leaf of bottom level
(Why?)n Step 3: bubble down
q Swap with maximum child as necessary, until in placeq each bubble down restores the heap property at the
swapped node
q this is called HEAPIFY
CS165 - Priority Queues 16
![Page 17: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/17.jpg)
Swapping down
n Swapping down enforces heap property at the swap location:
n new<x and y<x:swap(x,new)
x>y and x>new
CS165 - Priority Queues 17
new
xyx
new y
![Page 18: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/18.jpg)
Deletion from a heap
5
9
3 2
10
6
Delete 10Place last node in root
CS165 - Priority Queues 18
![Page 19: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/19.jpg)
9
5
3 2
6
CS165 - Priority Queues 19
bubble downheapifydraw the heap
![Page 20: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/20.jpg)
5
9
3 2
6
CS165 - Priority Queues 20
delete againdraw the heap
![Page 21: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/21.jpg)
CS165 - Priority Queues 21
5
6
3
25
2
3
6
![Page 22: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/22.jpg)
HeapSort
n Algorithmq Insert all elements (one at a time) to a heapq Iteratively delete them
n Removes minimum/maximum value at each step
CS165 - Priority Queues 22
![Page 23: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/23.jpg)
HeapSort
n Alternative method (in-place):q buildHeap: create a heap out of the input array:
n Consider the input array as a complete binary treen Create a heap by iteratively expanding the portion of the
tree that is a heap q Leaves are already heapsq Start at last internal nodeq Go backwards calling heapify with each internal node
q Iteratively swap the root item with last item in unsorted portion and rebuild
CS165 - Priority Queues 23
![Page 24: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/24.jpg)
Building the heap
n WHY start at (n-2)/2?n WHY go backwards?
n The whole method is called buildHeapn One bubble down is called heapify
buildheap(n){for (i = (n-2)/2 down to 0) //pre: the tree rooted at index is a semiheap//i.e., the sub trees are heapsheapify(i); // bubble down//post: the tree rooted at index is a heap
}
CS165 - Priority Queues 24
![Page 25: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/25.jpg)
CS165 - Priority Queues 25
6
3 7
109 2
6 3 7 9 2 10
Draw as a Complete Binary Tree:
Repeatedly heapify, starting at last internal node, going backwards
![Page 26: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/26.jpg)
CS165 - Priority Queues 26
6
3 10
79 2
![Page 27: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/27.jpg)
CS165 - Priority Queues 27
6
9 10
73 2
![Page 28: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/28.jpg)
CS165 - Priority Queues 28
10
9 7
63 2
10 9 7 3 2 6
![Page 29: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/29.jpg)
In place heapsort using an arrayn First build a heap out of an input array using
buildHeap(). See previous slides.n Then partition the array into two regions; starting
with the full heap and an empty sorted and stepwise growing sorted and shrinking heap.
29
HEAPSorted (Largest elements in array)
CS165 - Priority Queues
![Page 30: CS165: Priority Queues, Heapscs165/.Spring20/slides/11-PrioQsHeaps.pdfHeap Operations -heapInsert n Step 1: put a new value into first open position (maintaining completeness), i.e](https://reader036.vdocument.in/reader036/viewer/2022070722/5f01c06d7e708231d400dd1e/html5/thumbnails/30.jpg)
30
10 9 6 3 2 59 5 6 3 2 10
5 3 2 1096
6 5 2 3 9 10
3 2 10965
2 3 10965
2 3 10965
HEAP
SORTED
Do it, do it
CS165 - Priority Queues