Skip to content

Commit 08ed1bf

Browse files
committed
Refactored the heaps.
1 parent 32ab5f8 commit 08ed1bf

File tree

6 files changed

+55
-17
lines changed

6 files changed

+55
-17
lines changed

Diff for: DataStructures/Heaps/BinaryMaxHeap.cs

+17-8
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ public BinaryMaxHeap(int capacity, Comparer<T> comparer)
3333
/// <summary>
3434
/// Private Method. Builds a max heap from the inner array-list _collection.
3535
/// </summary>
36-
private void BuildMaxHeap()
36+
private void _buildMaxHeap()
3737
{
3838
int lastIndex = _collection.Count - 1;
3939
int lastNodeWithChildren = (lastIndex / 2);
4040

4141
for (int node = lastNodeWithChildren; node >= 0; node--)
4242
{
43-
MaxHeapify(node, lastIndex);
43+
_maxHeapify(node, lastIndex);
4444
}
4545
}
4646

4747
/// <summary>
4848
/// Private Method. Used in Building a Max Heap.
4949
/// </summary>
50-
private void MaxHeapify(int nodeIndex, int lastIndex)
50+
private void _maxHeapify(int nodeIndex, int lastIndex)
5151
{
5252
// assume that the subtrees left(node) and right(node) are max-heaps
5353
int left = (nodeIndex * 2) + 1;
@@ -66,10 +66,11 @@ private void MaxHeapify(int nodeIndex, int lastIndex)
6666
if (largest != nodeIndex)
6767
{
6868
_collection.Swap(nodeIndex, largest);
69-
MaxHeapify(largest, lastIndex);
69+
_maxHeapify(largest, lastIndex);
7070
}
7171
}
7272

73+
7374
/// <summary>
7475
/// Returns the number of elements in heap
7576
/// </summary>
@@ -112,7 +113,7 @@ public T this[int index]
112113
if (_heapComparer.Compare(_collection[index], _collection[0]) >= 0) // greater than or equal to max
113114
{
114115
_collection.Swap(0, index);
115-
BuildMaxHeap();
116+
_buildMaxHeap();
116117
}
117118
}
118119
}
@@ -134,7 +135,7 @@ public void Initialize(IList<T> newCollection)
134135
}
135136

136137
// Build the heap
137-
BuildMaxHeap();
138+
_buildMaxHeap();
138139
}
139140
}
140141

@@ -150,7 +151,7 @@ public void Add(T heapKey)
150151
else
151152
{
152153
_collection.Add(heapKey);
153-
BuildMaxHeap();
154+
_buildMaxHeap();
154155
}
155156
}
156157

@@ -184,7 +185,7 @@ public void RemoveMax()
184185
_collection.RemoveAt(last);
185186
last--;
186187

187-
MaxHeapify(0, last);
188+
_maxHeapify(0, last);
188189
}
189190

190191
/// <summary>
@@ -210,6 +211,14 @@ public void Clear()
210211
_collection.Clear();
211212
}
212213

214+
/// <summary>
215+
/// Rebuilds the heap.
216+
/// </summary>
217+
public void RebuildHeap()
218+
{
219+
_buildMaxHeap();
220+
}
221+
213222
/// <summary>
214223
/// Returns an array version of this heap.
215224
/// </summary>

Diff for: DataStructures/Heaps/BinaryMinHeap.cs

+18-8
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ public BinaryMinHeap(int capacity, Comparer<T> comparer)
3030
_heapComparer = comparer ?? Comparer<T>.Default;
3131
}
3232

33+
3334
/// <summary>
3435
/// Builds a min heap from the inner array-list _collection.
3536
/// </summary>
36-
private void BuildMinHeap()
37+
private void _buildMinHeap()
3738
{
3839
int lastIndex = _collection.Count - 1;
3940
int lastNodeWithChildren = (lastIndex / 2);
4041

4142
for (int node = lastNodeWithChildren; node >= 0; node--)
4243
{
43-
MinHeapify(node, lastIndex);
44+
_minHeapify(node, lastIndex);
4445
}
4546
}
4647

@@ -50,7 +51,7 @@ private void BuildMinHeap()
5051
/// <typeparam name="T">Type of Heap elements</typeparam>
5152
/// <param name="nodeIndex">The node index to heapify at.</param>
5253
/// <param name="lastIndex">The last index of collection to stop at.</param>
53-
private void MinHeapify(int nodeIndex, int lastIndex)
54+
private void _minHeapify(int nodeIndex, int lastIndex)
5455
{
5556
// assume that the subtrees left(node) and right(node) are max-heaps
5657
int left = (nodeIndex * 2) + 1;
@@ -69,10 +70,11 @@ private void MinHeapify(int nodeIndex, int lastIndex)
6970
if (smallest != nodeIndex)
7071
{
7172
_collection.Swap(nodeIndex, smallest);
72-
MinHeapify(smallest, lastIndex);
73+
_minHeapify(smallest, lastIndex);
7374
}
7475
}
7576

77+
7678
/// <summary>
7779
/// Returns the number of elements in heap
7880
/// </summary>
@@ -116,7 +118,7 @@ public T this[int index]
116118
if (_heapComparer.Compare(_collection[index], _collection[0]) <= 0) // less than or equal to min
117119
{
118120
_collection.Swap(0, index);
119-
BuildMinHeap();
121+
_buildMinHeap();
120122
}
121123
}
122124
}
@@ -139,7 +141,7 @@ public void Initialize(IList<T> newCollection)
139141
}
140142

141143
// Build the heap
142-
BuildMinHeap();
144+
_buildMinHeap();
143145
}
144146
}
145147

@@ -156,7 +158,7 @@ public void Add(T heapKey)
156158
else
157159
{
158160
_collection.Add(heapKey);
159-
BuildMinHeap();
161+
_buildMinHeap();
160162
}
161163
}
162164

@@ -191,7 +193,7 @@ public void RemoveMin()
191193
_collection.RemoveAt(last);
192194
last--;
193195

194-
MinHeapify(0, last);
196+
_minHeapify(0, last);
195197
}
196198

197199
/// <summary>
@@ -218,6 +220,14 @@ public void Clear()
218220
_collection.Clear();
219221
}
220222

223+
/// <summary>
224+
/// Rebuilds the heap.
225+
/// </summary>
226+
public void RebuildHeap()
227+
{
228+
_buildMinHeap();
229+
}
230+
221231
/// <summary>
222232
/// Returns an array version of this heap.
223233
/// </summary>

Diff for: DataStructures/Heaps/BinomialMinHeap.cs

+8
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,14 @@ public T[] ToArray()
339339
throw new NotImplementedException();
340340
}
341341

342+
/// <summary>
343+
/// Rebuilds the heap.
344+
/// </summary>
345+
public void RebuildHeap()
346+
{
347+
throw new NotImplementedException();
348+
}
349+
342350
/// <summary>
343351
/// Returns a list copy of heap.
344352
/// </summary>

Diff for: DataStructures/Heaps/IMaxHeap.cs

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public interface IMaxHeap<T> where T : System.IComparable<T>
4747
/// </summary>
4848
void Clear();
4949

50+
/// <summary>
51+
/// Rebuilds the heap.
52+
/// </summary>
53+
void RebuildHeap();
54+
5055
/// <summary>
5156
/// Returns an array version of this heap.
5257
/// </summary>

Diff for: DataStructures/Heaps/IMinHeap.cs

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public interface IMinHeap<T> where T : System.IComparable<T>
4747
/// </summary>
4848
void Clear();
4949

50+
/// <summary>
51+
/// Rebuilds the heap.
52+
/// </summary>
53+
void RebuildHeap();
54+
5055
/// <summary>
5156
/// Returns an array version of this heap.
5257
/// </summary>

Diff for: DataStructures/Heaps/MinPriorityQueue.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void Enqueue(TKey value, TPriority priority)
191191
/// <summary>
192192
/// Dequeue this instance.
193193
/// </summary>
194-
public TKey Dequeue()
194+
public TKey DequeueMin()
195195
{
196196
if (_heap.IsEmpty)
197197
throw new ArgumentOutOfRangeException("Queue is empty.");
@@ -222,6 +222,7 @@ public void UpdatePriority(TKey key, TPriority newPriority)
222222
throw new KeyNotFoundException();
223223

224224
_heap[index].Priority = newPriority;
225+
//_heap.RebuildHeap();
225226
}
226227

227228
/// <summary>

0 commit comments

Comments
 (0)