public void MergingTwoHeapsAndCheckingIfExtractedInSortedOrder() { var heap1 = new BinomialMaxHeap <int>(); var heap2 = new BinomialMaxHeap <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 BinomialMaxHeap <int>(); oldHeap1.Heapify(heap1.ToArray()); var oldHeap2 = new BinomialMaxHeap <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 max1 = heap1.PeekMax(); var max2 = heap2.PeekMax(); if (max1 != max2) { Assert.Fail("merged heaps min element is different"); } int removedElements = 0; while (!heap1.IsEmpty && !heap2.IsEmpty) { if (max1 < heap1.PeekMax()) { Assert.Fail(); } if (max2 < heap2.PeekMax()) { Assert.Fail(); } max1 = heap1.PopMax(); max2 = heap2.PopMax(); removedElements++; if (max1 != max2) { Assert.Fail("merged heaps min element is different"); } } Assert.IsTrue(heap1.IsEmpty && heap2.IsEmpty && heap1.Count == 0 && heap2.Count == 0 && mergedHeapElements == removedElements); }