1 / 36

Binary Heap

Binary Heap. Binary Heap. Note: this presentation only discusses maximum binary heaps. 25. 20. 13. 18. 8. 12. 7. 11. 17. 6. Binary Heap. A binary heap is a binary tree with a key in each node such that: All leaves are on, at most, two adjacent levels.

bath
Download Presentation

Binary Heap

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. Binary Heap

  2. Binary Heap • Note: this presentation only discusses maximum binary heaps 25 20 13 18 8 12 7 11 17 6

  3. Binary Heap • A binary heap is a binary tree with a key in each node such that: • All leaves are on, at most, two adjacent levels. • All leaves on the lowest level occur to the left, and all levels except the lowest one are completely filled. • The key in the root is greater or equal to all its children, and the left and right sub trees are again binary heaps.

  4. Binary Heap • A heap will often be implementedusing an array • This implementation requires an additional field, the heap size,which is at most the array size • Enables quick and easy access toa node's parent, and to its left andright sons

  5. Properties of the heap • The largest value of the heap is always at the root • The maximal height of a heap is log(n), where n is the number of elements • The items along every path from the root to a leaf are ordered, from larger to smaller

  6. Direct Access to Parent and to Children • If a node is at index n, then its parent will be indexed n / 2 • If a node is at index n, then its left son would be indexed at 2n • If a node is at index n, then its right son would be indexed at 2n + 1

  7. Direct Access to Parent and to Children – Implementation Parent (i) return i / 2 Left (i) return 2i Right (i) return 2i + 1

  8. 1 25 20 2 13 3 18 4 8 5 12 6 7 7 11 8 17 9 10 6 25 20 13 18 8 12 7 11 17 6 Binary Heap 1 2 3 4 7 5 6 8 9 10

  9. Perc-Down Perc-Down (A, i) l  Left(i) r  Right(i) if l ≤ heap-size[A] and A[l] > A[i] thenlargest = l elselargest = i if r ≤ heap-size[A] and A[r] > A[largest] thenlargest = r iflargest ≠ i then exchange A[i]  A[largest] Perc-Down (A, largest)

  10. Perc-Down (A, 1) 8 25 13 18 20 12 7 11 17 6

  11. The Heap Property • The ancestor relation defines a partial order on the heap elements: • Reflexive: x is an ancestor of itself • Anti-symmetric: if x is an ancestor of y and y is an ancestor of x, then x = y • Transitive: if x is an ancestor of y and y is an ancestor of z, x is an ancestor of z

  12. The Heap Property • The partial order defined by the heap structure is weaker than that of the total order, therefore: • A heap is easier to build and maintain than a sorted data structure • A heap is less powerful than a sorted data structure

  13. Constructing a Heap • Heaps can be constructed incrementally, by inserting new elements into the left-most openspot in the array • If the new element is greater than its parent, swap their positions and recur (percolate up)

  14. Constructing a Heap • Correctness • Since at each step, we replace the root of a sub tree by a larger one, we preserve the heap property • Complexity • Doing n such insertions takes O(n log n) time, since each of the last n/2 insertions requires O(log n) time each

  15. Constructing a Heap • Another way to construct a heap (assuming that all items are known in advance), is using Build-Heap: Build-Heap (A) heap-size[A]  length[A] for i  length[A] / 2 down to 1 do Perc-Down (A, i)

  16. 4 1 3 2 16 9 10 14 8 7 Build-Heap {4,1,3,2,16,9,10,14,8,7} 4 1 3 2 16 9 10 14 8 7

  17. 4 1 3 14 16 9 10 2 8 7 Build-Heap {4,1,3,2,16,9,10,14,8,7} 4 1 3 2 16 9 10 14 8 7

  18. 4 1 10 14 16 9 3 2 8 7 Build-Heap {4,1,3,2,16,9,10,14,8,7} 4 1 3 14 16 9 10 2 8 7

  19. 4 16 10 14 7 9 3 2 8 1 Build-Heap {4,1,3,2,16,9,10,14,8,7} 4 1 10 14 16 9 3 2 8 7

  20. 16 14 10 8 7 9 3 2 4 1 Build-Heap {4,1,3,2,16,9,10,14,8,7} 4 16 10 14 7 9 3 2 8 1

  21. Constructing a Heap • The Build-Heap algorithm can be seen as a merge algorithm – wheneverPerc-Down is called for a node, both its sub-trees are already heaps • Rough Analysis of build heap produces a running time of O(n log n) • However careful analysis will show that in fact it is a linear algorithm

  22. Build-Heap Analysis • A heap of size n has nodes at height h • So there are n/2 nodes which are leaves (height 0), n/4 nodes of height 1 and finally a single node of height

  23. Build-Heap Analysis • Perc-Down runs faster for lower nodes: at height h, runs in O(h) time • Since most nodes are low,most runs of Perc-Down are fast • The total running time of Build-heap is:

  24. Build-Heap Analysis • From the formula we assign x = ½ • And finally:

  25. Priority Queue • A priority queue is a data structure that supports the following operations: • Insert (S, x) – insert x into set S • Find-Max (S) – return the largest key in S • Delete-Max (S) – return the largest key in S, and remove it • These operations can be supported naturally using a binary heap

  26. Find-Max • The largest element in the heap is the root, therefore this method simply returns A[1]

  27. Delete-Max • Replace last leaf with root, decrease heap size by 1, and Perc-Down fromthe root: Delete-Max (A) max A[1] A[1]  A[heap-size[A]] heap-size[A]  heap-size[A] – 1 Perc-Down (A,1) returnmax

  28. Insert (A, key) Insert (A, key) heap-size[A]  heap-size[A] + 1 i  heap-size[A] while (i > 1 and A[Parent(i)] < key) do A[i]  A[Parent(i)] i  Parent(i) A[i]  key

  29. Question 1 • Insert the element 101 into the heap {100, 15, 99, 7, 3, 98, 1, 4, 5, 1, 2, 70} • Delete the maximum element from the resulting heap

  30. Question 2 • Given an array-based binary heap of size n, suggest an algorithm for each of the following operations: • Finding the second largest element • Finding the third largest element • What is the complexity of each of these algorithms?

  31. Question 2 – Finding the Second Largest Element • The second largest element in the heap must be one of the two sons of the root • If it isn't, the heap property of one of the sub-trees will be violated • This can be found in constant time • The algorithm: return max (A[2], A[3])

  32. Question 2 – Finding the Third Largest Element • The third largest element is either the smaller son of the root, or one of the sons of the greater son: if (A[2] > A[3]) return max (A[3], A[4], A[5]) elsereturn max (A[2], A[6], A[7]) • A simpler solution – traverse the first 7 elements, and return the third largest

  33. Question 2 – Complexity • In both cases, the number of operations performed by the algorithm is constant • Therefore: O(1)

  34. Question 3 – From Exam • An array-based binary heap of size nis given • The key of the root is log n • The key of each node is smaller than its parent's key by 1 • What is the sum of all keys in the tree? • O(log n), O(n) , O(nlog n), O(n2)?

  35. Question 3 – From Exam log n (log n)-1 (log n)-1 … (log n)-2 (log n)-2 . . . . . . . . .

  36. Question 3 – From Exam • The key in each of the nodes is exactly the running time of Build-heap on that node • As we have seen, the total running time of Build-heap is O(n) • Therefore the sum of all keys in the tree is also O(n)

More Related