二叉堆(C#)

参考:http://www.cnblogs.com/skywang12345/p/3610390.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Collections;

namespace ConsoleApplication2
{
    public class Program
    {
        public static void Main()
        {
            ShowMaxHeap();
            ShowMinHeap();
            Console.Read();
        }

        private static void ShowMaxHeap()
        {
            int[] a = { 10, 40, 30, 60, 90, 70, 20, 50, 80,85 }; 

            BinaryHeap<int> maxHeap = new BinaryHeap<int>((int1, int2) => int1.CompareTo(int2) > 0);//最大推

            Console.Write("测试数据:");
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write(string.Format("{0} ", a[i]));
                maxHeap.Insert(a[i]);
            }
            Console.WriteLine();

            Console.WriteLine("最大堆:" + maxHeap);


            Console.Write("删除元素90:");
            maxHeap.Remove(90);
            Console.WriteLine(maxHeap);
        }

        private static void ShowMinHeap()
        {
            int[] a = { 80, 40, 30, 60, 90, 70, 10, 50, 20 ,15};

            BinaryHeap<int> minHeap = new BinaryHeap<int>((int1, int2) => int1.CompareTo(int2) < 0);//最小推

            Console.Write("测试数据:");
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write(string.Format("{0} ", a[i]));
                minHeap.Insert(a[i]);
            }
            Console.WriteLine();

            Console.WriteLine("最小堆:" + minHeap);


            Console.Write("删除元素10:");
            minHeap.Remove(10);
            Console.WriteLine(minHeap);
        }
    }


    public class BinaryHeap<T> where T : IComparable
    { 
        private List<T> mHeap;
        private Func<T, T, bool> comparableFun;//这里我通过用表达式来决定大堆还是小堆
        

        public BinaryHeap(Func<T, T, bool> _comparableFun)
        {
            mHeap = new List<T>();
            comparableFun = _comparableFun;
        }

        public void Insert(T t)
        {
            int count = mHeap.Count;
            mHeap.Add(t);

            int middle = (count - 1) / 2;
            T temp = mHeap[count];

            while (count > 0 && comparableFun(temp, mHeap[middle]))//temp.CompareTo(mHeap[middle]) < 0
            {
                mHeap[count] = mHeap[middle];
                count = middle;
                middle = (middle - 1) / 2;
            }
            mHeap[count] = temp;
        }

        public T GetTopAndRemove()
        {
            var topT = mHeap[0];
            Remove(topT);
            return topT;
        }

        public void Remove(T t)
        {
            var index = mHeap.IndexOf(t);

            int count = mHeap.Count;
            var temp = mHeap[count - 1];
            mHeap.RemoveAt(count - 1);
            count = count - 1;

            mHeap[index] = temp;

            int middle = index * 2 + 1;
            while (middle < count && !comparableFun(temp,mHeap[middle]))//注意哦  这里多了个取反
            {
                mHeap[index] = mHeap[middle];
                index = middle;
                middle = middle * 2 + 2;
            }
            mHeap[index] = temp;
        }

        public override String ToString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mHeap.Count; i++)
            {
                sb.Append(mHeap[i] + " ");
            }
            return sb.ToString();
        }
    }
}

 

转载于:https://www.cnblogs.com/bbvi/p/5054355.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值