Presentation is loading. Please wait.

Presentation is loading. Please wait.

HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.

Similar presentations


Presentation on theme: "HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key."— Presentation transcript:

1 HEAPS

2 Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key value, depending on application.). What data structures may be used to implement a priority queue? What are the advantages/disadvantages of each? Arrays: removal quick but insertion slow O(log n) to find position but O(n) for shifts Linked lists: removal quick but insertion slow - O(n) to find position and O(1) to relink.

3 Def. Heap - a binary tree that is: completely filled in reading from left to right across each level, except the last level may not be full, and the key in each node is greater than (or equal to) both of its children’s keys. Note Since the greatest key is always at the top, a heap is a good data structure to implement a priority queue. Although removal is only O(log n) time, insertion is also O(log n) - better than arrays and linked lists.

4 Note: The highest priority key is always accessible at the top, but removing it produces a hole, which takes the same no. of moves as the height of the tree to fill.

5 Although we visualize a heap as a linked binary tree, we usually implement it as an array. 0 1 2 3 4 5 6 7 8 9 10 11 100 75 5060 4045 30 60 40 45 30 100 75 50 55 15 20 35 25 55 1520 35 25

6 Remember the children of node n are stored in locations: 2n + 1 and 2n + 2 if the first object is stored in location zero in the array but in 2n and 2n+1 when the first object is stored in location one in the array and the first location is left empty. Question: what is the location of the parent of the child at location x? (x - 1)/2 or x/2 if zero is left empty Question: does a heap provide easy searching?no Also, a heap does not provide easy traversal of nodes in order. But, a heap provides easy removal of the highest key and easy insertion - both O(log n) using the “trickleup()” and “trickledown()” methods - all that is needed to implement a priority queue.

7 “trickledown()” method needed for removal: After copying node with highest priority, replace it with the last node in the heap. Now the tree is not a heap! so re-heap Swap the new root node with the greater of its children. Repeat that process down to the lowest level. Remove the root node from the previous sample.

8 Num 100 60 40 45 30 75 50 55 15 20 35 25 25

9 60 40 45 30 25 50 55 15 20 35 25 75 Num 100

10 25 40 45 30 60 50 55 15 20 35 25 75 Num 100

11 55 40 45 30 60 50 25 15 20 35 25 75 Num 100 0 1 2 3 4 5 6 7 8 9 10 11 7560 5055 40 45 3025 1520 3525 NOTE: the last item is not really removed, only the length of the array is decremented.

12 public Node remove() // delete item with max key { // (assumes non-empty list) Node root = heapArray[0]; heapArray[0] = heapArray[--currentSize]; trickleDown(0); return root; } // end remove()

13 public void trickleDown(int index) { int largerChild; Node top = heapArray[index]; // save root while(index < currentSize/2) // while node has at { // least one child, int leftChild = 2*index+1; int rightChild = leftChild+1; // find larger child if(rightChild < currentSize && // (rightChild exists?) heapArray[leftChild].iData < heapArray[rightChild].iData) largerChild = rightChild; else // what is passed to index? 0

14 largerChild = leftChild; // top >= largerChild? if(top.iData >= heapArray[largerChild].iData) break; // shift child up heapArray[index] = heapArray[largerChild]; index = largerChild; // go down } // end while heapArray[index] = top; // root to index } // end trickleDown()

15 “trickleup()” method is needed for insertion: Insert new node at bottom of tree (at heap[heap.length] then increment length. Compare with parent and swap if needed. Repeat until root node is reached.

16 55 40 45 30 60 50 25 15 20 35 75 90

17 55 40 90 30 60 50 25 15 20 35 75 45

18 55 40 50 30 60 90 25 15 20 35 75 45

19 55 40 50 30 60 75 25 15 20 35 90 45

20 ------------------------------------------------------------- public boolean insert(int key) { if(currentSize==maxSize) return false; Node newNode = new Node(key); heapArray[currentSize] = newNode; trickleUp(currentSize++); return true; } // end insert()

21 public void trickleUp(int index) { int parent = (index-1) / 2; Node bottom = heapArray[index]; while( index > 0 && heapArray[parent].iData < bottom.iData ) { heapArray[index] = heapArray[parent]; // move it down index = parent; parent = (parent-1) / 2; } // end while heapArray[index] = bottom; } // end trickleUp()

22 Note: trickledown() and trickleup() require the most work for removal and insertion, but that is never more than the no. of levels of the tree. no.node min no.levels height 123456789123456789 10 11 12 13 14 15 16 12233334444444451223333444444445 01122223333333340112222333333334 Note: height = log 2 n =(int) log 2 n No. levels = height + 1

23 Heapsort - another use of the adt heap. The heap provides a simple and efficient sorting algorithm: Insert items from an unordered array into a heap then Remove from the heap and replace in the original array: for (j = 0; j < size; j++) theHeap.insert( anArray[j]); for (j = 0; j < size; j++) anArray[j] = theHeap.remove(); Since insert and remove operate in O(log n) time and each must be executed for every node in the array (n) the heapsort operates in O(n*log n ) time. What other sorts run in that time? Quicksort & mergesort

24 Question: what is the disadvantage of the heapsort? Space requirement: 2 arrays needed This heapsort algorithm space requirement can be improved by placing the random data into a heap within the same array then: 1. Remove top and hold temporarily. 2. Trickledown() value that was put in place of top. 3. Copy temp to position of last value that had been put in place of top. Question: In what order is the data after this heapsort? Answer: ascending order.

25 To convert an array of random data to a heap within the array itself you may use an iterative approach or a recursive method. Iteratively this would be: for (j = size/2 - 1; j >= 0; j--) theHeap.trickleDown(j) Where in the binary tree is the item with index size/2 - 1? The rightmost node that has children. (Remember: the number of leaves = number of internal nodes +1.) Starting there to convert the random array into a heap with the array itself, trickledown is called about half as many times as it is in the insert() method.

26 4560 5075 55 40 0 1 2 3 4 5 Random array: Start trickledown here 4560 4075 55 50 4575 5060 55 40 (only 1 swap needed this time) 4575 5060 55 40

27 7545 5060 55 40 The array is now a heap. 4575 5060 55 40 0 1 2 3 4 5 7560 5045 55 40

28 7560 5045 55 40 Use the remove() method and insert() at bottom to sort within the array itself. Now trickledown() 6040 5045 55 75 4060 5045 55 75 0 1 2 3 4 5 6055 5045 40 75 Remember remove decrements heap size.

29 6055 5045 40 75 Remove biggest and insert at bottom (of what is left of heap) 4055 5045 60 75 trickledown() 5540 5045 60 75 4045 5055 60 75 Remove biggest and insert at bottom (of what is left of heap) 5545 5040 60 75 trickledown() 5045 4055 60 75

30 5045 4055 60 75 Remove biggest and insert at bottom (of what is left of heap) 4045 5055 60 75 trickledown() 4540 5055 60 75 Remove biggest and insert at bottom (of what is left of heap) 4045 5055 60 75 No trickledown() needed


Download ppt "HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key."

Similar presentations


Ads by Google