【数据结构】堆

文章目录


前言

C#居然没有堆的数据结构,那就只能自己实现了。

实现参考:《挑战程序设计竞赛》


1 代码

mCompareFunc判断a小于b时为小跟堆,否则为大根堆。

public class Heap<T>
{
     List<T> mHeapList = new List<T>();
     Func<T, T, bool> mCompareFunc;

     public int Size => mHeapList.Count;

     public Heap(Func<T, T, bool> compareFunc)
     {
         mCompareFunc = compareFunc;
     }

     public void Push(T value)
     {
         int index = mHeapList.Count;
         mHeapList.Add(value);
         while (index > 0)
         {
             int parent = (index - 1) / 2;
             if (mCompareFunc(mHeapList[parent], value)) break;

             mHeapList[index] = mHeapList[parent];
             index = parent;
         }
         mHeapList[index] = value;
     }

     public T Pop()
     {
         if (Empty())
         {
             throw new ArgumentOutOfRangeException("堆为空!");
         }

         T ret = mHeapList[0];
         int size = mHeapList.Count - 1;
         T x = mHeapList[size];
         int index = 0;
         while(index * 2 + 1 < size)
         {
             int leftChildIndex = index * 2 + 1;
             int rightChildIndex = index * 2 + 2;
             if (leftChildIndex < size && mCompareFunc(mHeapList[rightChildIndex], mHeapList[leftChildIndex])) 
                 leftChildIndex = rightChildIndex;
             
             // 已经没有大小颠倒则退出
             if (!mCompareFunc(mHeapList[leftChildIndex], x)) break;

             mHeapList[index] = mHeapList[leftChildIndex];
             index = leftChildIndex;
         }
         mHeapList[index] = x;
         mHeapList.RemoveAt(mHeapList.Count - 1);
         return ret;
     }

     public T Top()
     {
         return mHeapList[0];
     }

     public bool Empty()
     {
         return mHeapList.Count == 0;
     }
 }

总结

如果要使用优先队列,那么其中一种实现就是把堆封装一下就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值