forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinomialMinHeap.cs
376 lines (308 loc) · 11.7 KB
/
BinomialMinHeap.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System;
using System.Collections.Generic;
using DataStructures.Common;
using DataStructures.Lists;
namespace DataStructures.Heaps
{
/// <summary>
/// BINOMIAL MIN HEAP Data Structure
/// </summary>
public class BinomialMinHeap<T> : IMinHeap<T> where T : IComparable<T>
{
/// <summary>
/// The Heap Node class.
/// </summary>
private class BinomialNode<T> where T : IComparable<T>
{
public T Value { get; set; }
public BinomialNode<T> Parent { get; set; }
public BinomialNode<T> Sibling { get; set; } // Right-Sibling
public BinomialNode<T> Child { get; set; } // Left-Child
// Constructors
public BinomialNode() : this(default(T), null, null, null) { }
public BinomialNode(T value) : this(value, null, null, null) { }
public BinomialNode(T value, BinomialNode<T> parent, BinomialNode<T> sibling, BinomialNode<T> child)
{
Value = value;
Parent = parent;
Sibling = sibling;
Child = child;
}
// Helper boolean flags
public bool HasSiblings
{
get { return this.Sibling != null; }
}
public bool HasChildren
{
get { return this.Child != null; }
}
}
/// <summary>
/// INSTANCE VARIABLES
/// </summary>
private int _size { get; set; }
private const int _defaultCapacity = 8;
private ArrayList<BinomialNode<T>> _forest { get; set; }
/// <summary>
/// CONSTRUCTORS
/// </summary>
public BinomialMinHeap()
: this(8)
{
// Empty constructor
}
public BinomialMinHeap(int capacity)
{
if (capacity <= 0)
throw new ArgumentOutOfRangeException();
capacity = (capacity < _defaultCapacity ? _defaultCapacity : capacity);
_size = 0;
_forest = new ArrayList<BinomialNode<T>>(capacity);
}
/************************************************************************************************/
/** PRIVATE HELPER FUNCTIONS */
/// <summary>
/// Removes root of tree at he specified index.
/// </summary>
private void _removeAtIndex(int minIndex)
{
// Get the deletedTree
// The min-root lies at _forest[minIndex]
BinomialNode<T> deletedTreeRoot = _forest[minIndex].Child;
// Exit if there was no children under old-min-root
if (deletedTreeRoot == null)
return;
// CONSTRUCT H'' (double-prime)
BinomialMinHeap<T> deletedForest = new BinomialMinHeap<T>();
deletedForest._forest.Resize(minIndex + 1);
deletedForest._size = (1 << minIndex) - 1;
for (int i = (minIndex - 1); i >= 0; --i)
{
deletedForest._forest[i] = deletedTreeRoot;
deletedTreeRoot = deletedTreeRoot.Sibling;
deletedForest._forest[i].Sibling = null;
}
// CONSTRUCT H' (single-prime)
_forest[minIndex] = null;
_size = deletedForest._size + 1;
Merge(deletedForest);
// Decrease the size
--_size;
}
/// <summary>
/// Returns index of the tree with the minimum root's value.
/// </summary>
private int _findMinIndex()
{
int i, minIndex;
// Loop until you reach a slot in the _forest that is not null.
// The final value of "i" will be pointing to the non-null _forest slot.
for (i = 0; i < _forest.Count && _forest[i] == null; ++i) ;
// Loop over the trees in forest, and return the index of the slot that has the tree with the min-valued root
for (minIndex = i; i < _forest.Count; ++i)
if (_forest[i] != null && (_forest[i].Value.IsLessThan(_forest[minIndex].Value)))
minIndex = i;
return minIndex;
}
/// <summary>
/// Combines two trees and returns the new tree root node.
/// </summary>
private BinomialNode<T> _combineTrees(BinomialNode<T> firstTreeRoot, BinomialNode<T> secondTreeRoot)
{
if (firstTreeRoot == null || secondTreeRoot == null)
throw new ArgumentNullException("Either one of the nodes or both are null.");
if (secondTreeRoot.Value.IsLessThan(firstTreeRoot.Value))
return _combineTrees(secondTreeRoot, firstTreeRoot);
secondTreeRoot.Sibling = firstTreeRoot.Child;
firstTreeRoot.Child = secondTreeRoot;
secondTreeRoot.Parent = firstTreeRoot;
return firstTreeRoot;
}
/// <summary>
/// Clones a tree, given it's root node.
/// </summary>
private BinomialNode<T> _cloneTree(BinomialNode<T> treeRoot)
{
if (treeRoot == null)
return null;
return new BinomialNode<T>() { Value = treeRoot.Value, Child = _cloneTree(treeRoot.Child), Sibling = _cloneTree(treeRoot.Sibling) };
}
/************************************************************************************************/
/** PUBLIC API FUNCTIONS */
/// <summary>
/// Returns count of elements in heap.
/// </summary>
public int Count
{
get { return _size; }
}
/// <summary>
/// Checks if heap is empty
/// </summary>
/// <returns></returns>
public bool IsEmpty
{
get { return (_size == 0); }
}
/// <summary>
/// Initializes this heap with a collection of elements.
/// </summary>
public void Initialize(IList<T> newCollection)
{
if (newCollection == null)
throw new ArgumentNullException();
if (newCollection.Count > ArrayList<T>.MAXIMUM_ARRAY_LENGTH_x64)
throw new OverflowException();
_forest = new ArrayList<BinomialNode<T>>(newCollection.Count + 1);
for (int i = 0; i < newCollection.Count; ++i)
this.Add(newCollection[i]);
}
/// <summary>
/// Inserts a new item to heap.
/// </summary>
public void Add(T heapKey)
{
var tempHeap = new BinomialMinHeap<T>();
tempHeap._forest.Add(new BinomialNode<T>(heapKey));
tempHeap._size = 1;
// Merge this with tempHeap
Merge(tempHeap);
// Increase the _size
++_size;
}
/// <summary>
/// Return the min element.
/// </summary>
public T Peek()
{
if (IsEmpty)
throw new Exception("Heap is empty.");
int minIndex = _findMinIndex();
var minValue = _forest[minIndex].Value;
return minValue;
}
/// <summary>
/// Remove the min element from heap.
/// </summary>
public void RemoveMin()
{
if (IsEmpty)
throw new Exception("Heap is empty.");
_removeAtIndex(_findMinIndex());
}
/// <summary>
/// Return the min element and then remove it from heap.
/// </summary>
public T ExtractMin()
{
if (IsEmpty)
throw new Exception("Heap is empty.");
// Get the min-node index
int minIndex = _findMinIndex();
var minValue = _forest[minIndex].Value;
// Remove min from heap
_removeAtIndex(minIndex);
return minValue;
}
/// <summary>
/// Merges the elements of another heap with this heap.
/// </summary>
public void Merge(BinomialMinHeap<T> otherHeap)
{
// Avoid aliasing problems
if (this == otherHeap)
return;
// Avoid null or empty cases
if (otherHeap == null || otherHeap.IsEmpty)
return;
BinomialNode<T> carryNode = null;
_size = _size + otherHeap._size;
// One capacity-change step
if (_size > _forest.Count)
{
int newSize = Math.Max(this._forest.Count, otherHeap._forest.Count) + 1;
this._forest.Resize(newSize);
}
for (int i = 0, j = 1; j <= _size; i++, j *= 2)
{
BinomialNode<T> treeRoot1 = (_forest.IsEmpty == true ? null : _forest[i]);
BinomialNode<T> treeRoot2 = (i < otherHeap._forest.Count ? otherHeap._forest[i] : null);
int whichCase = (treeRoot1 == null ? 0 : 1);
whichCase += (treeRoot2 == null ? 0 : 2);
whichCase += (carryNode == null ? 0 : 4);
switch (whichCase)
{
/*** SINGLE CASES ***/
case 0: /* No trees */
case 1: /* Only this */
break;
case 2: /* Only otherHeap */
this._forest[i] = treeRoot2;
otherHeap._forest[i] = null;
break;
case 4: /* Only carryNode */
this._forest[i] = carryNode;
carryNode = null;
break;
/*** BINARY CASES ***/
case 3: /* this and otherHeap */
carryNode = _combineTrees(treeRoot1, treeRoot2);
this._forest[i] = otherHeap._forest[i] = null;
break;
case 5: /* this and carryNode */
carryNode = _combineTrees(treeRoot1, carryNode);
this._forest[i] = null;
break;
case 6: /* otherHeap and carryNode */
carryNode = _combineTrees(treeRoot2, carryNode);
otherHeap._forest[i] = null;
break;
case 7: /* all the nodes */
this._forest[i] = carryNode;
carryNode = _combineTrees(treeRoot1, treeRoot2);
otherHeap._forest[i] = null;
break;
}//end-switch
}//end-for
// Clear otherHeap
otherHeap.Clear();
}
/// <summary>
/// Returns an array copy of heap.
/// </summary>
public T[] ToArray()
{
throw new NotImplementedException();
}
/// <summary>
/// Rebuilds the heap.
/// </summary>
public void RebuildHeap()
{
throw new NotImplementedException();
}
/// <summary>
/// Returns a list copy of heap.
/// </summary>
public List<T> ToList()
{
throw new NotImplementedException();
}
/// <summary>
/// Returns a binomial max-heap copy of this instance.
/// </summary>
public IMaxHeap<T> ToMaxHeap()
{
throw new NotImplementedException();
}
/// <summary>
/// Clear this instance.
/// </summary>
public void Clear()
{
_size = 0;
_forest.Clear();
}
}
}