public static void DoTest() { int i = 37; int numberOfItems = 100000; BinomialMinHeap<int> firstHeap = new BinomialMinHeap<int>(); BinomialMinHeap<int> secondHeap = new BinomialMinHeap<int>(); BinomialMinHeap<int> thirdHeap = new BinomialMinHeap<int>(); Console.WriteLine("Begin Test."); for (i = 37; i != 0; i = (i + 37) % numberOfItems) { if (i % 2 == 0) secondHeap.Add(i); else firstHeap.Add(i); } firstHeap.Merge(secondHeap); thirdHeap = firstHeap; for (i = 1; i <= thirdHeap.Count; i++) { var min = thirdHeap.ExtractMin(); Debug.Assert(min == i, "WRONG MIN"); } Debug.Assert(secondHeap.IsEmpty, "SECOND HEAP SHOULD BE EMPTY"); Console.WriteLine("END OF TEST"); Console.ReadLine(); }
public static void DoTest() { int i = 37; int numberOfItems = 100000; BinomialMinHeap <int> firstHeap = new BinomialMinHeap <int>(); BinomialMinHeap <int> secondHeap = new BinomialMinHeap <int>(); BinomialMinHeap <int> thirdHeap = new BinomialMinHeap <int>(); for (i = 37; i != 0; i = (i + 37) % numberOfItems) { if (i % 2 == 0) { secondHeap.Add(i); } else { firstHeap.Add(i); } } firstHeap.Merge(secondHeap); thirdHeap = firstHeap; for (i = 1; i <= thirdHeap.Count; i++) { var min = thirdHeap.ExtractMin(); Assert.True(min == i, "WRONG MIN"); } Assert.True(secondHeap.IsEmpty, "SECOND HEAP SHOULD BE EMPTY"); }
public void ReplacingMinElementAndCheckingIfExtractedInSortedOrder() { var heap = new BinomialMinHeap <int>(); int maxHeapElement = 50000; int minHeapElement = -50000; int addedElements = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minHeapElement; while (el <= maxHeapElement) { heap.Add(el); addedElements++; el += i; } } if (heap.Count != addedElements) { Assert.Fail(); } heap.ReplaceMin(int.MaxValue); heap.ReplaceMin(int.MaxValue); heap.ReplaceMin(int.MaxValue); int removedElements = 0; var min = heap.PeekMin(); while (!heap.IsEmpty) { if (min > heap.PeekMin()) { Assert.Fail(); } min = heap.PopMin(); removedElements++; } Assert.IsTrue(heap.IsEmpty && heap.Count == 0 && addedElements == removedElements); }
public static void DoTest() { int i = 37; int numberOfItems = 100000; BinomialMinHeap <int> firstHeap = new BinomialMinHeap <int>(); BinomialMinHeap <int> secondHeap = new BinomialMinHeap <int>(); BinomialMinHeap <int> thirdHeap = new BinomialMinHeap <int>(); Console.WriteLine("Begin Test."); for (i = 37; i != 0; i = (i + 37) % numberOfItems) { if (i % 2 == 0) { secondHeap.Add(i); } else { firstHeap.Add(i); } } firstHeap.Merge(secondHeap); thirdHeap = firstHeap; for (i = 1; i <= thirdHeap.Count(); i++) { var min = thirdHeap.ExtractMin(); Debug.Assert(min == i, "WRONG MIN"); } Debug.Assert(secondHeap.IsEmpty(), "SECOND HEAP SHOULD BE EMPTY"); Console.WriteLine("END OF TEST"); Console.ReadLine(); }
public void ConvertingToBinomialMaxHeap() { var minHeap = new BinomialMinHeap <int>(); int maxHeapElement = 50000; int minHeapElement = -50000; int addedElements = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minHeapElement; while (el <= maxHeapElement) { minHeap.Add(el); addedElements++; el += i; } } if (minHeap.Count != addedElements) { Assert.Fail(); } // Binomial max heap heapified from the min heap. Has to be the same // as the converted max heap var hepifiedMaxHeap = new BinomialMaxHeap <int>(); hepifiedMaxHeap.Heapify(minHeap.ToArray()); var maxHeap = minHeap.ToMaxHeap(); if (maxHeap.Count != hepifiedMaxHeap.Count) { Assert.Fail(); } var max1 = hepifiedMaxHeap.PeekMax(); var max2 = maxHeap.PeekMax(); int removedElements = 0; while (!hepifiedMaxHeap.IsEmpty && !maxHeap.IsEmpty) { if (max1 < hepifiedMaxHeap.PeekMax()) { Assert.Fail(); } if (max2 < maxHeap.PeekMax()) { Assert.Fail(); } max1 = hepifiedMaxHeap.PopMax(); max2 = maxHeap.PopMax(); removedElements++; if (max1 != max2) { Assert.Fail(); } } Assert.IsTrue(hepifiedMaxHeap.IsEmpty && maxHeap.IsEmpty && hepifiedMaxHeap.Count == 0 && maxHeap.Count == 0 && addedElements == removedElements); }
public void MergingTwoHeapsAndCheckingIfExtractedInSortedOrder() { var heap1 = new BinomialMinHeap <int>(); var heap2 = new BinomialMinHeap <int>(); int maxElementInFirstHeap = 100000; int minElementInFirstHeap = 0; int addedElementsInFirstHeap = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minElementInFirstHeap; while (el <= maxElementInFirstHeap) { heap1.Add(el); addedElementsInFirstHeap++; el += i; } } int maxElementInSecondHeap = 50000; int minElementInSecondHeap = -50000; int addedElementsInSecondHeap = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minElementInSecondHeap; while (el <= maxElementInSecondHeap) { heap2.Add(el); addedElementsInSecondHeap++; el += i; } } if (heap1.Count != addedElementsInFirstHeap) { Assert.Fail("first heap incorrect count"); } if (heap2.Count != addedElementsInSecondHeap) { Assert.Fail("second heap incorrect count"); } var oldHeap1 = new BinomialMinHeap <int>(); oldHeap1.Heapify(heap1.ToArray()); var oldHeap2 = new BinomialMinHeap <int>(); oldHeap2.Heapify(heap2.ToArray()); heap1.Merge(heap2); heap2.Heapify(oldHeap2.ToArray()); heap2.Merge(oldHeap1); int mergedHeapElements = addedElementsInFirstHeap + addedElementsInSecondHeap; if (heap1.Count != mergedHeapElements) { Assert.Fail("merged first with second incorect count"); } if (heap2.Count != mergedHeapElements) { Assert.Fail("merged second with first incorrect count"); } var min1 = heap1.PeekMin(); var min2 = heap2.PeekMin(); if (min1 != min2) { Assert.Fail("merged heaps min element is different"); } int removedElements = 0; while (!heap1.IsEmpty && !heap2.IsEmpty) { if (min1 > heap1.PeekMin()) { Assert.Fail(); } if (min2 > heap2.PeekMin()) { Assert.Fail(); } min1 = heap1.PopMin(); min2 = heap2.PopMin(); removedElements++; if (min1 != min2) { Assert.Fail("merged heaps min element is different"); } } Assert.IsTrue(heap1.IsEmpty && heap2.IsEmpty && heap1.Count == 0 && heap2.Count == 0 && mergedHeapElements == removedElements); }
public void AddingAfterClearingHeap() { var heap = new BinomialMinHeap <int>(); int maxHeapElement = 50000; int minHeapElement = -50000; int addedElements = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minHeapElement; while (el <= maxHeapElement) { heap.Add(el); addedElements++; el += i; } } if (heap.Count != addedElements) { Assert.Fail(); } heap.Clear(); if (heap.Count != 0) { Assert.Fail(); } addedElements = 0; //Adding every seventh number, then every fifth number, //every third and at last all numbers //NOTE: some items are added more than once for (int i = 7; i > 0; i -= 2) { int el = minHeapElement; while (el < maxHeapElement) { heap.Add(el); addedElements++; el += i; } } if (heap.Count != addedElements) { Assert.Fail(); } int removedElements = 0; var min = heap.PeekMin(); while (!heap.IsEmpty) { if (min > heap.PeekMin()) { Assert.Fail(); } min = heap.PopMin(); removedElements++; } Assert.IsTrue(heap.IsEmpty && heap.Count == 0 && addedElements == removedElements); }