1 / 52

Leftist Heaps

Leftist Heaps. Text Read Weiss, § 23.1 (Skew Heaps) Leftist Heap Definition of null path length Definition of leftist heap Building a Leftist Heap Sequence of inserts Re-heapification if leftist heap property is violated. Motivation.

roch
Download Presentation

Leftist 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. Leftist Heaps • Text • Read Weiss, §23.1 (Skew Heaps) • Leftist Heap • Definition of null path length • Definition of leftist heap • Building a Leftist Heap • Sequence of inserts • Re-heapification if leftist heap property is violated

  2. Motivation • A binary heap provides O(log n) inserts and O(log n) deletes but suffers from O(n log n) merges • A leftist heap offers O(log n) inserts and O(log n) deletes and O(log n) merges • Note, however, leftist heap inserts and deletes are more expensive than Binary Heap inserts and deletes

  3. Definition A Leftist (min)Heap is a binary tree that satisfies the follow-ing conditions. If X is a node and L and R are its left and right children, then: • X.value ≤ L.value • X.value ≤ R.value • null path length of L ≥ null path length of R where the null path length of a node is the shortest distance between from that node to a descendant with 0 or 1 child. If a node is null, its null path length is −1.

  4. Example: Null Path Length 4 8 19 12 27 20 15 25 43 node 4 8 19 12 15 25 27 20 43 npl 1 0 1 1 0 0 0 0 0

  5. Example: Null Path Length 4 8 19 0 1 What is the npl of the right child of 8? 12 1 27 20 0 0 What are the npl’s of the children of 20 and the right child of 27? 15 25 43 0 0 0 node 4 8 19 12 15 25 27 20 43 npl 1 0 1 1 0 0 0 0 0

  6. Example: Null Path Length • node 4 violates leftist heap property  fix by swapping children 4 8 19 0 1 12 1 27 20 0 0 15 25 43 0 0 0 node 4 8 19 12 15 25 27 20 43 npl 1 0 1 1 0 0 0 0 0

  7. 8 19 0 1 12 1 27 20 0 0 15 25 43 0 0 0 Leftist Heap 4 node 4 8 19 12 15 25 27 20 43 npl 1 0 1 1 0 0 0 0 0

  8. Merging Leftist Heaps Consider two leftist heaps … 4 6 8 19 8 7 12 14 27 20 15 25 43 Task: merge them into a single leftist heap

  9. Merging Leftist Heaps 4 6 8 19 8 7 12 14 27 20 15 25 43 First, instantiate a Stack

  10. Merging Leftist Heaps x y 4 6 8 19 8 7 12 14 27 20 15 25 43 Compare root nodes merge(x,y)

  11. Merging Leftist Heaps x y 4 4 6 8 19 8 7 12 14 27 20 15 25 43 Remember smaller value

  12. Merging Leftist Heaps y 4 x 4 6 8 19 8 7 12 14 27 20 15 25 43 Repeat the process with the right child of the smaller value

  13. Merging Leftist Heaps y 6 4 x 4 6 8 19 8 7 12 14 27 20 15 25 43 Remember smaller value

  14. Merging Leftist Heaps 6 4 x 4 6 y 8 19 8 7 12 14 27 20 15 25 43 Repeat the process with the right child of the smaller value

  15. Merging Leftist Heaps 7 6 4 x 4 6 y 8 19 8 7 12 14 27 20 15 25 43 Remember smaller value

  16. Merging Leftist Heaps 7 6 4 x 4 6 8 19 8 7 y 12 14 27 20 null 15 25 43 Repeat the process with the right child of the smaller value

  17. Merging Leftist Heaps 7 6 4 x 4 6 8 19 8 7 12 14 27 20 15 25 43 8 Because one of the arguments is null, return the other argument

  18. Merging Leftist Heaps 7 6 4 4 6 x Refers to node 8 19 8 7 8 14 27 20 12 43 8 15 25 Make 8 the right child of 7

  19. Merging Leftist Heaps 7 6 4 4 6 Refers to node 8 19 8 7 8 14 27 20 12 43 8 15 25 Make 7 leftist (by swapping children)

  20. Merging Leftist Heaps 6 4 4 6 Refers to node 8 19 8 7 8 14 27 20 12 43 15 25 7 Return node 7

  21. Merging Leftist Heaps 6 4 4 6 Refers to node 8 19 8 7 8 14 27 20 12 43 15 25 7 Make 7 the right child of 6 (which it already is)

  22. Merging Leftist Heaps 6 4 4 6 Refers to node 8 19 8 7 8 14 27 20 12 43 15 25 7 Make 6 leftist (it already is)

  23. Merging Leftist Heaps 4 4 6 Refers to node 8 19 8 7 8 14 27 20 12 43 15 25 6 Return node 6

  24. Merging Leftist Heaps 4 4 6 19 8 7 27 20 8 14 43 12 15 25 6 Make 6 the right child of 4

  25. Merging Leftist Heaps 4 4 6 19 8 7 27 20 8 14 43 12 15 25 6 Make 4 leftist (it already is)

  26. Final Leftist Heap 4 6 19 8 7 27 20 8 14 43 12 15 25 • Verify that the tree is heap • Verify that the heap is leftist 4 Return node 4

  27. Analysis • Height of a leftist heap ≈ O(log n) • Maximum number of values stored in Stack ≈ 2 * O(log n) ≈ O(log n) • Total cost of merge ≈ O(log n)

  28. Inserts and Deletes • To insert a node into a leftist heap, merge the leftist heap with the node • After deleting root X from a leftist heap, merge its left and right subheaps • In summary, there is only one operation, a merge.

  29. Skew Heaps • Text • Read Weiss, §6.7 • Skew Heap • No need for null path length • Definition of skew heap • Building a Skew Heap • Sequence of inserts • Swap children at every merge step

  30. Motivation • Simplify leftist heap by • not maintaining null path lengths • swapping children at every merge step

  31. Definition A Skew (min)Heap is a binary tree that satisfies the follow-ing conditions. If X is a node and L and R are its left and right children, then: • X.value ≤ L.value • X.value ≤ R.value A Skew (max)Heap is a binary tree that satisfies the follow-ing conditions. If X is a node and L and R are its left and right children, then: • X.value ≥ L.value • X.value ≥ R.value

  32. Merging Skew Heaps Consider two skew heaps … 4 6 8 19 8 7 12 14 27 20 15 25 43 Task: merge them into a single skew heap

  33. Merging Skew Heaps 4 6 8 19 8 7 12 14 27 20 15 25 43 First, instantiate a Stack

  34. Merging Skew Heaps x y 4 6 8 19 8 7 12 14 27 20 15 25 43 Compare root nodes merge(x,y)

  35. Merging Skew Heaps x y 4 4 6 8 19 8 7 12 14 27 20 15 25 43 Remember smaller value

  36. Merging Skew Heaps y 4 x 4 6 8 19 8 7 12 14 27 20 15 25 43 Repeat the process with the right child of the smaller value

  37. Merging Skew Heaps y 6 4 x 4 6 8 19 8 7 12 14 27 20 15 25 43 Remember smaller value

  38. Merging Skew Heaps 6 4 x 4 6 y 8 19 8 7 12 14 27 20 15 25 43 Repeat the process with the right child of the smaller value

  39. Merging Skew Heaps 7 6 4 x 4 6 y 8 19 8 7 12 14 27 20 15 25 43 Remember smaller value

  40. Merging Skew Heaps 7 6 4 x 4 6 8 19 8 7 y 12 14 27 20 null 15 25 43 Repeat the process with the right child of the smaller value

  41. Merging Skew Heaps 7 6 4 x 4 6 8 19 8 7 12 14 27 20 15 25 43 8 Because one of the arguments is null, return the other argument

  42. Merging Skew Heaps 7 6 4 4 6 x Refers to node 8 19 8 7 8 14 27 20 12 43 8 15 25 Make 8 the right child of 7

  43. Merging Skew Heaps 7 6 4 4 6 Refers to node 8 19 8 7 8 14 27 20 12 43 8 15 25 Swap children of node 7

  44. Merging Skew Heaps 6 4 4 6 Refers to node 8 19 8 7 8 14 27 20 12 43 15 25 7 Return node 7

  45. Merging Skew Heaps 6 4 4 6 Refers to node 8 19 8 7 8 14 27 20 12 43 15 25 7 Make 7 the right child of 6 (which it already is)

  46. Merging Skew Heaps 6 4 4 6 Refers to node 8 7 19 8 8 14 27 20 12 43 15 25 7 Swap children of node 6

  47. Merging Skew Heaps 4 4 6 Refers to node 8 7 19 8 8 14 27 20 12 43 15 25 6 Return node 6

  48. Merging Skew Heaps 4 4 6 19 7 8 27 20 8 14 43 12 15 25 6 Make 6 the right child of 4

  49. Merging Skew Heaps 4 4 19 6 7 27 20 8 8 43 14 12 15 25 6 Swap children of node 4

  50. Final Skew Heap 4 19 6 7 27 20 8 8 43 14 12 15 25 • Verify that the tree is heap • Verify that the heap is skew 4 Return node 4

More Related