2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline
3 O(1) (worst case O(n)) O(n) O(1) O(1) or O(n) O(n) or O(1) O(n) Implementations for Priority Queues
4 A heap is a binary tree whose root contains the minimum value in the tree and whose subtrees are heaps. Alternately, A heap is a binary tree whose values are in ascending order on every path from the root to the leaf. Examples of heaps: Generally speaking, we like to keep our heaps complete® What’s a Heap?
5 Because of the loosened requirements on ordering, heaps are easier to maintain in a consistent state, and therefore more efficient than other data structures for implementing priority queues. Advantages of a Heap
9 Removal The root is the smallest element Therefore, getFirst and remove always access the root Heap removal: 1.Replace the root with the element in the last filled position 2.Fix the heap by bubbling the root down
13 Big-O for Heaps Insertion add element to next position bubble element up Deletion remove root and swap in most recently added element percolate element down = O(1) = height of tree = O(logn)
14 Vector/Array based Heaps It is possible to efficiently store a complete binary tree in an array The following provides a mapping between array elements and the nodes of the tree: The element at index 0 is the root The left child of a node at index i is stored at index 2i+1 The right child is stored at 2i+2 The parent of a node at index i is at index floor((i-1)/2) For example:
15 Vector based Heaps - Insertion General Algorithm 1. Get index of next open spot and add element to it 2. While index isn’t the root a) Calculate index of parent b) If parent is bigger than element swap and set index to parent c) Else break;
20 Vector based Heaps - Deletion General Algorithm 1. Save value of 1 st element 2. Move value of last element to 1 st position, set idx to index of root 3. While a child of idx is smaller than idx a) swap value stored at idx with smallest child b) set idx to index of smallest child
26 We can use a priority queue to sort stuff: 1. Insert the elements one at a time into the priority queue. 2. Remove the elements 1 at a time from the priority queue and place them into an array. 3. Runtime? Priority Queues (Heaps) and Sorting
27 Does every subtree of a heap satisfy the properties of a heap? Is every heap an array? Is every array a heap? What is the height of a heap? What are the maximum and minimum numbers of elements in a heap of height h? Is an array that is sorted in ascending order a heap? Write an algorithm that will turn an array into a heap. Complexity? Write an equals method for a vector based heap. Complexity?Questions???
28 Given an array A, how can we turn it into a heap? Simple: –Elements A[ n/2 +1] through A[n] are leaves, so they can be considered one-element heaps. –Call Heapify in a bottom-up manner starting at index n/2 . Running time : –easy analysis: O(nlgn) –better analysis (proof omitted): O(n) BuildHeap