290 likes | 491 Views
Heaps. Chapter 21. What is a heap used for?. Sorting HeapSort sorts an N -element array on O ( N log N ) time and uses very little extra memory Priority Queues Heaps allow inserting an element and extracting the smallest element in a set, both in O (log N ) time. What is a heap?.
E N D
Heaps Chapter 21
What is a heap used for? • Sorting • HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory • Priority Queues • Heaps allow inserting an element and extracting the smallest element in a set, both in O(log N) time
What is a heap? • A heap is a binary tree with two properties • Order: the key at any node is less than or equal to the keys in the node’s children • the least element is at the root of the tree • there is no relative order of left and right children • Shape: all leaves are on at most two levels, with those on the bottom level asfar left as possible, and there are no “holes”
Implementing a heap • There are many possible representations of binary trees • The shape property allows us to use an array with the tree information only implicit in the representation
Operation on a tree with Shape Various functions on the tree are defined as follows: Root = 1 Key(i) = A[i] LeftChild(i) = 2*i RightChild(i) = 2*i+1 Parent(i) = i / 2 Null(i) = (i < 1) || (i > N)
Representation 12 20 15 29 23 17 22 35 40 26 51 19 1 12
Heap property • Since the Shape property is guaranteed by the representation, we will use the name Heap to mean that the key in any node is greater than or equal to the key in its parentHeap: 2iN A[i/2] A[i] • More generally, A[L..U] has the Heap property if Heap(L,U): 2LiU A[i/2] A[i]
Two Critical Routines • These two routines are used to fix an array whose Heap property is broken at one end or the other • Siftup - used when A[1..N-1] is a heap, but A[1..N] is not • Siftdown - used then A[2..N] is a heap, but A[1..N] is not
SiftUp illustrated (1) out of order
SiftUp illustrated (2) Swapped
SiftUp illustrated (3) Swapped
Siftup • This process continues until the circled node is greater than or equal to its parent, or until it is the root of the tree.
Siftup routine procedure SiftUp(int N)//pre: Heap(1,N-1), N > 0//post: Heap(1,N)int i = N;loop if (i == 1) break; // i is the root int p = i /2; // p is the parent if (A[p] <= A[i]) break; //in order swap(A[p], A[i]); i = p;end loop;
Performance of SiftUp • Since SiftUp does constant work at each level of the heap (binary tree), it takes time proportional to log N
SiftDown • Assigning a new value to A[1] when A[1..N] is a heap leaves Heap(2,N). • Routine SiftDown restores Heap(1,N). • A[1] is sifted down until it has no children or is less than or equal to the children it does have
SiftDown Illustrated out of order
SiftDown Illustrated (2) Swapped
SiftDown Illustrated (3) Swapped
SiftDown routine procedure SiftDown(int N)//pre: Heap(2,N), N >= 0//post: Heap(1,N)int i = 1;loop int c = 2*i; if (c > N) break; //no children if (c+1 <= N) if (A[c+1] < A[c]) c = c+1; // c -- least child if (A[i] <= A[c]) break; swap(A[c], A[i]); i = c;end loop;
Performance of SiftDown • Since SiftDown does constant work at each level of the heap (binary tree), it takes time proportional to log N
Priority Queue • Implementation of Insert procedure Insert(t) if (N >= MaxSize) throw OverflowException N++; A[N] = t; // Heap(1, N-1) SiftUp(N); // Heap(1,N)
Priority Queue • Implementation of ExtractMin procedure ExtractMin() if (N < 1) throw UnderflowException t = A[1]; A[1] = A[N]; N--; // Heap(2, N) SiftDown(N); // Heap(1,N) return t;
A Sorting Algorithm • A very simple sorting algorithm based on priority queues://sort array B[1..N]for (i=1; i<=N, i++) Insert(B[i]); for (i=1; i<=N, i++) B[i] = ExtractMin();
Sorting Algorithm • This sorting algorithm has a worst-case cost of O(N log N) • The array used for the heap requires additional N memory locations • HeapSort improves on this by sorting the array in place
HeapSort routine for(i = 2; i<=N; i++) SiftUp(i)for(i = N; i > 1; i--) { Swap(A[1],A[i]); SiftDown(i-1);}
HeapSort Performance • HeapSort does N-1 SiftUps and N-1 SiftDowns • Each SiftUp and SiftDown is O(log N) • Thus, HeapSort is O(N log N)