예제 #1
1
        private static void MaxTest()
        {
            Console.WriteLine("This section is to test the MAX HEAP class.\n");

            string sort_error = "The current value is not less than the next value on the array.";
            string pop_error = "The current value is not grater than the next value on the array";
            string invariant_error = "The invariants have been broken.";

            MaxHeap<int> myHeap = new MaxHeap<int>();

            int[] numberList = new int[] { 2, 5, 9, 2, 8, 1, 4, 7, 3, 6 };

            Console.WriteLine("Adding the following numbers to the heap: [{0}]\n",
                string.Join(", ", numberList));

            foreach (int number in numberList)
                myHeap.Add(number);

            Console.WriteLine("New Heap: [{0}]\n", string.Join(", ", myHeap));

            Console.WriteLine("Performing Heap Sort...\n");

            myHeap.Sort();
            TestMaxSort(myHeap, sort_error);

            Console.WriteLine("New Heap: [{0}]\n", string.Join(", ", myHeap));

            Console.WriteLine("Rebuilding Heap...\n");

            myHeap.BuildHeap();

            Console.WriteLine("New Heap: [{0}]\n", string.Join(", ", myHeap));

            Console.WriteLine("Poping the top value until heap is empty...\n");

            TestMaxPop(myHeap, pop_error);

            int elements = 20000;
            myHeap = new MaxHeap<int>(elements);
            int[] random_list = RandomIntArray(elements);

            Console.WriteLine("Adding {0} values to a new heap and verifying the invariants...\n", elements);

            Console.WriteLine("This part will take a while...\n");

            foreach (int number in random_list)
            {
                myHeap.Add(number);
                TestMaxInvariant(myHeap, invariant_error);
            }

            Console.WriteLine("Heap too big to print on console, current elements in heap: {0}\n", myHeap.Count);

            Console.WriteLine("Going to pop half of the values out of the heap and verifying the invariants...\n");

            Console.WriteLine("Again... This part will take a while...\n");

            for (int i = 0; i < elements / 2; i++)
            {
                myHeap.PopMax();
                TestMaxInvariant(myHeap, invariant_error);
            }

            Console.WriteLine("Heap too big to print on console, current elements in heap: {0}\n", myHeap.Count);

            Console.WriteLine("----------------------------------------------------");
        }
예제 #2
0
        public static void Sort(int[] arr)
        {
            int   n       = arr.Length;
            IHeap maxHeap = new MaxHeap(n);

            maxHeap.BuildHeap(arr);

            for (int i = n - 1; i > 0; i--)
            {
                Util.Swap(arr, 0, i);
                maxHeap.Heapify(arr, 0, i);
            }
        }
예제 #3
0
파일: Program.cs 프로젝트: ty-stylelabs/DSA
        private static void Main()
        {
            var array   = Enumerable.Range(1, 12).ToArray();
            var maxHeap = new MaxHeap <int>(array, 20);

            maxHeap.BuildHeap();
            maxHeap.Print();
            Console.ReadLine();
            Console.Clear();
            maxHeap.RemoveMax();
            maxHeap.Print();
            Console.ReadLine();
            Console.Clear();
            maxHeap.Remove(3);
            maxHeap.Insert(6);
            Console.WriteLine("Size : " + maxHeap.Size);
            maxHeap.Print();
            Console.ReadLine();

            array = Enumerable.Range(1, 5).ToArray();
            var minHeap = new MinHeap <int>(array, 20);

            minHeap.BuildHeap();
            minHeap.Print();
            Console.ReadLine();
            Console.Clear();
            minHeap.RemoveMin();
            minHeap.Print();
            Console.ReadLine();
            Console.Clear();
            minHeap.Remove(3);
            minHeap.Insert(6);
            Console.WriteLine("Size : " + minHeap.Size);
            minHeap.Print();
            Console.ReadLine();
        }
예제 #4
0
        private static void TestMaxInvariant(MaxHeap<int> heap, string error)
        {
            if (heap.Count % 500 == 0)
                Console.WriteLine("Cycle {0}\n", heap.Count);

            heap.Sort();

            for (int index = 0; index < heap.Count - 1; index++)
            {
                Debug.Assert(heap[index] <= heap[index + 1], error);
            }

            heap.BuildHeap();
        }