Skip to content

Commit

Permalink
Refactored the heaps: interfaces, binary heaps classes and add an emp…
Browse files Browse the repository at this point in the history
…ty bionomial min heap class.
  • Loading branch information
aalhour committed Jun 25, 2015
1 parent 6b0958c commit 442b3d6
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 141 deletions.
6 changes: 3 additions & 3 deletions DataStructures/DataStructures.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<Compile Include="Dictionaries\OpenScatterHashTable.cs" />
<Compile Include="Hashing\PrimeHashingFamily.cs" />
<Compile Include="Hashing\UniversalHashingFamily.cs" />
<Compile Include="Heaps\BionomialHeap.cs" />
<Compile Include="Heaps\BionomialMinHeap.cs" />
<Compile Include="Trees\AugmentedBinarySearchTree.cs" />
<Compile Include="Trees\AVLTree.cs" />
<Compile Include="Trees\BinarySearchTree.cs" />
Expand All @@ -57,8 +57,8 @@
<Compile Include="Lists\DLinkedList.cs" />
<Compile Include="Lists\SLinkedList.cs" />
<Compile Include="Lists\Queue.cs" />
<Compile Include="Heaps\MinBinaryHeap.cs" />
<Compile Include="Heaps\MaxBinaryHeap.cs" />
<Compile Include="Heaps\BinaryMinHeap.cs" />
<Compile Include="Heaps\BinaryMaxHeap.cs" />
<Compile Include="Common\Helpers.cs" />
<Compile Include="Heaps\KeyedPriorityQueue.cs" />
<Compile Include="Heaps\PriorityQueue.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace DataStructures.Heaps
/// <summary>
/// Maximum Heap Data Structure.
/// </summary>
public class MaxBinaryHeap<T> : IMaxHeap<T> where T : IComparable<T>
public class BinaryMaxHeap<T> : IMaxHeap<T> where T : IComparable<T>
{
/// <summary>
/// Instance Variables.
Expand All @@ -22,9 +22,9 @@ public class MaxBinaryHeap<T> : IMaxHeap<T> where T : IComparable<T>
/// <summary>
/// CONSTRUCTORS
/// </summary>
public MaxBinaryHeap() : this(0, null) { }
public MaxBinaryHeap(Comparer<T> comparer) : this(0, comparer) { }
public MaxBinaryHeap(int capacity, Comparer<T> comparer)
public BinaryMaxHeap() : this(0, null) { }
public BinaryMaxHeap(Comparer<T> comparer) : this(0, comparer) { }
public BinaryMaxHeap(int capacity, Comparer<T> comparer)
{
_collection = new ArrayList<T>(capacity);
_heapComparer = comparer ?? Comparer<T>.Default;
Expand Down Expand Up @@ -230,14 +230,14 @@ public List<T> ToList()
/// Union two heaps together, returns a new min-heap of both heaps' elements,
/// ... and then destroys the original ones.
/// </summary>
public MaxBinaryHeap<T> Union(ref MaxBinaryHeap<T> firstMaxHeap, ref MaxBinaryHeap<T> secondMaxHeap)
public BinaryMaxHeap<T> Union(ref BinaryMaxHeap<T> firstMaxHeap, ref BinaryMaxHeap<T> secondMaxHeap)
{
if (firstMaxHeap == null || secondMaxHeap == null)
throw new ArgumentNullException ("Null heaps are not allowed.");

// Create a new heap with reserved size.
int size = firstMaxHeap.Count() + secondMaxHeap.Count();
var newHeap = new MaxBinaryHeap<T> (size, Comparer<T>.Default);
var newHeap = new BinaryMaxHeap<T> (size, Comparer<T>.Default);

// Insert into the new heap.
while (firstMaxHeap.IsEmpty () == false)
Expand All @@ -255,65 +255,13 @@ public MaxBinaryHeap<T> Union(ref MaxBinaryHeap<T> firstMaxHeap, ref MaxBinaryHe
/// <summary>
/// Returns a new min heap that contains all elements of this heap.
/// </summary>
public MinBinaryHeap<T> ToMinHeap()
public IMinHeap<T> ToBinaryMinHeap()
{
MinBinaryHeap<T> newMinHeap = new MinBinaryHeap<T>(this.Count(), this._heapComparer);
newMinHeap.Heapify(this._collection.ToArray());
return newMinHeap;
}


/***
* COMMENTED FUNCTIONS.
* Not necessary for the purposes of MaxHeaps.
* Might be useful in some cases.
*/

///// <summary>
///// Remove a key from the heap.
///// </summary>
///// <param name="heapKey">Heap key.</param>
//public void Remove(T heapKey)
//{
// if (!IsEmpty)
// {
// int last = _collection.Count - 1;
// int index = _collection.IndexOf(heapKey);
// _collection.Swap (index, last);
// _collection.RemoveAt (last);
// last--;
// MaxHeapify(0, last);
// }
//}
///// <summary>
///// Removes the key at index.
///// </summary>
///// <param name="index">Index.</param>
//public void RemoveAt(int index)
//{
// if (!IsEmpty)
// {
// int last = _collection.Count - 1;
// _collection.Swap (index, last);
// _collection.RemoveAt (last);
// last--;
// MaxHeapify(0, last);
// }
//}
///// <summary>
///// Removes all keys that match the predicate.
///// </summary>
///// <param name="searchMatch">Search match.</param>
//public void RemoveAll(Predicate<T> searchMatch)
//{
// for (int i = 0; i < _collection.Count; ++i)
// {
// if (searchMatch (_collection [i]))
// {
// RemoveAt (i);
// }
// }
//}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,71 +263,13 @@ public MinBinaryHeap<T> Union(ref MinBinaryHeap<T> firstMinHeap, ref MinBinaryHe
/// <summary>
/// Returns a new max heap that contains all elements of this heap.
/// </summary>
public MaxBinaryHeap<T> ToMaxHeap()
public IMaxHeap<T> ToBinaryMaxHeap()
{
MaxBinaryHeap<T> newMaxHeap = new MaxBinaryHeap<T>(this.Count(), this._heapComparer);
BinaryMaxHeap<T> newMaxHeap = new BinaryMaxHeap<T>(this.Count(), this._heapComparer);
newMaxHeap.Heapify(this._collection.ToArray());
return newMaxHeap;
}


/***
* COMMENTED FUNCTIONS.
* Not necessary for the purposes of MinHeaps.
* Might be useful in some cases.
*/


///// <summary>
///// Remove a key from the heap.
///// </summary>
///// <param name="heapKey">Heap key.</param>
//public void Remove(T heapKey)
//{
// if (!IsEmpty)
// {
// int last = _collection.Count - 1;
// int index = _collection.IndexOf(heapKey);
// _collection.Swap (index, last);
// _collection.RemoveAt (last);
// last--;
// MinHeapify<T>(0, last);
// }
//}


///// <summary>
///// Removes the key at index.
///// </summary>
///// <param name="index">Index.</param>
//public void RemoveAt(int index)
//{
// if (!IsEmpty)
// {
// int last = _collection.Count - 1;
// _collection.Swap (index, last);
// _collection.RemoveAt (last);
// last--;
// MinHeapify<T>(0, last);
// }
//}


///// <summary>
///// Removes all keys that match the predicate.
///// </summary>
///// <param name="searchMatch">Search match.</param>
//public void RemoveAll(Predicate<T> searchMatch)
//{
// for (int i = 0; i < _collection.Count; ++i)
// {
// if (searchMatch (_collection [i]))
// {
// RemoveAt (i);
// }
// }
//}

}

}
12 changes: 0 additions & 12 deletions DataStructures/Heaps/BionomialHeap.cs

This file was deleted.

71 changes: 71 additions & 0 deletions DataStructures/Heaps/BionomialMinHeap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructures.Heaps
{
public class BionomialMinHeap<T> : IMinHeap<T> where T : IComparable<T>
{
public BionomialMinHeap()
{

}

public int Count()
{
throw new NotImplementedException();
}

public bool IsEmpty()
{
throw new NotImplementedException();
}

public void Heapify(IList<T> newCollection)
{
throw new NotImplementedException();
}

public void Insert(T heapKey)
{
throw new NotImplementedException();
}

public T Peek()
{
throw new NotImplementedException();
}

public void RemoveMin()
{
throw new NotImplementedException();
}

public T ExtractMin()
{
throw new NotImplementedException();
}

public void Clear()
{
throw new NotImplementedException();
}

public T[] ToArray()
{
throw new NotImplementedException();
}

public List<T> ToList()
{
throw new NotImplementedException();
}

public IMaxHeap<T> ToBinaryMaxHeap()
{
throw new NotImplementedException();
}
}
}
2 changes: 1 addition & 1 deletion DataStructures/Heaps/IMaxHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public interface IMaxHeap<T> where T : System.IComparable<T>
/// Returns a new max heap that contains all elements of this heap.
/// </summary>
/// <returns>The max heap.</returns>
MinBinaryHeap<T> ToMinHeap();
IMinHeap<T> ToBinaryMinHeap();
}
}
2 changes: 1 addition & 1 deletion DataStructures/Heaps/IMinHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public interface IMinHeap<T> where T : System.IComparable<T>
/// Returns a new min heap that contains all elements of this heap.
/// </summary>
/// <returns>The min heap.</returns>
MaxBinaryHeap<T> ToMaxHeap();
IMaxHeap<T> ToBinaryMaxHeap();
}
}
4 changes: 2 additions & 2 deletions DataStructures/Heaps/KeyedPriorityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PriorityQueue<K, V, P> where P : IComparable<P>
/// <summary>
/// Instance variables
/// </summary>
private MaxBinaryHeap<PriorityQueueNode<K, V, P>> _heap { get; set; }
private BinaryMaxHeap<PriorityQueueNode<K, V, P>> _heap { get; set; }
private Comparer<PriorityQueueNode<K, V, P>> _priorityComparer { get; set; }
private Dictionary<K, int> _keysMap { get; set; }

Expand Down Expand Up @@ -49,7 +49,7 @@ public PriorityQueue(int capacity, Comparer<PriorityQueueNode<K, V, P>> priority
_priorityComparer = priorityComparer;
}

_heap = new MaxBinaryHeap<PriorityQueueNode<K, V, P>>(capacity, this._priorityComparer);
_heap = new BinaryMaxHeap<PriorityQueueNode<K, V, P>>(capacity, this._priorityComparer);
_keysMap = new Dictionary<K, int> ();
}
else
Expand Down
4 changes: 2 additions & 2 deletions DataStructures/Heaps/PriorityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class PriorityQueue<V, P> where P : IComparable<P>
/// <summary>
/// Instance variables
/// </summary>
private MaxBinaryHeap<PriorityQueueNode<V, P>> _heap { get; set; }
private BinaryMaxHeap<PriorityQueueNode<V, P>> _heap { get; set; }
private Comparer<PriorityQueueNode<V, P>> _priorityComparer { get; set; }


Expand Down Expand Up @@ -46,7 +46,7 @@ public PriorityQueue(int capacity, Comparer<PriorityQueueNode<V, P>> priorityCom
_priorityComparer = priorityComparer;
}

_heap = new MaxBinaryHeap<PriorityQueueNode<V, P>>(capacity, this._priorityComparer);
_heap = new BinaryMaxHeap<PriorityQueueNode<V, P>>(capacity, this._priorityComparer);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion MainProgram/DataStructuresTests/HeapsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void DoTest ()
Debug.Assert (list.Count == minHeap.Count(), "Wrong size.");

array.HeapSortDescending();
var maxHeap = minHeap.ToMaxHeap ();
var maxHeap = minHeap.ToBinaryMaxHeap ();
Debug.Assert (maxHeap.Peek() == array[0], "Wrong maximum.");
}
}
Expand Down

0 comments on commit 442b3d6

Please sign in to comment.