Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees...

21
Skew heaps

Transcript of Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees...

Page 1: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Skew heaps

Page 2: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Heaps with merging

Another useful operation is merging two heaps into oneTo do this, let's go back to binary trees with the heap property (no completeness):

We can implement the other priority queue operations in terms of merging!

8

18 29

37 32 89

20 28 39 66

Page 3: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Insertion

To insert a single element:● build a heap containing just that one element● merge it into the existing heap!

E.g., inserting 12

8

18 29

37 32 89

20 28 39 66

+ 12

A tree withjust one node

Page 4: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Delete minimum

To delete the minimum element:● take the left and right branches of the tree● these contain every element except the smallest● merge them!

E.g., deleting 8 from the previous heap

18 29

37 32 89

20 28 39 66+

Page 5: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Naive merging

How to merge these two heaps?

Idea: root of resulting heap must be 18Take heap A. Pick one of its children. Recursively merge B into that child.Let's use A's right child for no particular reason

18 29

37 32 89

20 28 39 66A = B =

Page 6: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Naive merging

To merge two non-empty heaps:Pick the heap with the smallest root:

Let C be the other heapRecursively merge B and C!

x

A B

+ C → x

A B + C

x

A B

Page 7: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Example

18 < 29 so pick 18 as the root of the merged tree

18 29

37 32 74 89

20 28 39 66+

Page 8: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Naive merging

Recursively merge the right branch of 18 and the 29 tree

18

2937

3274 89

20

2839 66+

Page 9: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Naive merging

28 < 29 so pick 28 as the root of the merged tree

18

2937

3274 89

20

2839 66+

Page 10: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Naive merging

Recursively merge the right branch of 28 and the 29 tree

18

2937

32

74 89

20

39 66+28

Page 11: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Naive merging

29 < 32 so pick 29 as the root of the merged tree

18

2937

32

74 89

20

39 66+28

Page 12: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Naive merging

Recursively merge the right branch of 29 with 32

18

2937

3274 89

20

3966+

28

Page 13: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Naive merging

Base case: merge 66 with the empty tree

Notice that the tree looks pretty “right-heavy”

18

2937

32

74 89

20

39

66

28

Page 14: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Worst case for naive merging

A right-heavy tree:

Unfortunately, you get this just by doing insertions! So insert takes O(n) time...How can we stop the tree from becoming right-heavy?

Page 15: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Skew merging

In a skew heap, after making a recursive call to merge, we swap the two children:

Amazingly, this small change completely fixes the performance of merge!We never end up with right-heavy trees.We get O(log n) amortised complexity.

x

A B

+ C → →x

A B + C

x

AB + C

Page 16: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Naive merging in code

data Heap a = Nil | Node a (Heap a) (Heap a)

root (Node x _ _) = x

merge x Nil = xmerge Nil x = xmerge x y | root x > root y = merge y xmerge (Node x a b) c = Node x a (merge b c)

Make sure thatfirst argument has

smallest root

Page 17: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Skew merging in code

data Heap a = Nil | Node a (Heap a) (Heap a)

root (Node x _ _) = x

merge x Nil = xmerge Nil x = xmerge x y | root x > root y = merge y xmerge (Node x a b) c = Node x (merge b c) a

Page 18: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Example

One way to do skew merge is to first do naive merge, then go up the tree swapping left and right children...

18 29

37 32 74 89

20 28 39 66+18

2937

32

74 89

20

39

66

28→

Naive merge

Page 19: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Example

...like this:

18

2937

32

74 89

20

39

66

28 →18

2937

32

74 89

20

39

66

28 →18

2937

32

74 89

20

39

66

28

Page 20: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Example

...like this:

→ →18

2937

32

74 89

20

39

66

28

18

29 37

32

74 89

20

39

66

28

18

2937

32

74 89

20

39

66

28

Page 21: Skew heaps - Chalmers · 2016-09-16 · Skew heaps Implementation of priority queues: binary trees with heap property skew merging avoids right-heavy trees, gives O(log n) amortised

Skew heaps

Implementation of priority queues:● binary trees with heap property● skew merging avoids right-heavy trees,

gives O(log n) amortised complexity● other operations are based on merge

A good fit for functional languages:● based on trees rather than arrays

Other data structures based on naive merging + avoiding right heavy trees:● leftist heaps (swap children when needed)● meldable heaps (swap children at random)

See webpage for link to visualisation site!