Download presentation
1
data ordered along paths from root to leaf
Heaps data ordered along paths from root to leaf
2
Heaps and applications
what is a heap? how is a heap implemented? using a heap for a priority queue using a heap for sorting
3
Heap definition Complete binary tree with data ordered so that data value in a node always precedes data in child nodes 30 20 28 16 19 26 26 14 8 17 18 24 11 example: integer values in descending order along path from root blue node is only possible removal location red node is only possible insertion location
4
Heap storage An array is the ideal storage for a complete binary tree 1 2 3 30 20 28 16 19 26 26 14 8 17 18 24 level / 30 20 28 16 19 26 26 14 8 17 18 24 a size 12
5
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 30 28 20 16 14 8 19 17 18 26 24 1 2 3 level / 30 20 28 16 19 26 26 14 8 17 18 24 a size 12
6
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); }
7
Heap operations Insertion and deletion maintain the complete tree AND
maintain the array with no ‘holes’ 30 20 28 16 19 26 26 14 8 17 18 24 43 30 20 28 16 19 26 26 14 8 17 18 24 43 a size 12
8
Heap operations - terminology
heapify – 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
9
Heap operations - insertion
insert new data item after last current data if necessary, ‘reheapify’ up 30 20 28 16 19 26 26 14 8 17 18 24 29 30 20 28 16 19 26 26 14 8 17 18 24 29 a 13 size 12
10
Heap operations - insertion
insert new data item after last current data if necessary, ‘reheapify’ up 30 29 20 28 28 16 19 26 26 26 14 8 17 18 24 29 29 28 26 30 20 28 16 19 26 26 14 8 17 18 24 29 a size 13
11
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(); }
12
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; }
13
Heap operations - deletion
only the item at the root can be removed delete the root data and replace with the last data item if necessary, ‘reheapify’ down 24 30 20 28 16 19 26 26 14 8 17 18 24 24 30 20 28 16 19 26 26 14 8 17 18 24 a size 12 11
14
Heap operations - deletion
only the item at the root can be removed delete the root data and replace with the last data item if necessary, ‘reheapify’ down 28 24 26 20 28 16 19 26 24 26 14 8 17 18 28 26 24 24 20 28 16 19 26 26 14 8 17 18 a size 11
15
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; }
16
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
17
Heap operations – ‘heapify’
make a heap from an unsorted array buildHeap() recursive construction of a heap: (post-order traversal) heapify left subtree heapify right subtree send root to correct position using reheapifyDown() 3 1 2
18
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
19
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
20
implementation in an ADT: heapify an array - iterative
public void heapify () { for (int next = size/2; next>=1; next--) heapifyDown(next); }
21
Heap application priority queue implementation insert (enqueue)
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 unsorted linked list sorted linked list binary heap O(log n)
22
Heap application - sorting
Heapsort in place sorting in O(n log n) time two stage procedure: unsorted array -> heap -> sorted array 1 2 build heap repeat [delete maximum ]
23
Heap application - sorting
heap must be ordered opposite to final result e.g., for ascending order sort, heap must be descending order basic operation of second stage: heap sorted m x m x m x x m remove max item in heap (m); last item gets moved to top last item gets heapified down put m in vacant space after heap
24
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;
25
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 Codes a 1 b 010 c 0001 d 0000 x 011 y 001 1 a 1 1 1 1 y b x d c
26
Building the tree Based on frequencies of characters
- Make heap (lowest first) Example Frequencies a 125 b 15 c 4 d 3 x 17 y 12 3 d 12 y 4 c 17 x 125 a 15 b
27
Building the tree Remove two lowest from heap
4 c 3 d 17 x 12 y Remove two lowest from heap Make a tree with each as a subtree – add frequencies for root Put tree in heap by root frequency 3 d 125 a 15 b 4 c 17 x 12 y 4 c 15 b 12 y 125 a 17 x 7 12 y 15 b 125 a 17 x 3 d 4 c
28
Building the tree Remove two lowest from heap
12 y 17 x 15 b 4 c 3 d 7 Remove two lowest from heap Make a tree with each as a subtree Put tree in heap by total frequency Repeat until all in one tree . . . 17 x 12 y 125 a 15 b 4 c 3 d 7 19 19 125 a 32 12 y 15 b 17 x 7 3 d 4 c
29
Building the tree Remove two lowest from heap
19 125 a 32 Remove two lowest from heap Make a tree with each as a subtree Put tree in heap by total frequency Repeat until all in one tree . . . 12 y 15 b 17 x 7 3 d 4 c 51 125 a 19 32 7 12 y 15 b 17 x 3 d 4 c
30
Building the tree Remove two lowest from heap
51 125 a Remove two lowest from heap Make a tree with each as a subtree Put tree in heap by total frequency Repeat until all in one tree . . . 19 32 7 12 y 15 b 17 x 3 d 4 c 176 51 125 a 19 32 7 12 y 15 b 17 x 3 d 4 c
31
Translating the tree Make table from tree Example Codes a 1 b 010
176 Make table from tree Example Codes a 1 b 010 c 0001 d 0000 x 011 y 001 51 125 a 19 32 7 12 y 15 b 17 x 3 d 4 c
32
Using the Huffman coding
b 010 c 0001 d 0000 x 011 y 001 To encode, use table: day To decode, use tree day a 1 d c y b x
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.