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

Post on 09-Oct-2020

8 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Leftist Heaps and Skew Heaps

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

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

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

Example: Null Path Length

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

Example: Null Path Length

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

Example: Null Path Length

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

Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Merging Leftist Heaps

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

Final Leftist Heap

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

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

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

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

Merging Skew Heaps

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

Merging Skew Heaps

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

Merging Skew Heaps

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

Merging Skew Heaps

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

Merging Skew Heaps

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

Merging Skew Heaps

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

Merging Skew Heaps

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

Merging Skew Heaps

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

Merging Skew Heaps

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

Merging Skew Heaps

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

Merging Skew Heaps

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

Final Skew Heap

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

top related