-
Notifications
You must be signed in to change notification settings - Fork 292
/
PairingHeap.cs
255 lines (208 loc) · 6.58 KB
/
PairingHeap.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Advanced.Algorithms.DataStructures;
/// <summary>
/// A pairing minMax heap implementation.
/// </summary>
public class PairingHeap<T> : IEnumerable<T> where T : IComparable
{
private readonly IComparer<T> comparer;
private readonly bool isMaxHeap;
private readonly Dictionary<T, List<PairingHeapNode<T>>> heapMapping = new();
private PairingHeapNode<T> root;
public PairingHeap(SortDirection sortDirection = SortDirection.Ascending)
{
isMaxHeap = sortDirection == SortDirection.Descending;
comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
}
public int Count { get; private set; }
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return heapMapping.SelectMany(x => x.Value).Select(x => x.Value).GetEnumerator();
}
/// <summary>
/// Insert a new Node.
/// Time complexity: O(1).
/// </summary>
public void Insert(T newItem)
{
var newNode = new PairingHeapNode<T>(newItem);
root = Meld(root, newNode);
AddMapping(newItem, newNode);
Count++;
}
/// <summary>
/// Time complexity: O(log(n)).
/// </summary>
public T Extract()
{
var minMax = root;
RemoveMapping(minMax.Value, minMax);
Meld(root.ChildrenHead);
Count--;
return minMax.Value;
}
/// <summary>
/// Time complexity: O(log(n)).
/// </summary>
public void UpdateKey(T currentValue, T newValue)
{
var node = heapMapping[currentValue]?.Where(x => x.Value.Equals(currentValue)).FirstOrDefault();
if (node == null) throw new Exception("Current value is not present in this heap.");
if (comparer.Compare(newValue, node.Value) > 0)
throw new Exception($"New value is not {(!isMaxHeap ? "less" : "greater")} than old value.");
UpdateNodeValue(currentValue, newValue, node);
if (node == root) return;
DeleteChild(node);
root = Meld(root, node);
}
/// <summary>
/// Merge another heap with this heap.
/// Time complexity: O(1).
/// </summary>
public void Merge(PairingHeap<T> pairingHeap)
{
root = Meld(root, pairingHeap.root);
Count = Count + pairingHeap.Count;
}
/// <summary>
/// Time complexity: O(1).
/// </summary>
public T Peek()
{
if (root == null)
throw new Exception("Empty heap");
return root.Value;
}
/// <summary>
/// Time complexity: O(log(n))
/// </summary>
private void Meld(PairingHeapNode<T> headNode)
{
if (headNode == null)
return;
var passOneResult = new List<PairingHeapNode<T>>();
var current = headNode;
if (current.Next == null)
{
headNode.Next = null;
headNode.Previous = null;
passOneResult.Add(headNode);
}
else
{
while (true)
{
if (current == null) break;
if (current.Next != null)
{
var next = current.Next;
var nextNext = next.Next;
passOneResult.Add(Meld(current, next));
current = nextNext;
}
else
{
var lastInserted = passOneResult[passOneResult.Count - 1];
passOneResult[passOneResult.Count - 1] = Meld(lastInserted, current);
break;
}
}
}
var passTwoResult = passOneResult[passOneResult.Count - 1];
if (passOneResult.Count == 1)
{
root = passTwoResult;
return;
}
for (var i = passOneResult.Count - 2; i >= 0; i--)
{
current = passOneResult[i];
passTwoResult = Meld(passTwoResult, current);
}
root = passTwoResult;
}
/// <summary>
/// makes the smaller node parent of other and returns the Parent
/// </summary>
private PairingHeapNode<T> Meld(PairingHeapNode<T> node1,
PairingHeapNode<T> node2)
{
if (node2 != null)
{
node2.Previous = null;
node2.Next = null;
}
if (node1 == null) return node2;
node1.Previous = null;
node1.Next = null;
if (node2 != null && comparer.Compare(node1.Value, node2.Value) <= 0)
{
AddChild(ref node1, node2);
return node1;
}
AddChild(ref node2, node1);
return node2;
}
/// <summary>
/// Add new child to parent node
/// </summary>
private void AddChild(ref PairingHeapNode<T> parent, PairingHeapNode<T> child)
{
if (parent.ChildrenHead == null)
{
parent.ChildrenHead = child;
child.Previous = parent;
return;
}
var head = parent.ChildrenHead;
child.Previous = head;
child.Next = head.Next;
if (head.Next != null) head.Next.Previous = child;
head.Next = child;
}
/// <summary>
/// delete node from parent
/// </summary>
private void DeleteChild(PairingHeapNode<T> node)
{
//if this node is the child head pointer of parent
if (node.IsHeadChild)
{
var parent = node.Previous;
//use close sibling as new parent child pointer
if (node.Next != null) node.Next.Previous = parent;
parent.ChildrenHead = node.Next;
}
else
{
//just do regular deletion from linked list
node.Previous.Next = node.Next;
if (node.Next != null) node.Next.Previous = node.Previous;
}
}
private void AddMapping(T newItem, PairingHeapNode<T> newNode)
{
if (heapMapping.ContainsKey(newItem))
heapMapping[newItem].Add(newNode);
else
heapMapping[newItem] = new List<PairingHeapNode<T>>(new[] { newNode });
}
private void UpdateNodeValue(T currentValue, T newValue, PairingHeapNode<T> node)
{
RemoveMapping(currentValue, node);
node.Value = newValue;
AddMapping(newValue, node);
}
private void RemoveMapping(T currentValue, PairingHeapNode<T> node)
{
heapMapping[currentValue].Remove(node);
if (heapMapping[currentValue].Count == 0) heapMapping.Remove(currentValue);
}
}