310 likes | 423 Views
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
E N D
CSCI 2342Data Structures and Algorithms IIDr. 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 • 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)
FIGURE 17-1 (a) A maxheap and (b) a minheap The Heap ADT
FIGURE 17-2 UML diagram for the class Heap The Heap ADT
FIGURE 17-3 (a) Level-by-level numbering of a complete binary tree (b) its array-based implementation Array-Based Heap Implementation
Array Implementation For node[i]: left child = node[2*i+1] right child = node[2*i+2] parent = node[(i-1)/2]
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
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
FIGURE 17-4 (a) Disjoint heaps after removing the heap’s root Algorithms for Array-Based Heaps
FIGURE 17-4 (b) a semiheap Removal from Array-Based Heaps
FIGURE 17-4 (c) the restored heap Removal from Array-Based Heaps
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
FIGURE 17-6 Recursive calls to heapRebuild Algorithms for Array-Based Heaps
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
FIGURE 17-7 Insertion into a heap Adding to Array-Based Heaps
Adding to Array-Based Heaps • Pseudocode for add
Heap Construction • Building a heap from an array of data
FIGURE 17-8 (a) The initial contents of an array (b) the array’s corresponding complete binary tree Heap Construction
FIGURE 17-9 Transforming an array into a heap Heap Construction
FIGURE 17-9 Transforming an array into a heap Heap Construction
Implementation of Construction • Method heapCreate
Heap Access • Method peekTop
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
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
FIGURE 17-10 Heap sort partitions an array into two regions Heap Sort
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 – } }
FIGURE 17-11 A trace of heap sort beginning with the heap in Figure 17-9 Heap Sort
FIGURE 17-11 A trace of heap sort, beginning with the heap in Figure 17-9 Heap Sort
FIGURE 17-11 A trace of heap sort, beginning with the heap in Figure 17-9 Heap Sort
FIGURE 17-11 A trace of heap sort, beginning with the heap in Figure 17-9 Heap Sort