Heaps, MinHeap, MaxHeap, BinaryHeap, Whatever… CMPS 2133 Terry Griffin Binary Heap Definition: • • Property • A Tree is heap ordered if the key in each node is larger than or equal to the keys in all of it’s children Equivalently, the key in each node if a heap-ordered tree is smaller than or equal to the key of its parent No node in a heap ordered tree has a key larger than the key at the root Definition: • A heap is a set of nodes with keys arranged in a complete heap-ordered binary tree, represented as an array. Binary Heap: Properties Properties. • • Min (or max) element is in root. Heap with N elements has height = log2 N. 06 N = 14 Height = 3 14 78 83 45 18 91 81 47 77 84 53 99 64 Binary Heaps: Array Implementation Implementing • binary heaps. Use an array: no need for explicit parent or child pointers. Parent(i) = i/2 Left(i) = 2i Right(i) = 2i + 1 06 1 14 45 2 3 78 18 47 53 4 5 6 7 83 91 81 77 84 99 64 8 9 10 11 12 13 14 Binary Heap: Insertion Insert • • element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence 06 14 78 83 45 18 91 81 47 77 84 53 99 64 42 next free slot Binary Heap: Insertion Insert • • element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence swap with parent 06 14 78 83 45 18 91 81 47 77 84 53 99 64 42 Binary Heap: Insertion Insert • • element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence swap with parent 06 14 78 83 45 18 91 81 47 77 84 42 99 64 53 42 Binary Heap: Insertion Insert • • • element x into heap. Insert into next available slot. Bubble up until it's heap ordered. O(log N) operations. stop: heap ordered 06 14 78 83 42 18 91 81 47 77 84 45 99 64 53 Binary Heap: Decrease Key Decrease • • key of element x to k. Bubble up until it's heap ordered. O(log N) operations. 06 14 78 83 42 18 91 81 47 77 84 45 99 64 53 Binary Heap: Delete Min Delete • • minimum element from heap. Exchange root with rightmost leaf. Bubble root down until it's heap ordered. 06 14 78 83 42 18 91 81 47 77 84 45 99 64 53 Binary Heap: Delete Min Delete • • minimum element from heap. Exchange root with rightmost leaf. Bubble root down until it's heap ordered. 53 14 78 83 42 18 91 81 47 77 84 45 99 64 06 Binary Heap: Delete Min Delete • • minimum element from heap. Exchange root with rightmost leaf. Bubble root down until it's heap ordered. exchange with left child 53 14 78 83 42 18 91 81 47 77 84 45 99 64 Binary Heap: Delete Min Delete • • minimum element from heap. Exchange root with rightmost leaf. Bubble root down until it's heap ordered. exchange with right child 14 53 78 83 42 18 91 81 47 77 84 45 99 64 Binary Heap: Delete Min Delete • • • minimum element from heap. Exchange root with rightmost leaf. Bubble root down until it's heap ordered. O(log N) operations. stop: heap ordered 14 18 78 83 42 53 91 81 47 77 84 45 99 64 Binary Heap Operations Heapify Binary Heap Operations Heapify Example Binary Heap Operations Heapify Example Binary Heap Operations Heapify Example Binary Heap Operations Heapify Example Binary Heap Operations Heapify Example Binary Heap Operations Heapify Example Binary Heap Operations Heapify Example Binary Heap Operations Heapify Aside from the recursive call, what is the running time of Heapify. How many time can Heapify recursively call itself. Binary Heap Operations Building a Heap You can build a heap from the bottom up by running Heapify on successive sub-arrays. For an array of length n, all elements in the range: A[ceiling(n/2) + 1 … n ] are heaps. Why? Order of processing guarantees that the children of node i are heaps when that node is processed. Binary Heap Operations Building a Heap Binary Heap Operations Heap-Insert Heap-Insert(A,key) 1. heap-size[A]<-heap-size[A]+1 2. i<-heap-size[A] 3. while i>1 and A[Parent(i)]<key 4. 5. 6. do A[i]<-A[Parent(i)] i<-Parent(i) A[i]<-key Binary Heap Operations Heap-Insert Binary Heap Operations Heap-Insert Binary Heap Operations Heap-Insert Binary Heap Operations Heap-Insert Binary Heap Operations Heap Extract Max Heap-Extract Max 1. if heap-size[A] < 1 2. then error 3. max<- A[1] 4. A[1]<-A[heap-size[A]] 5. heap-size[A]<-heap-size[A] - 1 6. Heapify(A,1) 7. return max Binary Heap Operations Heap Extract Max example Binary Heap Operations Heap Extract Max example Binary Heap Operations Heap Extract Max example Binary Heap Operations Heap Extract Max example Binary Heap Operations Heap Extract Max example