Heaps A heap is a binary tree storing keys at its nodes and satisfying the folloing properties:! Heap-Order: " for every internal node v other than the root, key(v)! key(parent(v))! Complete Binary Tree: " A heap is filled from its root and donards level by level from the left. If h is the height of the heap, then! for i = 0,, h ", there are i nodes of depth i! at depth h ", the internal nodes are to the left of the external nodes! the last node of a heap is the rightmost node of depth h Lecture - Håkan Jonsson last node Height of a Heap Theorem: A heap storing n keys has height O(log n) Proof: (e apply the complete binary tree property)! Let h be the height of a heap storing n keys! Since there are i keys at depth i = 0,, h " and at least one key at depth h, e have n! + + + + h" +! Thus, n! h, i.e., h # log n depth 0 keys h" h h" Lecture - Håkan Jonsson Heaps and Priority Queues We can use a heap to implement a priority queue. We store a (key, element) item at each internal node. We keep track of the position of the last node. (For simplicity, e sho only the keys in the pictures.) (, Pat) (, Jeff) (, Anna) (, Sue) (, Mark) Lecture - Håkan Jonsson. Insertion into a Heap The insertion algorithm consists of three steps! Find the insertion node. This ill be a ne last node.! Store the item at.! Restore the heaporder property, hich could have been violated by the insertion. Lecture - Håkan Jonsson insertion node
Upheap To restore the heap-order property after the insertion of a ne key k, algorithm upheap saps k along an upard path from the insertion node. Upheap terminates hen the key k reaches the root or a node hose parent has a key smaller than or equal to k. Since a heap ith n elements has height O(log n), upheap runs in O(log n) time. Lecture - Håkan Jonsson. Removal from a Heap The removal algorithm extracts the root element of the tree and consists of three steps:! Replace the root key ith the key of the last node! Remove.! Restore the heap-order property Lecture - Håkan Jonsson ne last node last node Donheap After replacing the root key ith the key k of the last node, the heap-order property may, as hen inserting, be violated. Algorithm donheap restores the heap-order property by sapping key k along a donard path from the root. Upheap terminates hen key k reaches a leaf or a node hose children have keys greater than or equal to k. Since a heap has height O(log n), donheap runs in O(log n) time. Finding a Ne Last Node A ne insertion node can be found by traversing a path nodes:! Starting at the current last node, go up until a left child or the root is reached.! If a left child is reached, go to the right child.! Go don left until a leaf is reached The number of nodes visited during the traversal is O(log n). (Similar algorithm for finding the ne last node after a removal.) Lecture - Håkan Jonsson Lecture - Håkan Jonsson
Heap-Sort An Array-based Heap Implementation Consider a priority queue ith n items implemented by means of a heap:! the space used is O(n),! methods insert and deletemin take O(log n) time, and! methods sie, isempty, and extractmin take time O() time. So, using a heap-based priority queue, e can sort a sequence of n elements in O(n log n) time! The resulting algorithm is called heap-sort. Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort and selection-sort. Lecture - Håkan Jonsson Lecture - Håkan Jonsson 0 An Array-based Heap Implementation () In-place, meaning that all operations can be carried out easily ithout more extra memory than some constant amount. Memory efficient in the sense that all array elements are used (there are no unused holes inside the array). Lecture - Håkan Jonsson Merging To Heaps Given to heaps and a key k a ne heap is created by! first building a binary tree ith the root node storing k and ith the to heaps as subtrees.! Then, donheap is used to restore the heap-order property. Done in O(log n) time!! NB! n is the number of elements in the hole heap. Lecture - Håkan Jonsson
Bottom-up Heap Construction We can construct a heap storing n given keys by using a bottom-up construction ith log n phases. In phase i, pairs of heaps ith i " keys are merged into heaps ith i+ " keys. i " i " i+ " Example 0 0 Lecture - Håkan Jonsson Lecture - Håkan Jonsson Example (contd.) Example (contd.) 0 0 0 0 Lecture - Håkan Jonsson Lecture - Håkan Jonsson
Example (end) The Cost of Bottom-up Heap Construction 0 0 0 0 Easy to argue that it takes O(n log n) steps to build a heap containing n elements.! We count the number of comparisons needed in the orst case. But one can sho that it actually doesn t take more than O(n) comparisons.! This is faster than n successive insertions!! This makes that first step of Heap-Sort faster but since e still have to do n removals the total time doesn t improve in the orst case. Lecture - Håkan Jonsson Lecture - Håkan Jonsson Elements are considered right-to-left and bottom-up. Each element considered is moved to its right place don in its subtrees. (The to subtrees under the element considered are already heaps.) Lecture - Håkan Jonsson