1 / 31

CSCI 2342 Data Structures and Algorithms II Dr. Tami Meredith

CSCI 2342 Data Structures and Algorithms II Dr. Tami Meredith. Lecture 3 : Heaps (Chapter 17). The Heap ADT. Definition A heap is a complete binary tree that either is: empty, or whose root Contains a value greater (less) than or equal to the value in each of its children, and

rodd
Download Presentation

CSCI 2342 Data Structures and Algorithms II Dr. Tami Meredith

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 2342Data Structures and Algorithms IIDr. Tami Meredith Lecture 3: Heaps (Chapter 17)

  2. The Heap ADT • Definition • A heap is a complete binary tree that either is: • empty, or • whose root • Contains a value greater (less) than or equal to the value in each of its children, and • Has heaps as its subtrees • Idea: complete tree where each level contains values less than or equal to those of the previous level • NOT related to heap memory (totally different)

  3. FIGURE 17-1 (a) A maxheap and (b) a minheap The Heap ADT

  4. FIGURE 17-2 UML diagram for the class Heap The Heap ADT

  5. FIGURE 17-3 (a) Level-by-level numbering of a complete binary tree (b) its array-based implementation Array-Based Heap Implementation

  6. Array Implementation For node[i]: left child = node[2*i+1] right child = node[2*i+2] parent = node[(i-1)/2]

  7. Using a Heap • Heaps are used for data where we access it from max to min (or in the reverse) • They are somewhat like a sorted tree • Retrieval/Access: Almost always we want the largest value in the heap (i.e., the root) • Removal: We almost always remove the largest value in the heap (i.e., the root) • Adding: We add a value and have to rebuild the heap to maintain its ordering

  8. Removing an item • Remove the root item, thus leaving two unattached heaps (i.e., its left & right children) • Heaps are complete trees so we move the last item in the heap to form the new root – this is called a semi-heap • We swap the moved element with the largest of its children until it trickles down into its place

  9. FIGURE 17-4 (a) Disjoint heaps after removing the heap’s root Algorithms for Array-Based Heaps

  10. FIGURE 17-4 (b) a semiheap Removal from Array-Based Heaps

  11. FIGURE 17-4 (c) the restored heap Removal from Array-Based Heaps

  12. Removal from Array-Based Heaps

  13. Convert Semi-Heap to Heap heapRebuild (root: integer, items: ArrayType, itemCount:integer) if (the root is not a leaf) { // The root must have a left child; assume it is the larger child largerChildIndex = 2 * rootIndex + 1 if (the root has a right child) { rightChildIndex = largerChildIndex + 1 if (items[rightChildIndex] > items[largerChildIndex]) largerChildIndex = rightChildIndex } // If root is smaller than larger child, swap items if (items[rootIndex] < items[largerChildIndex]) { Swap items[rootIndex] and items[largerChildIndex] heapRebuild (largerChildIndex, items, itemCount) } } // Else root is a leaf, so you are done

  14. FIGURE 17-6 Recursive calls to heapRebuild Algorithms for Array-Based Heaps

  15. Inserting an item • Heaps are complete trees so we add the new item to the last (bottom-right) node • We swap the new element with its parent while it is greater than its parent until it bubbles up into place

  16. FIGURE 17-7 Insertion into a heap Adding to Array-Based Heaps

  17. Adding to Array-Based Heaps • Pseudocode for add

  18. Heap Construction • Building a heap from an array of data

  19. FIGURE 17-8 (a) The initial contents of an array (b) the array’s corresponding complete binary tree Heap Construction

  20. FIGURE 17-9 Transforming an array into a heap Heap Construction

  21. FIGURE 17-9 Transforming an array into a heap Heap Construction

  22. Implementation of Construction • Method heapCreate

  23. Heap Access • Method peekTop

  24. Heap Implementation of a Priority Queue • Using a heap to define a priority queue results in time-efficient implementation • Enqueue: Add an item to the heap • Dequeue: Peek then remove • Heaps always balanced (complete) so better than a BST if size is known

  25. Heap Sort • Basic Idea • Turn array into heap • Remove items from heap and return to array from back to front • More efficient implementations exist though if we use one array and have a heap part and a sorted part

  26. FIGURE 17-10 Heap sort partitions an array into two regions Heap Sort

  27. Heap Sort // Sorts anArray[0..n-1] heapSort (anArray: ArrayType, n:integer) { // Build initial heap for (index = n / 2 down to 0) { heapRebuild (index, anArray, n) } // Move the largest item from Heap part to sorted part Swap anArray[0] and anArray[n - 1] heapSize = n - 1 // Decrease size of the Heap region, expand the Sorted region while (heapSize > 1) { // Make the Heap region a heap again heapRebuild (0, anArray, heapSize) Swap anArray[0] and anArray[heapSize - 1] heapSize – } }

  28. FIGURE 17-11 A trace of heap sort beginning with the heap in Figure 17-9 Heap Sort

  29. FIGURE 17-11 A trace of heap sort, beginning with the heap in Figure 17-9 Heap Sort

  30. FIGURE 17-11 A trace of heap sort, beginning with the heap in Figure 17-9 Heap Sort

  31. FIGURE 17-11 A trace of heap sort, beginning with the heap in Figure 17-9 Heap Sort

More Related