-
Notifications
You must be signed in to change notification settings - Fork 316
/
PairingHeap.scala
138 lines (119 loc) · 3.91 KB
/
PairingHeap.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
*
* -Notes-
*
* Pairing heaps are heap-ordered multiway tree structures, and can be considered simplified Fibonacci heaps.
* The analysis of pairing heaps' time complexity was initially inspired by that of splay trees. The amortized
* time per delete-min is O(log n). The operations find-min, merge, and insert run in constant time, O(1).
*
* Wikipedia: http://en.wikipedia.org/wiki/Pairing_heap
* The Pairing-Heap: A New Form of Self-Adjusting Heap: http://www.cs.cmu.edu/~sleator/papers/pairing-heaps.pdf
* Improved upper bounds for pairing heaps: http://john2.poly.edu/papers/swat00/paper.pdf
* Union-Based Heaps: https://speakerdeck.com/kachayev/union-based-heaps?slide=28
*
*/
abstract sealed class Heap[+A <% Ordered[A]] {
/**
* Min value of this heap.
*/
def min: A
/**
* Subtrees (children of this heap).
*/
def subs: List[Heap[A]]
/**
* Whether this heap is empty or not.
*/
def isEmpty: Boolean
/**
* The 'insert' function might be defined through the 'Heap.merge' function
*/
def insert[B >: A <% Ordered[B]](x: B): Heap[B] =
Heap.merge(Heap.make(x), this)
/**
* Removes the minimum element from this heap.
*
* The min element is in the root of the tree. When the root is removed, we are left with zero or more
* max trees. In two pass pairing heaps, these max trees are melded into a single max tree as follows:
*
* - Make a left to right pass over the trees, melding pairs of trees.
* - Start with the rightmost tree and meld the remaining trees (right to left) into this tree one at a time.
*
* Time (amortized) - O(log n)
* Space - O(log n)
*/
def remove: Heap[A] = Heap.pairing(subs)
/**
* Fails with message.
*/
def fail(m: String) = throw new NoSuchElementException(m)
}
/**
* Empty node representation
*/
case object Leaf extends Heap[Nothing] {
def min: Nothing = fail("An empty heap.")
def subs: List[Heap[Nothing]] = fail("An empty heap.")
def isEmpty = true
}
/**
* Non-empty node is an element with linked-list of subtrees (Pairing Heaps)
*/
case class Branch[A <% Ordered[A]](min: A, subs: List[Heap[A]]) extends Heap[A] {
def isEmpty = false
}
object Heap {
/**
* An empty heap.
*/
def empty[A]: Heap[A] = Leaf
/**
* Makes a heap node.
*/
def make[A <% Ordered[A]](x: A, subs: List[Heap[A]] = List[Heap[A]]()) =
Branch(x, subs)
/**
* Merges two given heaps. Also known as 'union' or 'meld'.
*
* Two min pairing heaps may be melded into a single min pairing heap by performing a compare-link operation.
* In a compare-link, the roots of the two min trees are compared and the min tree that has the bigger root
* is made the leftmost subtree of the other tree (ties are broken arbitrarily).
*
* Time (amortized) - O(1)
* Space - O(1)
*/
def merge[A <% Ordered[A]](x: Heap[A], y: Heap[A]): Heap[A] = (x, y) match {
case (_, Leaf) => x
case (Leaf, _) => y
case (Branch(x1, subs1), Branch(x2, subs2)) =>
if (x1 < x2) Branch(x1, Branch(x2, subs2) :: subs1)
else Branch(x2, Branch(x2, Branch(x1, subs1) :: subs2))
}
/**
* Auxiliary function to merge list of pairing heaps one-by-one starting from the head of the list.
* Procedure is also known as 'melding'.
*/
def pairing[A <% Ordered[A]](subs: Heap[A]): Heap[A] = subs match {
case Nil => Leaf
case hd :: Nil => hd
case h1 :: h2 :: tail => pairing(merge(h1, h2) :: tail)
}
/**
* Builds a pairing heap from an unordered linked list.
*
* Time - O(n)
* Space - O(log n)
*/
def fromList[A <% Ordered[A]](ls: List[A]): Heap[A] = {
def loop(hs: List[Heap[A]]): Heap[A] = hs match {
case hd :: Nil => hd
case _ => loop(pass(hs))
}
def pass(hs: List[Heap[A]]): List[Heap[A]] = hs match {
case hd :: nk :: tl => Heap.merge(hd, nk) :: pass(tl)
case _ => hs
}
if (ls.isEmpty) Heap.empty
else loop(ls.map(Heap.make(_)))
}
}