Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Lab 08 - Heaps ====== ==== Heaps ==== Binary Heaps are binary trees with the following properties: **I.** Any node's $math[n] value is greater then his children's $math[c]. ($math[n.value \geq c.value]) **II.** The tree is almost complete (missing elements are possible only on the last level) 15 / \ 9 5 / \ / 2 3 1 Heap representation using an array: 15 9 5 2 3 1 This corresponds to a BF traversal of the tree, and it guarantees that the children nodes corresponding to v[i] are v[2i + 1] and v[2i + 2] ---- **1.** Basic operations. Implement and analyse the complexity for the following operations: **1.1** **empty_heap()** - creates an empty heap. **1.2** **insert(v, h)** - inserts $math[v] in $math[h]. At the end of the procedure , $math[h] is the updated heap. **1.3** **get_max(h)** - return the maximum value from the heap. **1.4** **delete(pos,h)** - deletes element at position $math[pos] from the heap. algorithm: swap the last element with the element whose position is to be deleted the resulting tree may no longer be a heap (!!) make sure the heap property is preserved Deleting 9: 15 / \ 9 5 / \ / 2 3 1 9 is swapped with 1, then deleted 15 / \ 1 5 / \ / 2 3 9 Here, we make sure the heap property is preserved by swapping the largest of the children to be with the current root and repeating the process all over for the modified subtree: 15 / \ 1 5 / \ 2 3 15 / \ 3 5 / \ 2 1 ---- **2.** Using heaps: **2.1** Implement a procedure which constructs a heap from an arbitrary array, using the insert operation. **2.2** Implement heapsort using the previous procedure.