1 / 79

Mergeable Heaps

Learn about binary heaps, max heap vs. min heap, and operations like Maximum, ExtractMax, Insert, IncreaseElement, and BuildHeap. Understand Heapify and Correctness of operations.

khoey
Download Presentation

Mergeable 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. Mergeable Heaps David Kauchak cs302 Spring 2013

  2. Admin • Homework 7?

  3. Binary heap A binary tree where the value of a parent is greater than or equal to the value of it’s children Additional restriction: all levels of the tree are complete except the last Max heap vs. min heap

  4. Binary heap - operations Maximum(S) - return the largest element in the set ExtractMax(S) – Return and remove the largest element in the set Insert(S, val) – insert val into the set IncreaseElement(S, x, val) – increase the value of element x to val BuildHeap(A) – build a heap from an array of elements

  5. 16 14 10 8 7 9 3 2 4 1 16 14 10 1 2 3 4 5 6 7 8 9 10 8 2 4 1 7 9 3 Binary heap representations

  6. Heapify Assume left and right children are heaps, turn current set into a valid heap

  7. Heapify Assume left and right children are heaps, turn current set into a valid heap

  8. Heapify Assume left and right children are heaps, turn current set into a valid heap find out which is largest: current, left of right

  9. Heapify Assume left and right children are heaps, turn current set into a valid heap

  10. Heapify Assume left and right children are heaps, turn current set into a valid heap if a child is larger, swap and recurse

  11. 16 10 8 2 4 1 7 9 5 Heapify 16 3 10 8 7 9 5 2 4 1 1 2 3 4 5 6 7 8 9 10 3

  12. 16 10 8 2 4 1 7 9 5 Heapify 16 3 10 8 7 9 5 2 4 1 1 2 3 4 5 6 7 8 9 10 3

  13. 16 10 3 2 4 1 7 9 5 Heapify 16 8 10 3 7 9 5 2 4 1 1 2 3 4 5 6 7 8 9 10 8

  14. 16 10 3 2 4 1 7 9 5 Heapify 16 8 10 3 7 9 5 24 1 1 2 3 4 5 6 7 8 9 10 8

  15. 16 10 4 2 3 1 7 9 5 Heapify 16 8 10 4 7 9 5 2 3 1 1 2 3 4 5 6 7 8 9 10 8

  16. 16 10 4 2 3 1 7 9 5 Heapify 16 8 10 4 7 9 5 2 3 1 1 2 3 4 5 6 7 8 9 10 8

  17. 16 10 4 2 3 1 7 9 5 Heapify 16 8 10 4 7 9 5 2 3 1 1 2 3 4 5 6 7 8 9 10 8

  18. Correctness of Heapify

  19. Correctness of Heapify Base case: • Heap with a single element • Trivially a heap

  20. Correctness of Heapify Both children are valid heaps Three cases: Case 1: A[i] (current node) is the largest • parent is greater than both children • both children are heaps • current node is a valid heap

  21. Correctness of Heapify Case 2: left child is the largest • When Heapify returns: • Left child is a valid heap • Right child is unchanged and therefore a valid heap • Current node is larger than both children since we selected the largest node of current, left and right • current node is a valid heap Case 3: right child is largest • similar to above

  22. Running time of Heapify What is the cost of each individual call to Heapify (not counting recursive calls)? • Θ(1) How many calls are made to Heapify? • O(height of the tree) What is the height of the tree? • Complete binary tree, except for the last level O(log n)

  23. Binary heap - operations Maximum(S) - return the largest element in the set ExtractMax(S) – Return and remove the largest element in the set Insert(S, val) – insert val into the set IncreaseElement(S, x, val) – increase the value of element x to val BuildHeap(A) – build a heap from an array of elements

  24. 16 14 10 8 7 9 3 2 4 1 1 2 3 4 5 6 7 8 9 10 Maximum Return the largest element from the set Return A[1]

  25. 16 14 10 8 2 4 1 7 9 3 ExtractMax Return and remove the largest element in the set

  26. 14 10 8 2 4 1 7 9 3 ExtractMax Return and remove the largest element in the set ?

  27. 14 10 8 2 4 1 7 9 3 ExtractMax Return and remove the largest element in the set ?

  28. 14 10 8 2 4 1 7 9 3 ExtractMax Return and remove the largest element in the set ?

  29. 14 10 8 2 4 1 7 9 3 ExtractMax Return and remove the largest element in the set ?

  30. 14 10 8 2 4 1 7 9 3 ExtractMax Return and remove the largest element in the set

  31. 14 10 8 2 4 1 7 9 3 ExtractMax Return and remove the largest element in the set Heapify

  32. 10 4 2 1 7 9 3 ExtractMax Return and remove the largest element in the set Heapify 14 8

  33. ExtractMax Return and remove the largest element in the set

  34. ExtractMax running time Constant amount of work plus one call to Heapify – O(log n)

  35. 16 14 10 8 2 4 1 7 9 3 IncreaseElement Increase the value of element x to val 15

  36. 16 14 10 8 2 1 7 9 3 IncreaseElement Increase the value of element x to val 15

  37. 16 14 10 2 1 7 9 3 IncreaseElement Increase the value of element x to val 15 8

  38. 16 14 10 2 8 1 7 9 3 IncreaseElement Increase the value of element x to val 15

  39. 16 15 10 2 8 1 7 9 3 IncreaseElement Increase the value of element x to val 14

  40. IncreaseElement Increase the value of element x to val

  41. Correctness of IncreaseElement Why is it ok to swap values with parent?

  42. Correctness of IncreaseElement Stop when heap property is satisfied

  43. Running time of IncreaseElement Follows a path from a node to the root Worst case O(height of the tree) O(log n)

  44. 16 14 10 8 2 4 1 7 9 3 Insert Insert val into the set 6

  45. 16 14 10 8 2 4 1 7 9 3 Insert Insert val into the set 6

  46. 16 14 10 8 2 4 1 7 9 3 Insert Insert val into the set propagate value up 6

  47. Insert

  48. Running time of Insert Constant amount of work plus one call to IncreaseElement – O(log n)

  49. Building a heap Can we build a heap using the functions we have so far? • Maximum(S) • ExtractMax(S) • Insert(S, val)| • IncreaseElement(S, x, val)

  50. Building a heap

More Related