示例#1
0
        private static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Binary Heap *****");
            var heap = new BinaryHeap();

            heap.Add(20);
            heap.Add(11);
            heap.Add(17);
            heap.Add(7);
            heap.Add(4);
            heap.Add(13);
            heap.Add(15);
            heap.Add(14);
        }
        private static void Main(string[] args)
        {
            var heap = new BinaryHeap <int>(10);

            heap.Insert(4);
            heap.Insert(1);
            heap.Insert(26);
            heap.Insert(19);
            heap.Insert(3);
            Console.WriteLine(heap.GetPreOrder());

            heap.DeleteMin();
            Console.WriteLine(heap.GetPreOrder());
        }
示例#3
0
        public static void Main()
        {
            Console.WriteLine("Created an empty heap.");

            var heap = new BinaryHeap <int>();

            heap.Insert(12);
            heap.Insert(7);
            heap.Insert(9);
            heap.Insert(4);
            heap.Insert(1);

            var arr = new[] { 1, 8, 4, 12, 34, 2, 5 };

            Heap <int> .Sort(arr);

            Console.WriteLine(string.Join(" ", arr));
        }
示例#4
0
        public static void Main()
        {
            Console.WriteLine("Created an empty heap.");
            var heap = new BinaryHeap <int>();

            heap.Insert(5);
            heap.Insert(8);
            heap.Insert(1);
            heap.Insert(3);
            heap.Insert(12);
            heap.Insert(-4);

            Console.WriteLine("Heap elements (max to min):");
            while (heap.Count > 0)
            {
                var max = heap.Pull();
                Console.WriteLine(max);
            }
        }
示例#5
0
        static void Main(string[] args)
        {
            var array = new[] { 1, 124, 23, 245, 2, 4, 6, 5670, 7, 3 };

            var heap = new BinaryHeap <int>(array.Length);

            foreach (var item in array)
            {
                heap.Insert(item);
            }

            do
            {
                var max = heap.DeleteMax();
                Console.WriteLine(max);
            }while (heap.Count() > 0);

            Console.ReadLine();
        }
示例#6
0
        static void Main(string[] args)
        {
            List <int> test1 = new List <int>();

            Random ran = new Random();

            for (int i = 0; i < 50; i++)
            {
                test1.Add(ran.Next(1, 50));
            }

            BinaryHeap <int> bh = new BinaryHeap <int>(test1, CompareInts);

            for (int i = 0; i < 50; i++)
            {
                Console.WriteLine(bh.ExtractTop());
            }

            Console.ReadLine();
        }
示例#7
0
        static void Main(string[] args)
        {
            List<int> test1 = new List<int>();

            Random ran = new Random();

            for (int i = 0; i < 50; i++)
            {
                test1.Add(ran.Next(1,50));
            }

            BinaryHeap<int> bh = new BinaryHeap<int>(test1, CompareInts);

            for (int i = 0; i < 50; i++)
            {
                Console.WriteLine(bh.ExtractTop());
            }

            Console.ReadLine();
        }
示例#8
0
        static void Main(string[] args)
        {
            var heap = new BinaryHeap <Integer>(arrayDel.Length);

            foreach (var el in arrayDel)
            {
                heap.Insert(el);
            }
            heap.DeleteMax();
            heap.DeleteMax();
            heap.DeleteMax();
            foreach (var el in heap.PQ)
            {
                if (el != null)
                {
                    Console.Write(el.data.ToString() + " ");
                }
            }
            var heap1 = new BinaryHeap <Integer>(array.Length + 3);

            foreach (var el in array)
            {
                heap1.Insert(el);
            }
            heap1.Insert(new Integer(55));
            heap1.Insert(new Integer(52));
            heap1.Insert(new Integer(57));

            foreach (var el in heap1.PQ)
            {
                if (el != null)
                {
                    Console.Write(el.data.ToString() + " ");
                }
            }
        }
示例#9
0
 void IDisposable.Dispose()
 {
     this.owner = null;
     this.items = null;
 }
示例#10
0
        private static bool CompareTo(this BinaryHeap <Integer> a, IComparable el1, IComparable el2)
        {
            var result = el1.CompareTo(el2);

            return(result < 0);
        }
示例#11
0
        static void Main(string[] args)
        {
            int[] arr = { 2, 1, 5, 9, 0, 6, 8, 7, 3 };

            BinaryHeap heap = new BinaryHeap(arr);
            Console.Write("上浮方式(与父节点比较):");
            heap.BuildWithSwim();
            heap.Heap.ToList().ForEach(x =>
            {
                Console.Write(x + ",");
            });
            Console.Write("\r\n下沉方式(父节点与两个子节点中的大者比较):");
            heap = new BinaryHeap(arr);
            heap.BuildWithSink();
            heap.Heap.ToList().ForEach(x =>
            {
                Console.Write(x + ",");
            });
            /*
                最终的二叉(大顶)堆大约是这样的:
                          9
                      7       8
                    5   0   2   6
                  1  3
            */
            Console.Read();
        }
示例#12
0
 public void Setup()
 {
     queue = new BinaryHeap <int, string>(queueSize, min, max);
 }