1 / 29

Heaps

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?.

ora
Download Presentation

Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Heaps Chapter 21

  2. 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

  3. 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”

  4. Here is a heap of 12 integers

  5. 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

  6. A 12-element tree as an array

  7. 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)

  8. Representation 12 20 15 29 23 17 22 35 40 26 51 19 1 12

  9. 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: 2iN A[i/2]  A[i] • More generally, A[L..U] has the Heap property if Heap(L,U): 2LiU A[i/2]  A[i]

  10. 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

  11. SiftUp illustrated (1) out of order

  12. SiftUp illustrated (2) Swapped

  13. SiftUp illustrated (3) Swapped

  14. 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.

  15. 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;

  16. Performance of SiftUp • Since SiftUp does constant work at each level of the heap (binary tree), it takes time proportional to log N

  17. 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

  18. SiftDown Illustrated out of order

  19. SiftDown Illustrated (2) Swapped

  20. SiftDown Illustrated (3) Swapped

  21. 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;

  22. Performance of SiftDown • Since SiftDown does constant work at each level of the heap (binary tree), it takes time proportional to log N

  23. Priority Queue Performance

  24. 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)

  25. 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;

  26. 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();

  27. 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

  28. HeapSort routine for(i = 2; i<=N; i++) SiftUp(i)for(i = N; i > 1; i--) { Swap(A[1],A[i]); SiftDown(i-1);}

  29. 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)

More Related