leftist heaps and skew heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. ·...

41
Leftist Heaps and Skew Heaps (Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 1 / 41

Upload: others

Post on 09-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Leftist Heaps and Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 1 / 41

Page 2: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Leftist Heaps

A binary heap provides O(log n) inserts and O(log n) deletes butsuffers from O(n log n) mergesA leftist heap offers O(log n) inserts and O(log n) deletes andO(logn) mergesNote, however, leftist heap inserts and deletes are more expensivethan Binary Heap inserts and deletes

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 2 / 41

Page 3: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Leftist Heaps: Definition

A Leftist (min)Heap is a binary tree that satisfies the followingconditions. If X is a node and L and R are its left and rightchildren, then:

1 X.value ≤ L.value2 X.value ≤ R.value3 null path length of L ≥ null path length of R

where the null path length of a node is the shortest between fromthat node to a descendant with 0 or 1 child. If a node is null, itsnull path length is -1.

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 3 / 41

Page 4: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Example: Null Path Length

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 4 / 41

Page 5: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Example: Null Path Length

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 5 / 41

Page 6: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Example: Null Path Length

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 6 / 41

Page 7: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 7 / 41

Page 8: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 8 / 41

Page 9: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 9 / 41

Page 10: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 10 / 41

Page 11: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 11 / 41

Page 12: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 12 / 41

Page 13: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 13 / 41

Page 14: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 14 / 41

Page 15: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 15 / 41

Page 16: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 16 / 41

Page 17: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 17 / 41

Page 18: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 18 / 41

Page 19: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 19 / 41

Page 20: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 20 / 41

Page 21: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 21 / 41

Page 22: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 22 / 41

Page 23: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 23 / 41

Page 24: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 24 / 41

Page 25: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Leftist Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 25 / 41

Page 26: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Final Leftist Heap

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 26 / 41

Page 27: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

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)

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 27 / 41

Page 28: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Insert and Delete

To insert a node into a leftist heap, merge the leftist heap with thenodeAfter deleting root X from a leftist heap, merge its left and rightsubheapsIn summary, there is only one operation, a merge.

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 28 / 41

Page 29: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Skew Heaps

Simplify leftist heap byI not maintaining null path lengthsI swapping children at every step

A Skew (min)Heap is a binary tree that satisfies the followingconditions. If X is a node and L and R are its left and rightchildren, then:

1 X.value ≤ L.value2 X.value ≤ R.value

A Skew (max)Heap is a binary tree that satisfies the followingconditions. If X is a node and L and R are its left and rightchildren, then:

1 X.value ≥ L.value2 X.value ≥ R.value

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 29 / 41

Page 30: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 30 / 41

Page 31: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 31 / 41

Page 32: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 32 / 41

Page 33: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 33 / 41

Page 34: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 34 / 41

Page 35: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 35 / 41

Page 36: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 36 / 41

Page 37: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 37 / 41

Page 38: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 38 / 41

Page 39: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 39 / 41

Page 40: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Merging Skew Heaps

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 40 / 41

Page 41: Leftist Heaps and Skew Heapsccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-6d.pdf · 2017. 5. 22. · Leftist Heaps: Definition A Leftist (min)Heap is a binary tree that satisfies

Final Skew Heap

(Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2017 41 / 41