CIS210 1
Topic
Binary Heaps
CIS210 2
Binary Heaps
as Non-linear Data Structures
CIS210 3
A Binary Heap?
A binary tree that satisfies two conditions:
The shape condition
The (partial) order condition between every
node and the nodes in its left and right
subtrees.
Heap order property
CIS210 4
A Binary Heap
TrightTleft
root
+ +Heap
Order
Condition Shape
Condition
CIS210 5
1. The Shape Condition of Heaps
The binary tree must be a complete binary
tree.
CIS210 6
2. The Order Condition of Heaps
There is a necessary ordering relationship
between a node and its children.
Each node in a tree has a key which is more
extreme (greater or less) than or equal to the
key of its parent.
Note:
There is no necessary ordering relationship
between a node and its siblings!
CIS210 7
The Order Condition of Heaps
For every node in the binary tree, the value
stored in that node must be greater than or
equal to the value in each of its children.
A max-heap
For every node in the binary tree, the value
stored in that node must be less than or equal
to the value in each of its children.
A min-heap
CIS210 8
A Binary Max-Heap?
A special complete binary tree s.t.
The search key contained in any node is greater
(larger) than or equal to the search key in each of its
children.
The search key contained in any node is never less
(smaller) than the search key in each of its children.
Every level except the deepest must contain as many
nodes as possible, and at the deepest level, all the
nodes are as far left as possible.
The root node has the largest key!
CIS210 9
Logical Structure of Binary Max-Heaps
TrightTleft
root
CIS210 10
Example: Binary Max Heap?
10
9 6
3 2 5
10
9 6
3
10
9
CIS210 11
A Binary Min-Heap?
A special complete binary tree s.t.
The search key contained in any node is less (smaller)
than or equal to the search key in each of its children.
The search key contained in any node is never greater
(larger) than the search key in each of its children.
Every level except the deepest must contain as many
nodes as possible, and at the deepest level, all the
nodes are as far left as possible.
The root node has the smallest key!
CIS210 12
Logical Structure of Binary Min-Heaps
TrightTleft
root
CIS210 13
Example:
2
3 6
10 5 9
2
3 6
10
2
3
CIS210 14
The World of Binary Heaps
Binary Trees
Complete
Binary Trees
(Binary)
HeapsHeap-order
Property
Shape PropertyMin-Heaps
Max-Heaps
CIS210 15
How to Represent a Binary Heap?
A heap is a complete binary tree!
An efficient array-based representation
Represent a node in the complete binary tree
as
A data
Represent the complete binary tree as an array.
CIS210 16
An Array-Based Representation of a Binary Heap
0
1
2
3
4
5
.
.
.
Data
10
9
6
3
2
5
Root
Heap[0]
Heap
10
9 6
3 2 5
CIS210 17
An Array-Based Representation of a Binary Heap
const int MAX_NODES = 100;
typedef string DataType;
DataType Heap[MAX_NODES];
int Root;
CIS210 18
Finding Parent and Children in a Binary Heap
For any node Heap[ i ]
Its left child =
Heap [ 2 * i + 1 ]
Its right child =
Heap [ 2 * i + 2 ]
Its parent =
Heap [ (i -1) / 2 ]
CIS210 19
Operations on Binary Max-heaps
Create an empty heap.
Destroy a heap.
Insert (add) a new item to the heap.
Retrieve and then delete a heap’s root item.
(This item has the largest search key.)
Determine whether a heap empty?
...
CIS210 20
Insert an Item Into a Binary Max-heap
9
5 6
3 2
9
5 6
3 2 15
Insert 15
Heap:
CIS210 21
Example: Insert 15
9
5 15
3 2 6
9
5 6
3 2 15
15
5 9
3 2 6
Heapify-Up Heapify-Up
CIS210 22
Heapify-Up
Heapify-Up (Heap, root, bottom):
The heap order may be violated only at the
bottom node.
Restores the heap order between the root and
the bottom.
CIS210 23
Heapify-Up
Heapify-Up (Heap, root, bottom):
If bottom > root then
Set Parent to the index of bottom node;
If heap[Parent] < heap[bottom] then
• Swap heap[Parent] with heap[bottom];
• Heapify-Up (Heap, root, Parent);
CIS210 24
Insert an Item Into a Binary Max-heap
Insert (Heap, item):
Increment the size of the heap;
Put item in the next available position;
Heapify-Up (Heap, 0, size-1);
CIS210 25
Example: Insert 15
9
5 6
3 2
9
5 6
3 2 150
1
2
3
4
5
.
.
.
95632
0
1
2
3
4
5
.
.
.
9563215
CIS210 26
Example: Insert 15
9
5 15
3 2 6
9
5 6
3 2 15
15
5 9
3 2 6
0
1
2
3
4
5
.
.
.
9563215
0
1
2
3
4
5
.
.
.
9515326
0
1
2
3
4
5
.
.
.
1559326
Heapify-Up Heapify-Up
CIS210 27
Retrieve & Delete a heap’s root item
10
9 6
3 2 5
9 6
3 2 5
Delete the root
CIS210 28
Example: Delete the root
9 6
3 2 5
5
9 6
3 2 5
9
5 6
3 2
Heapify-Down
CIS210 29
Heapify-Down
Heapify-Down (Heap, root, bottom):
The heap order may be violated only at the
root node.
Restores the heap order between the root and
the bottom.
CIS210 30
Heapify-Down
Heapify-Down (Heap, root, bottom):
If root is not a leaf then
Set BiggerChild to the index of child with
larger value.
If heap[root] < heap[BiggerChild] then
• Swap heap[root] with heap[BiggerChild];
• Heapify-Down (BiggerChild, bottom);
CIS210 31
Retrieve & Delete a heap’s root item
DeleteTheRoot (Heap):
Delete the root value;
Move the last leaf value into the root position;
Decrement the size of the heap;
Heapify-Down (Heap, 0, size-1);
CIS210 32
Example: Delete the root
0
1
2
3
4
5
.
.
.
96325
10
9 6
3 2 5
9 6
3 2 50
1
2
3
4
5
.
.
.
1096325
CIS210 33
Example: Delete the root
0
1
2
3
4
5
.
.
.
95632
9 6
3 2 5
5
9 6
3 2 5
9
5 6
3 2
0
1
2
3
4
5
.
.
.
96325
0
1
2
3
4
5
.
.
.
596325
Heapify-Down
CIS210 34
Heap ADT
//--------------------------------------------------------------------
// heap.h
// Class declaration for the array implementation of the Heap ADT
//--------------------------------------------------------------------
using namespace std;
const int defMaxHeapSize = 10; // Default maximum heap size
CIS210 35
Heap ADT
template < class DT >
class Heap
{
public:
// Constructor
Heap ( int maxNumber = defMaxHeapSize );
// Destructor
~Heap ();
CIS210 36
Heap ADT
// Heap manipulation operations
void insert ( const DT &newElement ); // Insert element
DT removeMax (); // Remove max pty element
void clear (); // Clear heap
// Heap status operations
bool isEmpty () const; // Heap is empty
bool isFull () const; // Heap is full
CIS210 37
Heap ADT
// Output the heap structure -- used in testing/debugging
void showStructure () const;
void writeLevels () const; // Output in level order
CIS210 38
Heap ADT
private:
// Data members
int maxSize, // Maximum number of elements in the heap
size; // Actual number of elements in the heap
DT *dataItems; // Array containing the heap elements
};
CIS210 39
Binary Heap Operations -Analysis
The height of a complete binary tree with N nodes is
O(log N)
Worst-case running time:
Heapify-Up
O(log N)
Insert
O(log N)
Heapify-Down
O(log N)
DeletetheRoot
O(log N)
CIS210 40
Transform an Array To a
Binary (Max- or Min-) Heap
CIS210 41
Build a Binary Max-Heap
Convert an array into a max-heap.
6 3 5 9 2 10
0 1 2 3 4 5
10 9 6 3 2 5
0 1 2 3 4 5
Build a max-heap
10 6 9 3 2 5
0 1 2 3 4 5
CIS210 42
Building a Binary Max-Heap -Approach 1
Approach 1:
N inserts (Heapify-Up)
Top-down heap building
CIS210 43
Example: Transform an Array into a Binary Max-Heap -
Approach 1
6 3 5 9 2 10
0 1 2 3 4 5
10 6 9 3 2 5
0 1 2 3 4 5
?
CIS210 44
Example: Approach 1
6 3 5 9 2 10
0 1 2 3 4 5
6 3 5 9 2 10
0 1 2 3 4 5
6
Insert
Insert
CIS210 45
Example: Approach 1
6 3 5 9 2 10
0 1 2 3 4 5
6 3 5 9 2 10
0 1 2 3 4 5
6
3 5
Insert
6
3
Insert
CIS210 46
Example: Approach 1
9
6 5
3
9 6 5 3 2 10
0 1 2 3 4 5
Insert
2
9
6 5
3
9 6 5 3 2 10
0 1 2 3 4 5
Insert
CIS210 47
Example: Approach 1
10
6 9
3 2 5
10 6 9 3 2 5
0 1 2 3 4 5
CIS210 48
Quiz: Top-down Construction of a Binary Min-Heap?
6 3 5 9 2 10
0 1 2 3 4 5
2 3 5 9 6 10
0 1 2 3 4 5
?
CIS210 49
Building a Binary Heap-Approach 2
Approach 2:
Using Heapify-Down
Bottom-up heap building
CIS210 50
Example: Transform an Array into a Binary Max-Heap -
Approach 2
6 3 5 9 2 10
0 1 2 3 4 5
10 9 6 3 2 5
0 1 2 3 4 5
?
CIS210 51
Example: Transform an Array into a Binary Max-Heap -
Approach 2
6 3 5 9 2 10
0 1 2 3 4 5
6 3 5 9 2 10
0 1 2 3 4 5
6
3 5
9 2 10
6
3 5
9 2 10
Heapify-Down
CIS210 52
Example: Approach 2
6 3 5 9 2 10
0 1 2 3 4 5
6 3 5 9 2 10
0 1 2 3 4 5
6
3 5
9 2 10
6
3 5
9 2 10
Heapify-Down
CIS210 53
Example: Approach 2
6 3 5 9 2 10
0 1 2 3 4 5
6 3 5 9 2 10
0 1 2 3 4 5
6
3 5
9 2 10
6
3 5
9 2 10
Heapify-Down
CIS210 54
Example: Approach 2
6 3 5 9 2 10
0 1 2 3 4 5
6 3 10 9 2 5
0 1 2 3 4 5
6
3 5
9 2 10
6
3 10
9 2 5
Heapify-Down
CIS210 55
Example: Approach 2
6 3 10 9 2 5
0 1 2 3 4 5
6 9 10 3 2 5
0 1 2 3 4 5
6
3 10
9 2 5
6
9 10
3 2 5
Heapify-Down
CIS210 56
Example: Approach 2
6 9 10 3 2 5
0 1 2 3 4 5
10 9 6 3 2 5
0 1 2 3 4 5
6
9 10
9 2 5
10
9 6
3 2 5
Heap!
Heapify-Down
CIS210 57
Quiz: Bottom-up Construction of a Binary Min-Heap?
6 3 5 9 2 10
0 1 2 3 4 5
2 3 5 9 6 10
0 1 2 3 4 5
?
CIS210 58
Build a Binary Heap - Analysis
Top-down Approach 1:
N inserts (Heapify-Up)
Worst-case running time
O(N log N)
CIS210 59
Build a Binary Heap - Analysis
Bottom-up Approach 2:
N Heapify-Down
Worst-case running time
O(N log N)
O(N) linear time!
CIS210 60
Applications of
Heaps
CIS210 61
Applications of Heaps
Priority queues
Sorting
CIS210 62
A Better Data Structure for PQ?
Worst-case time complexity of a heap-based
implementation of PQ:
Enqueue
O(log N)
Dequeue
O(log N)
CIS210 63
What is Heap Sort?
A sorting algorithm using an advanced data
structure called a heap!
CIS210 64
Heap Sort
Convert an array of unsorted data into a max-heap.
Take the root element from the heap and put it into its place.
Re-heap the remaining elements
via the reheapfication downward, i.e. Heapify-Down!
Repeat until all elements are in the correct positions.
CIS210 65
Heap Sort
Sorted
In an ascending orderHeapify-Down
A diminishing heap & A growing sorted array
An unsorted array
A Max-Heap
CIS210 66
Example: Heap Sort
6 3 5 9 2 10
0 1 2 3 4 5
2 3 5 6 9 10
0 1 2 3 4 5
Sorting?
CIS210 67
Example: Transform an Array into a Heap
6 9 10 3 2 5
0 1 2 3 4 5
10 9 6 3 2 5
0 1 2 3 4 5
6
9 10
9 2 5
10
9 6
3 2 5
Max-Heap
CIS210 68
Example: Heap Sort
10 9 6 3 2 5
5 9 6 3 2 10
9 5 6 3 2 10
Max-Heap
CIS210 69
Example: Heap Sort
9 5 6 3 2 10
2 5 6 3 9 10
6 5 2 3 9 10
CIS210 70
Example: Heap Sort
6 5 2 3 9 10
3 5 2 6 9 10
5 3 2 6 9 10
CIS210 71
Example: Heap Sort
5 3 2 6 9 10
2 3 5 6 9 10
3 2 5 6 9 10
CIS210 72
Example: Heap Sort
3 2 5 6 9 10
2 3 5 6 9 10
2 3 5 6 9 10
CIS210 73
Example: Heap Sort
2 3 5 6 9 10
2 3 5 6 9 10
Sorted!
CIS210 74
Quiz: Heap Sort?
6 3 5 9 2 10
0 1 2 3 4 5
10 9 6 5 3 2
0 1 2 3 4 5
Sorting?
CIS210 75
Heap Sort
template < class DT >
void heapSort ( DT dataItems [], int size )
// Heap sort routine. Sorts the data items in the array in ascending
// order based on priority.
{
DT temp; // Temporary storage
int j; // Loop counter
// Build successively larger heaps within the array until the
// entire array is a heap.
for ( j = (size-1)/2 ; j >= 0 ; j-- )
moveDown(dataItems,j,size);
CIS210 76
Heap Sort
// Swap the root data item from each successively smaller heap with
// the last unsorted data item in the array. Restore the heap after
// each exchange.
for ( j = size-1 ; j > 0 ; j-- )
{
temp = dataItems[j];
dataItems[j] = dataItems[0];
dataItems[0] = temp;
moveDown(dataItems,0,j);
}
}
CIS210 77
Heap Sort
template < class DT >
void moveDown ( DT dataItems [], int root, int size )
// Restores the binary tree that is rooted at root to a heap by moving
// dataItems[root] downward until the tree satisfies the heap property.
// Parameter size is the number of data items in the array.
{
DT insertDataItem; // Data item to be inserted -- dataItems[root]
int stop, // (Re)insertion point found
j; // Array index that moves down the heap
insertDataItem = dataItems[root];
stop = 0;
j = 2*root+1;
while ( j < size && !stop )
{
if ( j < size-1 &&
dataItems[j].pty() < dataItems[j+1].pty() )
j++;
if ( insertDataItem.pty() >= dataItems[j].pty() )
stop = 1;
else
{
dataItems[(j-1)/2] = dataItems[j];
j = 2*j+1;
}
}
dataItems[(j-1)/2] = insertDataItem;
}
CIS210 78
Heap Sort - Analysis
The worst-case running time
O(n log n)
The best-case running time
O(n log n)
The average-case running time
O(n log n)