CS 302 - Data Structures Chapter 17 Heaps Mehmet H Gunes Modified from authors’ slides
Contents The ADT Heap An Array-Based Implementation of a Heap A Heap Implementation of the ADT Priority Queue Heap Sort Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The ADT Heap Definition A heap is a complete binary tree that either is empty … or … it’s root Contains a value greater than or equal to the value in each of its children, and Has heaps as its subtrees Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Complete Binary Tree (1) A binary tree that is either full or full through the next-to-last level (2) The last level is full from left to right (i.e., leaves are as far to the left as possible) Complete Binary Tree
What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: Its shape must be a complete binary tree. For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.
Are these Both Heaps? C A T treePtr 50 20 18 30 10
Is this a Heap? tree 70 60 12 40 30 8 10
Unique?
Where is the Largest Element in a Heap Always Found? tree 70 60 12 40 30 8
We Can Number the Nodes Left to Right by Level This Way tree 70 60 1 12 2 40 3 30 4 8 5
And use the Numbers as Array Indexes to Store the Heap tree.nodes [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 70 60 1 40 3 30 4 12 2 8 5 tree
With Array Representation For any node tree.nodes[index] its left child is in tree.nodes[index*2 + 1] right child is in tree.nodes[index*2 + 2] its parent is in tree.nodes[(index – 1)/2] Leaf nodes: tree.nodes[numElements/2] to tree.nodes[numElements - 1]
The ADT Heap (a) A maxheap and (b) a minheap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
View interface, Listing 17-1 The ADT Heap UML diagram for the class Heap View interface, Listing 17-1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Array-Based Implementation of a Heap (a) Level-by-level numbering of a complete binary tree; (b) its array-based implementation Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Algorithms for Array-Based Heap Operations (a) Disjoint heaps after removing the heap’s root; Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Algorithms for Array-Based Heap Operations (b) a semiheap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Algorithms for Array-Based Heap Operations (c) the restored heap View algorithm to make this conversion, Listing 17-A Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Reheap Down bottom
Algorithms for Array-Based Heap Operations The array representation the heap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Algorithms for Array-Based Heap Operations The array representation of the semiheap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Algorithms for Array-Based Heap Operations The array representation of the restored heap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Algorithms for Array-Based Heap Operations Recursive calls to heapRebuild Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Algorithms for Array-Based Heap Operations Insertion into a heap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Reheap Up bottom
Algorithms for Array-Based Heap Operations Pseudocode for add Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The Implementation The header file for class ArrayMaxHeap An array-based implementation of the ADT heap View Listing 17-2. This heap is a maxheap. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The Implementation Definition of constructor Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The Implementation (a) The initial contents of an array; (b) the array’s corresponding complete binary tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The Implementation Transforming an array into a heap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The Implementation Transforming an array into a heap Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Building a Heap: i = 4 i = 3 i = 2 2 14 8 1 16 7 4 3 9 10 2 14 8 1 16 5 6 2 14 8 1 16 7 4 3 9 10 5 6 14 2 8 1 16 7 4 3 9 10 5 6 i = 1 i = 0 14 2 8 1 16 7 4 10 9 3 5 6 14 2 8 16 7 1 4 10 9 3 5 6 8 2 4 14 7 1 16 10 9 3 5 6
The Implementation Method heapCreate Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
The Implementation Method peekTop Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Heap Implementation of the ADT Priority Queue Using a heap to define a priority queue results in a more time-efficient implementation Listing 17-3 contains a header file for a class of priority queues. View implementation, Listing 17-4 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Heap Sort Heap sort partitions an array into two regions View heap sort algorithm, Listing 17-B Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Heap Sort A trace of heap sort, beginning with the heap in Figure 17-9 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Heap Sort A trace of heap sort, beginning with the heap in Figure 17-9 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Heap Sort A trace of heap sort, beginning with the heap in Figure 17-9 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Heap Sort A trace of heap sort, beginning with the heap in Figure 17-9 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013