Heaps A heap is a binary tree that
Heaps A heap is a binary tree that satisfies the following properties: • Structure property: It is a complete binary tree • Heap-order property: Each node satisfies the heap condition: – The key of every node must be smaller than (or equal to) the keys of its children, i. e. n. info n. left. info and n. info n. right. info, for all nodes n • Note: this is called a min-heap – max-heaps have the opposite heap condition. COSC 2 P 03 Week 6 1
remove. Min Algorithm – min-heap temp = root; root = last node; current = root; while current has at least 1 child { smaller = smallest child; if (current < smaller) break; else swap(current, smaller); } return temp; • What is the complexity? COSC 2 P 03 Week 6 2
insert Algorithm – min-heap put new node at bottom right of tree and call it current; // trickle up while key of current < key of its parent swap (current, parent); • What is the complexity? COSC 2 P 03 Week 6 3
Array implementation of Heap • array[0. . capacity]: the heap array • array[1. . currentsize]: elements currently in heap • currentsize: number of nodes in heap • remove. Min (see fig. 6. 12 for full details): – The smallest element is the root, which is array[1] – The “last” element (rightmost on lowest level) is array[currentsize] – In trickle-down, we need to find the children of a node: • Left child of array[t] is array[2 t] • Right child of array[t] is array[2 t+1] COSC 2 P 03 Week 6 4
Array implementation of Heap – remove. Min Example 0 0 0 1 2 3 4 5 6 7 5 35 10 50 60 40 20 1 2 3 4 5 6 20 35 10 50 60 40 1 2 3 4 5 6 10 35 20 50 60 40 COSC 2 P 03 Week 6 5
Array implementation of Heap (insert – see fig. 6. 8 for full details) • Insert in array[currentsize+1] • During trickle-up, we need to find the parent of a node: – Parent of array[t] is array[ t/2 ] • Example: insert 15 0 0 1 2 3 4 5 6 7 10 35 20 50 60 40 15 1 2 3 4 5 6 7 10 35 15 50 60 40 20 COSC 2 P 03 Week 6 6
Transforming a complete binary tree into a heap • In a heap, all subtrees are also heaps. • Idea: transform all subtrees into heaps, starting from smallest. Note that leaves are already heaps. • Note: array[ currentsize/2 ]is the parent of the “last” node in the heap. build. Heap algorithm: for (i = currentsize/2 ; i >= 1; i--) apply trickle-down to array[i]; COSC 2 P 03 Week 6 7
build. Heap example i=5 59 36 58 21 41 97 31 16 26 53 i=4 59 36 58 21 41 97 31 16 26 53 i=3 59 36 58 16 41 97 31 21 26 53 i=2 59 36 31 16 41 97 58 21 26 53 i=1 59 16 31 21 41 97 58 36 26 53 End 16 21 31 26 41 97 58 36 59 53 COSC 2 P 03 Week 6 8
Heapsort version 1 – min heap • The results are stored in B[1. . n]. build. Heap(A); for(i = 1; i <= n; i++) B[i] = remove. Min(A); (etc) COSC 2 P 03 Week 6 9
Heapsort version 2 – max heap • Better idea: avoid the use of a second array by storing sorted elements at the end of the array • Use a max-heap: – At each step, swap root with last element, then apply trickle down to new root • Example – after conversion to max heap: • After 1 st swap: • After 2 nd swap: COSC 2 P 03 Week 6 10
- Slides: 10