Ch15 Heap

2,210 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,210
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
110
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Ch15 Heap

  1. 1. Chapter 15 Heaps
  2. 2. Chapter Objectives <ul><li>Define a heap abstract data structure </li></ul><ul><li>Demonstrate how a heap can be used to solve problems </li></ul><ul><li>Examine various heap impmentations </li></ul><ul><li>Compare heap implementations </li></ul>
  3. 3. Heaps <ul><li>A heap is a binary tree with two added properties: </li></ul><ul><ul><li>It is a complete tree </li></ul></ul><ul><ul><li>For each node, the node is less than or equal to both the left child and the right child </li></ul></ul><ul><li>This definition described a minheap </li></ul>
  4. 4. Heaps <ul><li>In addition to the operations inherited from a binary tree, a heap has the following additional operations: </li></ul><ul><ul><li>addElement </li></ul></ul><ul><ul><li>removeMin </li></ul></ul><ul><ul><li>findMin </li></ul></ul>
  5. 5. FIGURE 15.1 The operations on a heap
  6. 6. Listing 15.1
  7. 7. FIGURE 15.2 UML description of the HeapADT
  8. 8. The addElement Operation <ul><li>The addElement method adds a given element to the appropriate location in the heap </li></ul><ul><li>A binary tree is considered complete if all of the leaves are level h or h-1 where h = log 2 n and n is the number of elements in the tree </li></ul>
  9. 9. The addElement Operation <ul><li>Since a heap is a complete tree, there is only one correct location for the insertion of a new node </li></ul><ul><ul><li>Either the next open position from the left at level h </li></ul></ul><ul><ul><li>Or the first position in level h+1 if level h is full </li></ul></ul>
  10. 10. The addElement Operation <ul><li>Once we have located the new node in the proper position, then we must account for the ordering property </li></ul><ul><li>We simply compare the new node to its parent value and swap the values if necessary </li></ul><ul><li>We continue this process up the tree until either the new value is greater than its parent or the new value becomes the root of the heap </li></ul>
  11. 11. FIGURE 15.3 Two minheaps containing the same data
  12. 12. FIGURE 15.4 Insertion points for a heap
  13. 13. FIGURE 15.5 Insertion and reordering in a heap
  14. 14. The removeMin Operation <ul><li>The removeMin method removes the minimum element from the heap </li></ul><ul><li>The minimum element is always stored at the root </li></ul><ul><li>Thus we have to return the root element and replace it with another element </li></ul>
  15. 15. The removeMin Operation <ul><li>The replacement element is always the last leaf </li></ul><ul><li>The last leaf is always the last element at level h </li></ul>
  16. 16. FIGURE 15.6 Examples of the last leaf in a heap
  17. 17. The removeMin Operation <ul><li>Once the element stored in the last leaf has been moved to the root, the heap will have to reordered </li></ul><ul><li>This is accomplished by comparing the new root element to the smaller of its children and the swapping them if necessary </li></ul><ul><li>This process is repeated down the tree until the element is either in a leaf or is less than both of its children </li></ul>
  18. 18. FIGURE 15.7 Removal and reordering in a heap
  19. 19. Using Heaps: Heap Sort <ul><li>Given the ordering property of a heap, it is natural to think of using a heap to sort a list of objects </li></ul><ul><li>One approach would be to simply add all of the objects to a heap and then remove them one at a time in ascending order </li></ul>
  20. 20. Using Heaps: Heap Sort <ul><li>Insertion into a heap is O(log n) for any given node and thus would O(n log n) for n nodes to build the heap </li></ul><ul><li>However, it is also possible to build a heap in place using an array </li></ul><ul><li>Since we know the relative position of each parent and its children in the array, we simply start with the first non-leaf node in the array, compare it to its children and swap if necessary </li></ul>
  21. 21. Using Heaps: Heap Sort <ul><li>However, it is also possible to build a heap in place using an array </li></ul><ul><li>Since we know the relative position of each parent and its children in the array, we simply start with the first non-leaf node in the array, compare it to its children and swap if necessary </li></ul>
  22. 22. Using Heaps: Heap Sort <ul><li>We then work backward in the array until we reach the root </li></ul><ul><li>Since at most, this will require us to make two comparisons for each non-leaf node, this approach is O(n) to build the heap </li></ul><ul><li>We will revisit the analysis of HeapSort at the end of the chapter </li></ul>
  23. 23. Using Heaps: Priority Queue <ul><li>A priority queue is a collection that follows two ordering rules: </li></ul><ul><ul><li>Items which have higher priority go first </li></ul></ul><ul><ul><li>Items with the same priority use a first in, first out method to determine their ordering </li></ul></ul><ul><li>A priority queue could be implemented using a list of queues where each queue represents items of a given priority </li></ul>
  24. 24. Using Heaps: Priority Queue <ul><li>Another solution is to use a minheap </li></ul><ul><li>Sorting the heap by priority accomplishes the first ordering </li></ul><ul><li>However, the first in, first out ordering for items with the same priority has to be manipulated </li></ul>
  25. 25. Using Heaps: Priority Queue <ul><li>The solution is to create a PriorityQueueNode object that stores the element to be placed on the queue, the priority of the element and the arrival order of the element </li></ul><ul><li>Then we simply define a compareTo method for the PriorityQueueNode class that first compares priority then arrival time </li></ul><ul><li>The PriorityQueue class then extends the Heap class and stores PriorityQueueNodes </li></ul>
  26. 26. Listing 15.2
  27. 27. Listing 15.2 (cont.)
  28. 28. Listing 15.2 (cont.)
  29. 29. Listing 15.2 (cont.)
  30. 30. Listing 15.3
  31. 31. Listing 15.3 (cont.)
  32. 32. Listing 15.3 (cont.)
  33. 33. Implementing Heaps with Links <ul><li>A linked implementation of a minheap would simply be an extension of our LinkedBinaryTree class </li></ul><ul><li>However, since we need each node to have a parent pointer, we will create a HeapNode class to extend our BinaryTreeNode class we used earlier </li></ul>
  34. 34. Listing 15.4
  35. 35. Implementing Heaps with Links <ul><li>The addElement method must accomplish three tasks: </li></ul><ul><ul><li>Add the new node at the appropriate location </li></ul></ul><ul><ul><li>Reorder the heap </li></ul></ul><ul><ul><li>Reset the lastNode pointer to point to the new last node </li></ul></ul>
  36. 36. LinkedHeap - the addElement Operation
  37. 37. LinkedHeap - the addElement Operation
  38. 38. LinkedHeap - the addElement Operation <ul><li>The addElement operation makes use of two private methods: </li></ul><ul><ul><li>getNextParentAdd that returns a reference to the node that will be the parent of the new node </li></ul></ul><ul><ul><li>heapifyAdd that reorders the heap after the insertion </li></ul></ul>
  39. 39. LinkedHeap - the addElement Operation
  40. 40. LinkedHeap - the addElement Operation
  41. 41. LinkedHeap - the removeMin Operation <ul><li>The removeMin operation must accomplish three tasks: </li></ul><ul><ul><li>Replace the element stored in the root with the element stored in the last leaf </li></ul></ul><ul><ul><li>Reorder the heap if necessary </li></ul></ul><ul><ul><li>Return the original root element </li></ul></ul>
  42. 42. LinkedHeap - the removeMin Operation
  43. 43. LinkedHeap - the removeMin Operation
  44. 44. LinkedHeap - the removeMin Operation <ul><li>Like the addElement operation, the removeMin operation makes use of two private methods </li></ul><ul><ul><li>getNewLastNode that returns a reference to the new last node in the heap </li></ul></ul><ul><ul><li>heapifyRemove that reorders the heap after the removal </li></ul></ul>
  45. 45. LinkedHeap - the removeMin Operation
  46. 46. LinkedHeap - the removeMin Operation
  47. 47. LinkedHeap - the removeMin Operation
  48. 48. Implementing Heaps with Arrays <ul><li>An array implementation of a heap may provide a simpler alternative </li></ul><ul><li>In an array implementation, the location of parent and child can always be calculated </li></ul><ul><li>Given that the root is in postion 0, then for any given node stored in position n of the array, its left child is in position 2n + 1 and its right child is in position 2(n+1) </li></ul><ul><li>This means that its parent is in position (n-1)/2 </li></ul>
  49. 49. Implementing Heaps with Arrays <ul><li>Like the linked version, the addElement operation for an array implementation of a heap must accomplish three tasks: </li></ul><ul><ul><li>Add the new node, </li></ul></ul><ul><ul><li>Reorder the heap, </li></ul></ul><ul><ul><li>Increment the count by one </li></ul></ul><ul><li>The ArrayHeap version of this method only requires one private method, heapifyAdd which reorders the heap after the insertion </li></ul>
  50. 50. ArrayHeap - the addElement Operation
  51. 51. ArrayHeap - the addElement Operation
  52. 52. ArrayHeap - the removeMin Operation <ul><li>The removeMin operation must accomplish three tasks: </li></ul><ul><ul><li>Replace the element stored at the root with the element stored in the last leaf </li></ul></ul><ul><ul><li>Reorder the heap as necessary </li></ul></ul><ul><ul><li>Return the original root element </li></ul></ul><ul><li>Like the addElement operation, the removeMin operation makes use of a private method, heapifyRemove to reorder the heap </li></ul>
  53. 53. ArrayHeap - the removeMin Operation
  54. 54. ArrayHeap - the removeMin Operation
  55. 55. ArrayHeap - the removeMin Operation
  56. 56. Analysis of Heap Implementations <ul><li>The addElement operation is O(log n) for both implementations </li></ul><ul><li>The removeMin operation is O(log n) for both implementations </li></ul><ul><li>The findMin operation is O(1) for both implementations </li></ul>
  57. 57. Analysis of Heap Implementations <ul><li>One might conclude then that HeapSort is O(log n) since both adding and removing elements is O(log n) </li></ul><ul><li>Keep in mind that for HeapSort, we must perform both operations for each of n elements </li></ul><ul><li>Thus the time complexity of HeapSort is 2*n*log n or O(n log n) </li></ul>

×