heaps data ordered along paths from root to leaf
TRANSCRIPT
Heaps
data ordered along paths from root to leaf
Heaps and applications
• what is a heap?
• how is a heap implemented?
• using a heap for a priority queue
• using a heap for sorting
Heap definitionComplete binary tree with data ordered so that data value in a node always precedes data in child nodes
30
2820
16
14 8
19
17 18
26
24 11
26
example:•integer values in descending order along path from root•blue node is only possible removal location•red node is only possible insertion location
Heap storageAn array is the ideal storage for a complete binary tree
30
2820
16
14 8
19
17 18
26
24
26
0
1
2
3
0
30 20 28 16 19 26 26 14 8 17 18 24/
0 1 2 3
a
size 12
level
Heap implementation
Assuming 0th element of array is unused,
• root node is at index 1
• for node at index i:
• parent node is at i/2
• child nodes are at 2i and 2i+1
• last node is at index size
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
30 20 28 16 19 26 26 14 8 17 18 24/
0 1 2 3
a
size 12
level
30
2820
16
14 8
19
17 18
26
24
26
0
1
2
3
implementation in an ADT:array and size
Comparable[] a;
int size;
private boolean full()
{ // first element of array is empty
return (size == a.length-1);
}
Heap operationsInsertion and deletion maintain the complete tree AND
maintain the array with no ‘holes’
30
2820
16
14 8
19
17 18
26
24
26
0
30 20 28 16 19 26 26 14 8 17 18 24 43a
size 12
43
Heap operations - terminologyheapify – reorganize data in an array so it is a heap
reheapify – reorganize data in a heap with one data item out of order
reheapify down – when data at root is out of order
reheapify up – when last data item is out of order
Heap operations - insertion
30
2820
16
14 8
19
17 18
26
24
26
0
30 20 28 16 19 26 26 14 8 17 18 24 29a
size 12
1. insert new data item after last current data
2. if necessary, ‘reheapify’ up
29
13
Heap operations - insertion
30
2820
16
14 8
19
17 18
26
24
26
0
30 20 28 16 19 26 26 14 8 17 18 24 29a
size 13
1. insert new data item after last current data
2. if necessary, ‘reheapify’ up
29
29
28
26
29 28 26
implementation in an ADT:insertion
public void add(Comparable data)
{
if (full()) // expand array
ensureCapacity(2*size);
size++;
a[size] = data;
if (size > 1)
heapifyUp();
}
implementation in an ADT:heapifyUp
private void heapifyUp(){ Comparable temp; int next = size; while (next != 1 && a[next].compareTo(a[next/2]) > 0) { temp = a[next]; a[next] = a[next/2]; a[next/2] = temp; next = next/2; }}
Heap operations - deletion
30
2820
16
14 8
19
17 18
26
24
26
0
30 20 28 16 19 26 26 14 8 17 18 24a
size 12
only the item at the root can be removed
1. delete the root data and replace with the last data item
2. if necessary, ‘reheapify’ down24
24
11
Heap operations - deletion
24
2820
16
14 8
19
17 18
26 26
0
24 20 28 16 19 26 26 14 8 17 18a
size 11
only the item at the root can be removed
1. delete the root data and replace with the last data item
2. if necessary, ‘reheapify’ down
28 26 24
28
26
24
implementation in an ADT:deletion
public Comparable removeMax(){ if (size == 0) throw new IllegalStateException(“empty heap”); Comparable max = a[1]; a[1] = a[size]; size--; if (size > 1) heapifyDown(1); return max;}
implementation in an ADT:heapifyDown
private void heapifyDown(int root){ Comparable temp; int next = root; while (next*2 <= size) // node has a child { int child = 2*next; // left child if (child < size && a[child].compareTo(a[child+1]) < 0) child++; // right child instead if (a[next].compareTo(a[child]) < 0) { temp = a[next]; a[next] = a[child]; a[child] = temp; next = child; } else; next = size; // stop loop }}
Heap operations – ‘heapify’make a heap from an unsorted array
buildHeap()
recursive construction of a heap: (post-order traversal)
1. heapify left subtree
2. heapify right subtree
3. send root to correct position using reheapifyDown()
1 2
3
implementation in an ADT:heapify an array - recursive
public void heapify ()
{
heapify(1);
}
private void heapify(int root)
{
if(root > size/2) return; //no children
heapify(root*2); // left subtree
if (root*2+1 <=size)
heapify(root*2+1); // right subtree
heapifyDown(root); // do root node
}
Heap operations – ‘heapify’iterative construction of a heap: (bottom-up) – O(n)
• the leaves are already (one node) heaps
• start at second row from bottom-heapifyDown()
• row by row to row 0 (root)leaves
implementation in an ADT:heapify an array - iterative
public void heapify ()
{
for (int next = size/2; next>=1; next--)
heapifyDown(next);
}
Heap application
these operations fit the requirements of the priority queue with O(log n) performance
priority queue implementation
insert (enqueue)
delete min (dequeue)
unsorted array O(1) O(n)
sorted array O(n) O(1)
unsorted linked list O(1) O(n)
sorted linked list O(n) O(1)
binary heap O(log n) O(log n)
21
Heap application - sorting
Heapsort
• in place sorting in O(n log n) time
• two stage procedure:
unsorted array -> heap -> sorted array
build heap
repeat [delete maximum ]
Heap application - sorting
m x
heap must be ordered opposite to final resulte.g., for ascending order sort, heap must be descending order
basic operation of second stage:
heap sorted
x m
x
x
m
m
1. remove max item in heap (m); last item gets moved to top2. last item gets heapified down3. put m in vacant space after heap
implementation in an ADT:heapsort
public void heapsort(){ heapify(); int keepSize = size; for (int next = 1; next <= keepSize; next++) { Comparable temp = removeMax(); a[size+1] = temp; } size = keepSize;}
Huffman coding with a heap
• Huffman codes are based on a binary tree– Left branch coded 0– Right branch coded 1– Coded characters are in leaves
Example Codesa 1b 010c 0001d 0000x 011y 001
a
0 1
0 1
0 1 0 1
0 1
d c
y b x
Building the tree1. Based on frequencies of characters
- Make heap (lowest first)
Example Frequenciesa 125b 15c 4d 3x 17y 12
125 a 15 b4 c3 d 17 x12 y
Building the tree
2. Remove two lowest from heap
- Make a tree with each as a subtree – add frequencies for root
- Put tree in heap by root frequency
125 a 15 b4 c3 d 17 x12 y
125 a15 b4 c 17 x12 y
15 b12 y 125 a17 x
3 d
4 c
125 a12 y 17 x15 b
4 c3 d
7
Building the tree
2. Remove two lowest from heap
- Make a tree with each as a subtree
- Put tree in heap by total frequency
3. Repeat until all in one tree . . .
125 a12 y 17 x15 b
4 c3 d
7
17 x
12 y
125 a15 b
4 c3 d
7
19
17 x
125 a
15 b12 y
4 c3 d
7
19 32
Building the tree
2. Remove two lowest from heap
- Make a tree with each as a subtree
- Put tree in heap by total frequency
3. Repeat until all in one tree . . .
17 x
125 a
15 b12 y
4 c3 d
7
19 32
17 x
125 a
15 b12 y
4 c3 d
7
19 32
51
Building the tree
2. Remove two lowest from heap
- Make a tree with each as a subtree
- Put tree in heap by total frequency
3. Repeat until all in one tree . . .
17 x
125 a
15 b12 y
4 c3 d
7
19 32
51
176
17 x
125 a
15 b12 y
4 c3 d
7
19 32
51
Translating the tree
4. Make table from tree
Example Codes
a 1
b 010
c 0001
d 0000
x 011
y 001
17 x
125 a
15 b12 y
4 c3 d
7
19 32
51
176
Using the Huffman coding
• To encode, use table:
day 00001001
• To decode, use tree
00001001 day
a 1b 010c 0001d 0000x 011y 001
a
0 1
0 1
0 1 0 1
0 1
d c
y b x