chapter 12 heaps & heapsort © john urrutia 2014, all rights reserved1
TRANSCRIPT
Chapter 12 Heaps & HeapSort
© John Urrutia 2014, All Rights Reserved 1
Heaps – Versus – Priority Queues Priority queues implemented as sorted arrays
Insertion – O(N) (slow)Deletion – O(1) (fast)
Heaps implemented as an arrayInsertion – O(logN) (faster than PQs)Deletion – O(logN) (slower than PQs)
© John Urrutia 2014, All Rights Reserved 2
Heaps – Versus – Priority Queues Conceptually –
Priority Queues are wrappers for Heaps or Arrays
The methods are sameBoth are ADT’s
© John Urrutia 2014, All Rights Reserved 3
Introduction to HeapsA heap is a binary tree with conditions.
Completeness All levels are full except the lowest All nodes on the lowest level are filled left to right
with no gapsHeap Condition
Each node’s key >= the keys of its children
Heaps are weakly ordered binary treesLacks the ability to traverse in orderEvery path from root to leaf are in descending
orderEach path is independent of every other.
© John Urrutia 2014, All Rights Reserved 4
Heap Nodespublic Node{
private int iData;public Node(int key){ iData = key; }public int getKey(){ return iData; }public void setKey(int id){ iData = id; }
}© John Urrutia 2014, All Rights Reserved 5
Heap Arrayclass Heap{ private Node[] heapArray;
private int maxSize = 256;private int currentSize = 0;public Heap(int mx){ maxSize = mx;
currentSize = 0; heapArray = new Node[maxSize];
}}
© John Urrutia 2014, All Rights Reserved 6
Heap100
10
20
55
30
45
60
50
70
90
80
40
45 0
© John Urrutia 2014, All Rights Reserved 7
Heap Array Implementation
Heap Array
0 100
1 90
Heap
100
90 0
Root
80
2 80
3 30
4 60
5 50
1 2
30 60 50 70
3 4 5 6
6 70
7 20
20 10
40 55 45 5
Last Node
8 10
9 40
10 55
11 45
12 5
7 8 9 10 11 12
.
© John Urrutia 2014, All Rights Reserved 8
Introduction to HeapsCreating the array to hold the heap
Given array index for a node X: The parent of X is – (X -1)/2 The left Child of X is – 2 * X + 1 The right Child of X is – 2 * X + 2
© John Urrutia 2014, All Rights Reserved 9
Introduction to HeapsBecause Heaps are weakly ordered binary
treesSearching for a key is slow O(N)Deleting for a key is slow O(N)Adding the minimum key is fast O(1) since it is
always at the end leaf or end of the array.
© John Urrutia 2014, All Rights Reserved 10
Introduction to HeapsBecause Heaps are weakly ordered binary trees
Removing the maximum key is fast O(1) since it is always at the root or top of the array, but that violates the completeness of the heap
Replace the root with the minimum key which is the last entry in the array is fast O(1) since it always at the end, but violates the heap condition
The solution: Replace the root with the minimum key Percolate the root node down until it is in the right
position
© John Urrutia 2014, All Rights Reserved 11
Heap – Remove Max Key100
10
20
55
30
45
60
50
70
90
80
40
5
© John Urrutia 2014, All Rights Reserved 12
Heap – Remove codeDeletes the root node (contains the maximum Key )
public Node remove() {Node root = heapArray[0]; heapArray[0] = heapArray[--currentSize]; trickleDown(0); return root;}
© John Urrutia 2014, All Rights Reserved 13
Heap – Remove codeDeletes the root node (contains the maximum Key )
public void trickleDown(int index){ int largerChild; Node top = heapArray[index]; // save root while (index < currentSize / 2 // while node has at least 1 child { int leftChild = 2 * index + 1;
int rightChild = leftChild + 1; // find larger child if (rightChild < currentSize && // (rightChild exists?)
heapArray[leftChild].getKey() < heapArray[rightChild].getKey()) largerChild = rightChild;
elselargerChild = leftChild;
if (top.getKey() >= heapArray[largerChild].getKey())break;
heapArray[index] = heapArray[largerChild];index = largerChild;} // end while
heapArray[index] = top; // root to index}
© John Urrutia 2014, All Rights Reserved 14
Introduction to HeapsInserting a non-minimum Node
Searching or finding a node position is O(N) The solution:
Place the node after the minimum key at the end of the array
Percolate the node up until it is in the right position
© John Urrutia 2014, All Rights Reserved 15
Heap – Insert non-minimum100
10
20
55
30
45
60
50
70
90
80
40
5 120
© John Urrutia 2014, All Rights Reserved 16
Introduction to HeapsCopy vs. Swap
Swapping involves 3 steps for each node
Copying involves 1 Step for each node except the last Copy A to Temp Copy B to A Copy C to B Copy D to C Copy temp to D
• Copy A to Temp
• Copy B to A• Copy Temp to
B
• Copy B to Temp
• Copy C to B• Copy Temp to
C
• Copy C to Temp
• Copy D to C• Copy Temp to
D
© John Urrutia 2014, All Rights Reserved 17
Heap – Insert non-minimum100
10
20
55
30
45
60
50
70
90
80
40
5
120
© John Urrutia 2014, All Rights Reserved 18
Heap – Insert codeInserts the last node
public Boolean insert(int key){ if (currentSize == maxSize) return false; Node newNode = new Node(key); heapArray[currentSize] = newNode; trickleUp(currentSize++); return true;} // end insert()
© John Urrutia 2014, All Rights Reserved 19
Heap – Insert codeInserts the last node
public void trickleUp(int index){ int parent = (index - 1) / 2; Node bottom = heapArray[index]; while (index > 0 && heapArray[parent].getKey() < bottom.getKey()) { heapArray[index] = heapArray[parent]; // move it down index = parent; parent = (parent - 1) / 2; } // end while heapArray[index] = bottom;} // end trickleUp()
© John Urrutia 2014, All Rights Reserved 20
The HeapsortThe structure of a heap unexpectedly lends
itself to sorting unordered data.Recall in a heap the maximum key is always at
the rootHence repeatedly removing the root will return
the data in descending sequence.The steps:
Insert each item into a heap O(logN)Remove each root item from heap O(logN)Each must be executed N times giving
O(N*logN)the same as quicksort© John Urrutia 2014, All Rights Reserved 21
The HeapsortEfficiency of O(N*logN) (the same as
quicksort) is actually longer – more instructions in the loop.
Two tricks to improve efficiencyLoad all array items randomly
Trickle-down the root N/2 times
© John Urrutia 2014, All Rights Reserved 22
15 Element Random Array05
10
25
90
60
55
15
50
45
20
35
40
95
© John Urrutia 2014, All Rights Reserved 23
30
65
[00]
[01]
[02]
[03]
[04]
[05]
[06]
[07]
[08]
[09]
[10]
[11]
[12]
[13]
[14]
L0
L3
L2
L1
Heap Levels
Trickledown heapsort05
10
25
90
60
55
15
50
45
20
35
40
95
© John Urrutia 2014, All Rights Reserved 24
30
65
L2
L1
L0
Heapsort Array95
10
25
15
60
35
40
55
45
90
65
20
50
© John Urrutia 2014, All Rights Reserved 25
30
05
[00]
[01]
[02]
[03]
[04]
[05]
[06]
[07]
[08]
[09]
[10]
[11]
[12]
[13]
[14]
L0
L3
L2
L1
Heap Levels
Heapsort – Create Heap & Load with random numbers
public static void Main(String[] args) {
int size, j; Console.Write("Enter number of items: "); size = int.Parse(Console.ReadLine()); Heap theHeap = new Heap(size);
Random rnd = new Random();
for(j=0; j<size; j++) // fill array with { // random nodes int random = (int)rnd.Next(0,100); Node newNode = new Node(random); theHeap.insertAt(j, newNode); theHeap.incrementSize();
}
© John Urrutia 2014, All Rights Reserved 26
Heapsort – Applies the heap rule to the array
for(j=size/2-1; j>=0; j--) // make random array into heap theHeap.trickleDown(j);
for(j=size-1; j>=0; j--) // remove from heap and{ // store at array end
Node biggestNode = theHeap.remove();theHeap.insertAt(j, biggestNode);
}
Console.Write("Sorted: ");for(int j=0; j<maxSize; j++){ Console.Write(heapArray[j].getKey() + " "); Console.WriteLine("");}
© John Urrutia 2014, All Rights Reserved 27
Heapsort – Sorted Array95
10
25
15
60
35
40
55
45
90
65
20
50
© John Urrutia 2014, All Rights Reserved 28
30
05
Last Note on HeapsortAlthough heapsort is slightly less efficient
than Quicksort, it makes up for this by being less sensitive to the initial data distribution.
Quicksort can slow to O(N2) whereas heapsort remains O(N*logN) regardless of distribution.
© John Urrutia 2014, All Rights Reserved 29
SummaryIn an ascending-priority queue the item with
the largest key is said to have the highest priority. (It’s the smallest item in a descending queue.)
A priority queue is an Abstract Data Type (ADT) that offers methods for insertion of data and removal of the largest (or smallest) item.
A heap is an efficient implementation of an ADT priority queue.
A heap offers removal of the largest item, and insertion, in O(N*logN) time.
© John Urrutia 2014, All Rights Reserved 30
SummaryThe largest item is always in the root.Heaps do not support ordered traversal of
the data, locating an item with a specific key, or deletion.
A heap is usually implemented as an array representing a complete binary tree.
The root is at index 0 and the last item at index N-1.
© John Urrutia 2014, All Rights Reserved 31
SummaryEach node has a key less than its parents and
greater than its children.An item to be inserted is always placed in the
first vacant cell of the array and then trickled up to its appropriate position.
When an item is removed from the root, it’s replaced by the last item in the array, which is then trickled down to its appropriate position.
The trickle-up and trickle-down processes can be thought of as a sequence of swaps, but are more efficiently implemented as a sequence of copies.
© John Urrutia 2014, All Rights Reserved 32
SummaryThe priority of an arbitrary item can be
changed. First, its key is changed.Then, if the key was increased, the item is
trickled up, but if the key was decreased, the item is trickled down.
A heap can be based on a binary tree (not a search tree) that mirrors the heap structure; this is called a treeheap.
Algorithms exist to find the last occupied node or the first free node in a treeheap.
© John Urrutia 2014, All Rights Reserved 33
SummaryHeapsort is an efficient sorting procedure that
requires O(N*logN) time.Conceptually, heapsort consists of making N
insertions into a heap, followed by N removals.Heapsort can be made to run faster by applying
the trickle-down algorithm directly to N/2 items in the unsorted array, rather than inserting N items.
The same array can be used for the initial unordered data, for the heap array, and for the final sorted data. Thus, heapsort requires no extra memory.
© John Urrutia 2014, All Rights Reserved 34