Binary heaps and Heapsort
Priority Queues n The smallest element is removed item = remove();item = remove(); Priority Queue Minimum element is 5
Priority Queues n The smallest element is removed item = remove();item = remove(); Priority Queue Minimum element is 7
Priority Queues n Additions made wherever add(15);add(15); Priority Queue Minimum element is
List Representations n Represent P.Q. as a sorted list insert item into the appropriate position = O(N)insert item into the appropriate position = O(N) remove item at one end = O(1)remove item at one end = O(1) n Represent P.Q. as an unsorted list insert at one end of list = O(1)insert at one end of list = O(1) remove by finding minimum & deleting = O(N)remove by finding minimum & deleting = O(N) n Which to prefer?
List Representations n Sorted Rep Insert 14Insert 14 Remove-HighestRemove-Highest n Unsorted Rep
A Simpler Representation n Keep people semi-sorted by importance most important person right at frontmost important person right at front people near front are smallerpeople near front are smaller people near back are biggerpeople near back are bigger each person can have two people behindeach person can have two people behind »one to left, one to right n People arranged in rows: 1 in 1 st row, 2 in 2 nd row, 4 in 3 rd, 8 in 4 th, …1 in 1 st row, 2 in 2 nd row, 4 in 3 rd, 8 in 4 th, …
A “Heap” of People n Most important: 1 st row 2 nd row less important than 1 st2 nd row less important than 1 st 3 rd row less important than 2 nd3 rd row less important than 2 nd and so onand so on n Each person can have 2 more people behind the two people behind are less important (bigger) than the person in front of themthe two people behind are less important (bigger) than the person in front of them
Heap Order Property n Highest priority item at root here that’s the 5here that’s the 5 n Left & right children also heap ordered highest priority in that sub-tree at its roothighest priority in that sub-tree at its root here: 7 on left & 12 on righthere: 7 on left & 12 on right n Note: 5’s children are not next two smallest they could have been, but aren’t necessarilythey could have been, but aren’t necessarily
Duplicate Entries n Heaps allow duplicates n A highest priority item appears at root others of same priority will be top in their sub-treeothers of same priority will be top in their sub-tree duplicates may also appear in separate sub-treesduplicates may also appear in separate sub-trees
Starting the PQ n First person goes to front of “heap” n Second person arrives if smaller, goes to frontif smaller, goes to front »first person moves back to the left otherwise, new person goes to back and leftotherwise, new person goes to back and left n Third person arrives if smaller than FRONT person, goes to frontif smaller than FRONT person, goes to front »front person goes back and to the right otherwise, new person goes to back and rightotherwise, new person goes to back and right
Growing the PQ n As people arrive, they start in the back row as far left in that row as they can goas far left in that row as they can go if smaller than person immediately in front, the two exchange positionsif smaller than person immediately in front, the two exchange positions new person continues pushing forward until runs into someone smaller (more important)new person continues pushing forward until runs into someone smaller (more important) »or until reach front of PQ
New Person Joins the Heap n Insert requires adding a new element to the array & making the array back into a heap small (high priority) items must “percolate up”small (high priority) items must “percolate up” stop when parent is smaller…stop when parent is smaller… or reach rootor reach root
Storing Heaps in Arrays n Heap can be stored compactly in an array do a “level order” traversal of the tree into the arraydo a “level order” traversal of the tree into the array n Left child of i is in 2i n Right child of i is in 2i+1 n Parent of i is in i/2 n Leave location 0 empty! Note: array index starts at 1
Exercise n Store the following heap into an array
Exercise n Draw the complete “heap” represented by the following array: [2, 4, 9, 7, 13, 11, 20, 18, 23, 15, 14, 12, 16, 19][2, 4, 9, 7, 13, 11, 20, 18, 23, 15, 14, 12, 16, 19] n Is it actually a heap? if not, can you make it into a heap?if not, can you make it into a heap?
Insert item into Heap 1.Let N be the next available position in the array 2.While N > 1 & A[N/2] > item a)set A[N] to A[N/2] b)set N to N/2 3.Set A[N] to the new item 4.Add 1 to size of heap
Insertion Example n Insert 6 into this heap n N =
Insertion Example n Insert 6 into this heap n N 8 n A[N/2] = A[4] = 81 n 81 > 6 percolate up ?
Insertion Example n Insert 6 into this heap n N = 4 n A[N/2] = A[2] = 7 n 7 > 6 percolate up
Insertion Example n Insert 6 into this heap n N = 2 n A[N/2] = A[1] = 5 n 5 6 percolating done n Set A[N] = A[2] to
Exercise n Continuing the example: insert 14insert 14 insert 7insert 7 insert 4insert
Removing from a Heap n Front person leaves heap n How shall we fill the empty space? NOT by pushing forward into the open spaceNOT by pushing forward into the open space that might ruin our nice heapthat might ruin our nice heap n Instead we put the last person in the front(!) n Then people start pushing forward starting with the smaller of the people in the second row of the heapstarting with the smaller of the people in the second row of the heap
Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down
Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down choose smaller of two childrenchoose smaller of two children
Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down choose smaller of two childrenchoose smaller of two children
Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down choose smaller of two childrenchoose smaller of two children (if it has two children)(if it has two children)
Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down choose smaller of two childrenchoose smaller of two children (if it has two children)(if it has two children)
Percolating Down n Check number of children if none – then we are done percolatingif none – then we are done percolating if one – it is the smallest childif one – it is the smallest child if two – choose whichever is smallerif two – choose whichever is smaller n If smaller child is smaller than percolator move the child upmove the child up continue percolating from the child’s positioncontinue percolating from the child’s position
Percolating Down 1.Let P be the last position in the heap 2.Let N be 1 3.If 2N<P (* has surviving children *) a)If 2N+1=P (* one child *), let S be 2N b)Else let S be (A[2N]<A[2N+1]) ? 2N : 2N+1 c)If (A[P]>A[S]) i.let A[N] be A[S], N be S & go to step 3 4.Let A[N] be A[P] NOTE: P is the position we are deleting from
Deleting from the Heap 1.Set temp to A[1] 2.Percolate Down// from previous slide 3.Reduce the size of the heap by 1 4.Return temp
Exercise n Delete from this heap n Delete again from this heap n And again
Our Heap is Now a PQ n remove method always gets smallest item and leaves next smallest item in its placeand leaves next smallest item in its place n add method inserts new item anywhere but moves it up to front if it’s smallestbut moves it up to front if it’s smallest n We can add convenience methods look at front, check if empty, make empty, …look at front, check if empty, make empty, …
17 “Almost” Heap Sort n Can sort an array using our heap/PQ add each element of array to heapadd each element of array to heap remove items from heap back into the arrayremove items from heap back into the array n But we can do better! we can do it all in the arraywe can do it all in the array turn the array into a heapturn the array into a heap
Random Array to Heap n Need to get smaller items up & bigger items down percolate up or downpercolate up or down n Need to be sure we get everything to its level only items with children matteronly items with children matter have childrenhave no children
Random Array to Heap n Start with the last item with children n Percolate it down n Move on to the previous node stop at the rootstop at the root have childrenhave no children
Random Array to Heap n Item 5 is an 8 its one child (item 10) is a 14its one child (item 10) is a 14 it’s OKit’s OK n Item 4 is a 20 its smaller child (item 9) is a 13its smaller child (item 9) is a 13 swap themswap them now OKnow OK have childrenhave no children
Random Array to Heap n Item 3 is a 13 its smaller child is a 9its smaller child is a 9 swap & OKswap & OK n Item 2 is a 19 smaller child is 8smaller child is 8 »swap its smaller child is 14:its smaller child is 14: »swap again have childrenhave no children
Random Array to Heap n Item 1 is a 29 smaller child is 8smaller child is 8 »swap its smaller child is 13its smaller child is 13 »swap again its smaller child is 14its smaller child is 14 »swap again donedone have childrenhave no children
Exercise n Use the BuildHeap algorithm to build a heap from the following array [9, 86, 54, 92, 91, 38, 28, 87, 93, 84, 88, 24, 64, 96, 3, 18, 1, 72, 66, 8][9, 86, 54, 92, 91, 38, 28, 87, 93, 84, 88, 24, 64, 96, 3, 18, 1, 72, 66, 8]
Code for Building a Heap n Uses a modified version of percolate down say where to percolate down fromsay where to percolate down from To BuildHeap(Array A, int Length) i Length ÷ 2; while (i 1) PercolateDown(A, i, Length); i – –;
Code for Percolate Down To PercolateDown(Array A, int N, int L) T A[N]; while (2N L) if (2N = L)S 2N; elseS (A[2N]<A[2N+1]) ? 2N : 2N+1; if (A[S]<T)A[N] A[S], N S; elsebreak; A[N] T;
Building a Heap is O(N) n There will be less than N swaps establish an upper bound on the # of swapsestablish an upper bound on the # of swaps n Let k be the height of the tree n Worst case # of swaps: # of swaps for left sub-tree# of swaps for left sub-tree plus # of swaps for rightplus # of swaps for right plus k swaps for the rootplus k swaps for the root assume all leaves at same level (simplifies math)assume all leaves at same level (simplifies math)
Maximum Number of Swaps kNformulamax swapsN – k – * * * * Equality can be proven by induction on k
Heap Class n Operations Insert, FindMin, DeleteMinInsert, FindMin, DeleteMin IsEmpty, MakeEmptyIsEmpty, MakeEmpty n Constructors default (creates empty heap)default (creates empty heap) with vector (uses BuildHeap to make heap out of the vector)with vector (uses BuildHeap to make heap out of the vector)
Heap Sort n Turn array into a reverse heap bigger numbers at the frontbigger numbers at the front modified version of BuildHeapmodified version of BuildHeap n After you remove each item… n …put it at the back of the array which just became available!which just became available!
Heap Sort Example n Original array: n maxHeapify: n removeAll:
Questions?