public void InsertTests() { int[] values = { 2, 19, 1, 11, 10, 3, 18 }; var minHeap = new MinHeap <int>(); minHeap.BuildHeap(values); var heap = minHeap.Heap; Assert.AreEqual(7, minHeap.Size); Assert.AreEqual(1, minHeap.GetMin()); var expectedHeap = new int[] { 0, 1, 10, 2, 19, 11, 3, 18 }; for (int i = 0; i < heap.Length; i++) { Assert.AreEqual(expectedHeap[i], heap[i]); } }
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(); }
private static void MinTest() { Console.WriteLine("\nThis section is to test the MIN HEAP class.\n"); string sort_error = "The current value is not grater than the next value on the array."; string pop_error = "The current value is not less than the next value on the array"; string invariant_error = "The invariants have been broken."; MinHeap<int> myHeap = new MinHeap<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(); TestMinSort(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"); TestMinPop(myHeap, pop_error); int elements = 20000; myHeap = new MinHeap<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); TestMinInvariant(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.PopMin(); TestMinInvariant(myHeap, invariant_error); } Console.WriteLine("Heap too big to print on console, current elements in heap: {0}\n", myHeap.Count); }
private static void TestMinInvariant(MinHeap<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(); }