Skip to content

Commit

Permalink
Refactored the heaps.
Browse files Browse the repository at this point in the history
  • Loading branch information
aalhour committed Jul 29, 2015
1 parent 32ab5f8 commit 08ed1bf
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
25 changes: 17 additions & 8 deletions DataStructures/Heaps/BinaryMaxHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ public BinaryMaxHeap(int capacity, Comparer<T> comparer)
/// <summary>
/// Private Method. Builds a max heap from the inner array-list _collection.
/// </summary>
private void BuildMaxHeap()
private void _buildMaxHeap()
{
int lastIndex = _collection.Count - 1;
int lastNodeWithChildren = (lastIndex / 2);

for (int node = lastNodeWithChildren; node >= 0; node--)
{
MaxHeapify(node, lastIndex);
_maxHeapify(node, lastIndex);
}
}

/// <summary>
/// Private Method. Used in Building a Max Heap.
/// </summary>
private void MaxHeapify(int nodeIndex, int lastIndex)
private void _maxHeapify(int nodeIndex, int lastIndex)
{
// assume that the subtrees left(node) and right(node) are max-heaps
int left = (nodeIndex * 2) + 1;
Expand All @@ -66,10 +66,11 @@ private void MaxHeapify(int nodeIndex, int lastIndex)
if (largest != nodeIndex)
{
_collection.Swap(nodeIndex, largest);
MaxHeapify(largest, lastIndex);
_maxHeapify(largest, lastIndex);
}
}


/// <summary>
/// Returns the number of elements in heap
/// </summary>
Expand Down Expand Up @@ -112,7 +113,7 @@ public T this[int index]
if (_heapComparer.Compare(_collection[index], _collection[0]) >= 0) // greater than or equal to max
{
_collection.Swap(0, index);
BuildMaxHeap();
_buildMaxHeap();
}
}
}
Expand All @@ -134,7 +135,7 @@ public void Initialize(IList<T> newCollection)
}

// Build the heap
BuildMaxHeap();
_buildMaxHeap();
}
}

Expand All @@ -150,7 +151,7 @@ public void Add(T heapKey)
else
{
_collection.Add(heapKey);
BuildMaxHeap();
_buildMaxHeap();
}
}

Expand Down Expand Up @@ -184,7 +185,7 @@ public void RemoveMax()
_collection.RemoveAt(last);
last--;

MaxHeapify(0, last);
_maxHeapify(0, last);
}

/// <summary>
Expand All @@ -210,6 +211,14 @@ public void Clear()
_collection.Clear();
}

/// <summary>
/// Rebuilds the heap.
/// </summary>
public void RebuildHeap()
{
_buildMaxHeap();
}

/// <summary>
/// Returns an array version of this heap.
/// </summary>
Expand Down
26 changes: 18 additions & 8 deletions DataStructures/Heaps/BinaryMinHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ public BinaryMinHeap(int capacity, Comparer<T> comparer)
_heapComparer = comparer ?? Comparer<T>.Default;
}


/// <summary>
/// Builds a min heap from the inner array-list _collection.
/// </summary>
private void BuildMinHeap()
private void _buildMinHeap()
{
int lastIndex = _collection.Count - 1;
int lastNodeWithChildren = (lastIndex / 2);

for (int node = lastNodeWithChildren; node >= 0; node--)
{
MinHeapify(node, lastIndex);
_minHeapify(node, lastIndex);
}
}

Expand All @@ -50,7 +51,7 @@ private void BuildMinHeap()
/// <typeparam name="T">Type of Heap elements</typeparam>
/// <param name="nodeIndex">The node index to heapify at.</param>
/// <param name="lastIndex">The last index of collection to stop at.</param>
private void MinHeapify(int nodeIndex, int lastIndex)
private void _minHeapify(int nodeIndex, int lastIndex)
{
// assume that the subtrees left(node) and right(node) are max-heaps
int left = (nodeIndex * 2) + 1;
Expand All @@ -69,10 +70,11 @@ private void MinHeapify(int nodeIndex, int lastIndex)
if (smallest != nodeIndex)
{
_collection.Swap(nodeIndex, smallest);
MinHeapify(smallest, lastIndex);
_minHeapify(smallest, lastIndex);
}
}


/// <summary>
/// Returns the number of elements in heap
/// </summary>
Expand Down Expand Up @@ -116,7 +118,7 @@ public T this[int index]
if (_heapComparer.Compare(_collection[index], _collection[0]) <= 0) // less than or equal to min
{
_collection.Swap(0, index);
BuildMinHeap();
_buildMinHeap();
}
}
}
Expand All @@ -139,7 +141,7 @@ public void Initialize(IList<T> newCollection)
}

// Build the heap
BuildMinHeap();
_buildMinHeap();
}
}

Expand All @@ -156,7 +158,7 @@ public void Add(T heapKey)
else
{
_collection.Add(heapKey);
BuildMinHeap();
_buildMinHeap();
}
}

Expand Down Expand Up @@ -191,7 +193,7 @@ public void RemoveMin()
_collection.RemoveAt(last);
last--;

MinHeapify(0, last);
_minHeapify(0, last);
}

/// <summary>
Expand All @@ -218,6 +220,14 @@ public void Clear()
_collection.Clear();
}

/// <summary>
/// Rebuilds the heap.
/// </summary>
public void RebuildHeap()
{
_buildMinHeap();
}

/// <summary>
/// Returns an array version of this heap.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions DataStructures/Heaps/BinomialMinHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ 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>
Expand Down
5 changes: 5 additions & 0 deletions DataStructures/Heaps/IMaxHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public interface IMaxHeap<T> where T : System.IComparable<T>
/// </summary>
void Clear();

/// <summary>
/// Rebuilds the heap.
/// </summary>
void RebuildHeap();

/// <summary>
/// Returns an array version of this heap.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions DataStructures/Heaps/IMinHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public interface IMinHeap<T> where T : System.IComparable<T>
/// </summary>
void Clear();

/// <summary>
/// Rebuilds the heap.
/// </summary>
void RebuildHeap();

/// <summary>
/// Returns an array version of this heap.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion DataStructures/Heaps/MinPriorityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void Enqueue(TKey value, TPriority priority)
/// <summary>
/// Dequeue this instance.
/// </summary>
public TKey Dequeue()
public TKey DequeueMin()
{
if (_heap.IsEmpty)
throw new ArgumentOutOfRangeException("Queue is empty.");
Expand Down Expand Up @@ -222,6 +222,7 @@ public void UpdatePriority(TKey key, TPriority newPriority)
throw new KeyNotFoundException();

_heap[index].Priority = newPriority;
//_heap.RebuildHeap();
}

/// <summary>
Expand Down

0 comments on commit 08ed1bf

Please sign in to comment.