二叉堆(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();
        }
    }
}

 

posted @ 2015-12-17 15:38  拉拉叟  阅读(399)  评论(0编辑  收藏  举报