52
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

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

Embed Size (px)

Citation preview

Page 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

Leftist HeapsText• 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

Page 2: 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

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

Page 3: 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

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:

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 distance between from that node to a descendant with 0 or 1 child.If a node is null, its null path length is −1.

Page 4: 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

Example: Null Path Length

19

2027

4325

12

15

8

4

node 4 8 19 12 15 25 27 20 43

npl 1 0 1 1 0 0 0 0 0

Page 5: 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

Example: Null Path Length

19

2027

4325

12

15

8

4

node 4 8 19 12 15 25 27 20 43

npl 1 0 1 1 0 0 0 0 0

0 1

1

0

00

0 0

What are the npl’s of the children of 20 and the right child of 27?

What is the npl of the right child of 8?

Page 6: 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

Example: Null Path Length

19

2027

4325

12

15

8

4

node 4 8 19 12 15 25 27 20 43

npl 1 0 1 1 0 0 0 0 0

•node 4 violates leftist heap property fix by swapping children

0 1

1

0

00

0 0

Page 7: 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

Leftist Heap

4

node 4 8 19 12 15 25 27 20 43

npl 1 0 1 1 0 0 0 0 0

25

12

15

8 0

1

0 0

19

2027

43

1

00

0

Page 8: 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

Merging Leftist Heaps

4

25

12

15

819

2027

43

6

78

14

Consider two leftist heaps …

Task: merge them into a single leftist heap

Page 9: 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

Merging Leftist Heaps

4

25

12

15

819

2027

43

6

78

14

First, instantiate a Stack

Page 10: 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

Merging Leftist Heaps

4

25

12

15

819

2027

43

6

78

14

Compare root nodesmerge(x,y)

x y

Page 11: 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

Merging Leftist Heaps

4

25

12

15

819

2027

43

6

78

14

Remember smaller value

4

x y

Page 12: 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

Merging Leftist Heaps

4

25

12

15

819

2027

43

6

78

14

Repeat the process with the right child of the smaller value

4

x

y

Page 13: 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

Merging Leftist Heaps

4

25

12

15

819

2027

43

6

78

14

Remember smaller value

6

4

x

y

Page 14: 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

Merging Leftist Heaps

4

25

12

15

819

2027

43

6

78

14

Repeat the process with the right child of the smaller value

6

4

x y

Page 15: 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

Merging Leftist Heaps

4

25

12

15

819

2027

43

6

78

14

Remember smaller value

7

6

4

x y

Page 16: 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

Merging Leftist Heaps

4

25

12

15

819

2027

43

6

78

14

Repeat the process with the right child of the smaller value

7

6

4

x

y

null

Page 17: 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

Merging Leftist Heaps

4

25

12

15

819

2027

43

6

78

14

Because one of the arguments is null, return the other argument

7

6

4

x

8

Page 18: 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

Merging Leftist Heaps

4

25

12

15

19

2027

43

6

8

14

Make 8 the right child of 7

7

6

4

x

8

8

7Refers to node 8

Page 19: 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

Merging Leftist Heaps

4

25

12

15

19

2027

43

6

8

14

Make 7 leftist (by swapping children)

7

6

4

8

8

7Refers to node 8

Page 20: 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

Merging Leftist Heaps

4

25

12

15

19

2027

43

6

8

14

Return node 7

6

4

7

8

7Refers to node 8

Page 21: 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

Merging Leftist Heaps

4

25

12

15

19

2027

43

6

8

14

Make 7 the right child of 6 (which it already is)

6

4

7

8

7Refers to node 8

Page 22: 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

Merging Leftist Heaps

4

25

12

15

19

2027

43

6

8

14

Make 6 leftist (it already is)

6

4

7

8

7Refers to node 8

Page 23: 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

Merging Leftist Heaps

4

25

12

15

19

2027

43

6

8

14

Return node 6

46

8

7Refers to node 8

Page 24: 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

Merging Leftist Heaps

4

25

12

15

19

2027

43

6

8

14

Make 6 the right child of 4

46

8

7

Page 25: 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

Merging Leftist Heaps

4

25

12

15

19

2027

43

6

8

14

Make 4 leftist (it already is)

46

8

7

Page 26: 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

Final Leftist Heap

4

25

12

15

19

2027

43

6

8

14

Return node 4

4

8

7

• Verify that the tree is heap

• Verify that the heap is leftist

Page 27: 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

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)

Page 28: 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

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.

Page 29: 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

Skew HeapsText• 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

Page 30: 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

Motivation

• Simplify leftist heap by

– not maintaining null path lengths

– swapping children at every merge step

Page 31: 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

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:

1. X.value ≤ L.value2. 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:

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

Page 32: 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

Merging Skew Heaps

4

25

12

15

819

2027

43

6

78

14

Consider two skew heaps …

Task: merge them into a single skew heap

Page 33: 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

Merging Skew Heaps

4

25

12

15

819

2027

43

6

78

14

First, instantiate a Stack

Page 34: 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

Merging Skew Heaps

4

25

12

15

819

2027

43

6

78

14

Compare root nodesmerge(x,y)

x y

Page 35: 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

Merging Skew Heaps

4

25

12

15

819

2027

43

6

78

14

Remember smaller value

4

x y

Page 36: 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

Merging Skew Heaps

4

25

12

15

819

2027

43

6

78

14

Repeat the process with the right child of the smaller value

4

x

y

Page 37: 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

Merging Skew Heaps

4

25

12

15

819

2027

43

6

78

14

Remember smaller value

6

4

x

y

Page 38: 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

Merging Skew Heaps

4

25

12

15

819

2027

43

6

78

14

Repeat the process with the right child of the smaller value

6

4

x y

Page 39: 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

Merging Skew Heaps

4

25

12

15

819

2027

43

6

78

14

Remember smaller value

7

6

4

x y

Page 40: 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

Merging Skew Heaps

4

25

12

15

819

2027

43

6

78

14

Repeat the process with the right child of the smaller value

7

6

4

x

y

null

Page 41: 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

Merging Skew Heaps

4

25

12

15

819

2027

43

6

78

14

Because one of the arguments is null, return the other argument

7

6

4

x

8

Page 42: 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

Merging Skew Heaps

4

25

12

15

19

2027

43

6

8

14

Make 8 the right child of 7

7

6

4

x

8

8

7Refers to node 8

Page 43: 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

Merging Skew Heaps

4

25

12

15

19

2027

43

6

8

14

Swap children of node 7

7

6

4

8

8

7Refers to node 8

Page 44: 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

Merging Skew Heaps

4

25

12

15

19

2027

43

6

8

14

Return node 7

6

4

7

8

7Refers to node 8

Page 45: 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

Merging Skew Heaps

4

25

12

15

19

2027

43

6

8

14

Make 7 the right child of 6 (which it already is)

6

4

7

8

7Refers to node 8

Page 46: 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

Merging Skew Heaps

4

25

12

15

19

2027

43

6

8

14

Swap children of node 6

6

4

7

8

7Refers to node 8

Page 47: 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

Merging Skew Heaps

4

19

2027

43

Return node 6

46

Refers to node 8

25

12

15

6

8

148

7

Page 48: 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

Merging Skew Heaps

Make 6 the right child of 4

46

4

19

2027

43

25

12

15

6

8

148

7

Page 49: 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

Merging Skew Heaps

Swap children of node 4

46

4

19

2027

43

25

12

15

6

8

148

7

Page 50: 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

Final Skew Heap

Return node 4

4

• Verify that the tree is heap

• Verify that the heap is skew

4

19

2027

43

25

12

15

6

8

148

7

Page 51: 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

Analysis

• Height of a skew 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)

Page 52: 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

Inserts and Deletes

• To insert a node into a skew heap, merge the leftist heap with the node

• After deleting root X from a skew heap, merge its left and right subheaps

• In summary, there is only one operation, a merge.