-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathd-aryHeap.cs
217 lines (172 loc) · 5.44 KB
/
d-aryHeap.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Advanced.Algorithms.DataStructures;
/// <summary>
/// A D-ary minMax heap implementation.
/// </summary>
public class DaryHeap<T> : IEnumerable<T> where T : IComparable
{
private readonly IComparer<T> comparer;
private readonly bool isMaxHeap;
public int Count;
private T[] heapArray;
private readonly int k;
/// <summary>
/// Time complexity: O(n) when initial is provided otherwise O(1).
/// </summary>
/// <param name="k">The number of children per heap node.</param>
/// <param name="initial">The initial items if any.</param>
public DaryHeap(int k, SortDirection sortDirection = SortDirection.Ascending, IEnumerable<T> initial = null)
{
isMaxHeap = sortDirection == SortDirection.Descending;
comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
if (k <= 2) throw new Exception("Number of nodes k must be greater than 2.");
this.k = k;
if (initial != null)
{
var items = initial as T[] ?? initial.ToArray();
var initArray = new T[items.Count()];
var i = 0;
foreach (var item in items)
{
initArray[i] = item;
i++;
}
Count = initArray.Length;
BulkInit(initArray);
}
else
{
heapArray = new T[k];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return heapArray.Take(Count).GetEnumerator();
}
/// <summary>
/// Initialize with given input.
/// Time complexity: O(n).
/// </summary>
private void BulkInit(T[] initial)
{
var i = (initial.Length - 1) / k;
while (i >= 0)
{
BulkInitRecursive(i, initial);
i--;
}
heapArray = initial;
}
/// <summary>
/// Recursively load bulk init values.
/// </summary>
private void BulkInitRecursive(int i, T[] initial)
{
var parent = i;
var minMax = FindMinMaxChildIndex(i, initial);
if (minMax != -1 && comparer.Compare(initial[minMax], initial[parent]) < 0)
{
var temp = initial[minMax];
initial[minMax] = initial[parent];
initial[parent] = temp;
BulkInitRecursive(minMax, initial);
}
}
/// <summary>
/// Time complexity: O(log(n) base K).
/// </summary>
public void Insert(T newItem)
{
if (Count == heapArray.Length) DoubleArray();
heapArray[Count] = newItem;
//percolate up
for (var i = Count; i > 0; i = (i - 1) / k)
if (comparer.Compare(heapArray[i], heapArray[(i - 1) / k]) < 0)
{
var temp = heapArray[(i - 1) / k];
heapArray[(i - 1) / k] = heapArray[i];
heapArray[i] = temp;
}
else
{
break;
}
Count++;
}
/// <summary>
/// Time complexity: O(log(n) base K).
/// </summary>
public T Extract()
{
if (Count == 0) throw new Exception("Empty heap");
var minMax = heapArray[0];
//move last element to top
heapArray[0] = heapArray[Count - 1];
Count--;
var currentParent = 0;
//now percolate down
while (true)
{
var swapped = false;
//init to left-most child
var minMaxChildIndex = FindMinMaxChildIndex(currentParent, heapArray);
if (minMaxChildIndex != -1 &&
comparer.Compare(heapArray[currentParent], heapArray[minMaxChildIndex]) > 0)
{
var tmp = heapArray[minMaxChildIndex];
heapArray[minMaxChildIndex] = heapArray[currentParent];
heapArray[currentParent] = tmp;
swapped = true;
}
if (!swapped) break;
currentParent = minMaxChildIndex;
}
if (heapArray.Length / 2 == Count && heapArray.Length > 2) HalfArray();
return minMax;
}
/// <summary>
/// Returns the max Index of child if any.
/// Otherwise returns -1.
/// </summary>
private int FindMinMaxChildIndex(int currentParent, T[] heap)
{
var currentMinMax = currentParent * k + 1;
if (currentMinMax >= Count)
return -1;
for (var i = 2; i <= k; i++)
{
if (currentParent * k + i >= Count)
break;
var nextSibling = heap[currentParent * k + i];
if (comparer.Compare(heap[currentMinMax], nextSibling) > 0) currentMinMax = currentParent * k + i;
}
return currentMinMax;
}
/// <summary>
/// Time complexity: O(1).
/// </summary>
public T Peek()
{
if (Count == 0) throw new Exception("Empty heap");
return heapArray[0];
}
private void HalfArray()
{
var smallerArray = new T[heapArray.Length / 2];
for (var i = 0; i < Count; i++) smallerArray[i] = heapArray[i];
heapArray = smallerArray;
}
private void DoubleArray()
{
var biggerArray = new T[heapArray.Length * 2];
for (var i = 0; i < Count; i++) biggerArray[i] = heapArray[i];
heapArray = biggerArray;
}
}