Leftist Heaps Text Read Weiss 6 6 Leftist

  • Slides: 52
Download presentation
Leftist Heaps Text • Read Weiss, § 6. 6 Leftist Heap • • Definition

Leftist Heaps Text • Read Weiss, § 6. 6 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 • A binary heap provides O(log n) inserts and O(log n) deletes but

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

Definition A Leftist (min)Heap is a binary tree that satisfies the following conditions. If

Definition A Leftist (min)Heap is a binary tree that satisfies the following conditions. If X is a node and L and R are its left and right children, then: 1. X. value ≤ L. value 2. X. value ≤ R. value 3. 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.

Example: Null Path Length 4 8 19 12 27 15 25 20 43 node

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

Example: Null Path Length 4 8 12 15 1 0 19 0 What is

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

Example: Null Path Length 4 8 12 15 • node 4 violates leftist heap

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

Leftist Heap 4 19 27 43 8 1 20 0 12 0 15 0

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

Merging Leftist Heaps Consider two leftist heaps … 4 6 8 19 27 43

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

Merging Leftist Heaps 4 6 8 19 27 43 12 20 15 8 7

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

Merging Leftist Heaps x y 4 6 8 19 27 43 12 20 15

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

Merging Leftist Heaps x y 4 6 8 19 27 43 12 20 15

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

Merging Leftist Heaps y 4 8 19 27 43 6 x 12 20 15

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

Merging Leftist Heaps y 4 8 19 27 43 6 x 12 20 15

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

Merging Leftist Heaps 4 8 19 27 43 6 x 12 20 15 8

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

Merging Leftist Heaps 4 8 19 27 43 6 x 12 20 15 8

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

Merging Leftist Heaps 4 8 19 27 43 6 x 12 20 15 8

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

Merging Leftist Heaps 4 8 19 27 43 6 x 12 20 15 8

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

Merging Leftist Heaps 4 19 27 6 8 Refers to node 8 20 x

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

Merging Leftist Heaps 4 19 27 6 8 Refers to node 8 20 7

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

Merging Leftist Heaps 4 19 27 6 8 Refers to node 8 20 7

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

Merging Leftist Heaps 4 19 27 6 8 Refers to node 8 20 7

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

Merging Leftist Heaps 4 19 27 6 8 Refers to node 8 20 7

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

Merging Leftist Heaps 4 19 27 6 8 Refers to node 8 20 7

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

Merging Leftist Heaps 4 6 19 27 43 8 20 7 8 14 12

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

Merging Leftist Heaps 4 6 19 27 43 8 20 7 8 14 12

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

Final Leftist Heap 4 6 19 27 43 8 20 7 8 14 12

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

Analysis • Height of a leftist heap ≈ O(log n) • Maximum number of

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)

Inserts and Deletes • To insert a node into a leftist heap, merge the

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.

Skew Heaps Text • Read Weiss, § 6. 7 Skew Heap • • No

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 Fix heap if leftist heap property is violated

Motivation • Simplify leftist heap by – not maintaining null path lengths – swapping

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

Definition A Skew (min)Heap is a binary tree that satisfies the following conditions. If

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

Merging Skew Heaps Consider two skew heaps … 4 6 8 19 27 43

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

Merging Skew Heaps 4 6 8 19 27 43 12 20 15 8 7

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

Merging Skew Heaps x y 4 6 8 19 27 43 12 20 15

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

Merging Skew Heaps x y 4 6 8 19 27 43 12 20 15

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

Merging Skew Heaps y 4 8 19 27 43 6 x 12 20 15

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

Merging Skew Heaps y 4 8 19 27 43 6 x 12 20 15

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

Merging Skew Heaps 4 8 19 27 43 6 x 12 20 15 8

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

Merging Skew Heaps 4 8 19 27 43 6 x 12 20 15 8

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

Merging Skew Heaps 4 8 19 27 43 6 x 12 20 15 8

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

Merging Skew Heaps 4 8 19 27 43 6 x 12 20 15 8

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

Merging Skew Heaps 4 19 27 6 8 Refers to node 8 20 x

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

Merging Skew Heaps 4 19 27 6 8 Refers to node 8 20 7

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

Merging Skew Heaps 4 19 27 6 8 Refers to node 8 20 7

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

Merging Skew Heaps 4 19 27 6 8 Refers to node 8 20 7

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

Merging Skew Heaps 4 19 27 6 7 Refers to node 8 8 20

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

Merging Skew Heaps 4 19 27 6 7 Refers to node 8 8 20

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

Merging Skew Heaps 4 6 19 27 7 20 8 43 8 14 12

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

Merging Skew Heaps 4 19 6 7 8 27 8 14 20 43 12

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

Final Skew Heap 4 19 6 7 8 27 8 14 20 43 12

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

Analysis • Height of a leftist heap ≈ O(log n) • Maximum number of

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)

Inserts and Deletes • To insert a node into a leftist heap, merge the

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.