最小堆的接口 IMinHeap
/// <summary>
/// 最小堆的接口
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IMinHeap<T> : IHeapBase<T> where T : IComparable<T>
{
/// <summary>
/// 删除最小项
/// </summary>
void RemoveMin();
/// <summary>
/// 提取最小项
/// </summary>
/// <returns></returns>
T ExtractMin();
/// <summary>
/// 转换成最大堆
/// </summary>
/// <returns></returns>
IMaxHeap<T> ToMaxHeap();
}
具体实现BinaryMinHeap
最大堆的接口 IMaxHeap
/// <summary>
/// 最大堆的接口
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IMaxHeap<T> IHeapBase<T> where T : IComparable<T>
{
/// <summary>
/// 删除最大项
/// </summary>
void RemoveMax();
/// <summary>
/// 提取最大项
/// </summary>
/// <returns></returns>
T ExtractMax();
/// <summary>
/// 转换成最小堆
/// </summary>
/// <returns></returns>
IMinHeap<T> ToMinHeap();
}
具体实现BinaryMaxHeap
堆的基本接口 IHeapBase
/// <summary>
/// 堆的基本接口
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IHeapBase<T>
{
/// <summary>
/// 堆的项数
/// </summary>
int Count { get; }
/// <summary>
/// 堆是否空,ture空,false不空
/// </summary>
bool IsEmpty { get; }
/// <summary>
/// 堆的初始化
/// </summary>
/// <param name="collection"></param>
void Initialize(IList<T> collection);
/// <summary>
/// 堆中添加项
/// </summary>
/// <param name="heapKey"></param>
void Add(T heapKey);
/// <summary>
/// 查看堆的首项
/// </summary>
/// <returns></returns>
T Peek();
/// <summary>
/// 清空堆
/// </summary>
void Clear();
/// <summary>
/// 重建堆
/// </summary>
void RebuildHeap();
/// <summary>
/// 转换成数组形式
/// </summary>
/// <returns></returns>
T[] ToArray();
/// <summary>
/// 转换成列表形式
/// </summary>
/// <returns></returns>
List<T> ToList();
}