Beispiel #1
0
        public void HeapifyUnsortedCollectionAndCheckIfExtractedInSortedOrder()
        {
            var heap = new BinomialMinHeap <int>();

            var unsortedList = new List <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)
                {
                    unsortedList.Add(el);
                    addedElements++;
                    el += i;
                }
            }

            heap.Heapify(unsortedList);

            if (heap.Count != addedElements)
            {
                Assert.Fail("1");
            }

            int removedElements = 0;
            var min             = heap.PeekMin();

            while (!heap.IsEmpty)
            {
                if (min > heap.PeekMin())
                {
                    Assert.Fail("2");
                }

                min = heap.PopMin();
                removedElements++;
            }

            Assert.IsTrue(heap.IsEmpty &&
                          heap.Count == 0 &&
                          addedElements == removedElements);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        public void ConvertingToBinomialMinHeap()
        {
            var maxHeap = new BinomialMaxHeap <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)
                {
                    maxHeap.Add(el);
                    addedElements++;
                    el += i;
                }
            }

            if (maxHeap.Count != addedElements)
            {
                Assert.Fail();
            }

            // Binomial min heap heapified from the max heap. Has to be the same
            // as the converted min heap
            var heapifiedMinHeap = new BinomialMinHeap <int>();

            heapifiedMinHeap.Heapify(maxHeap.ToArray());

            var minHeap = maxHeap.ToMinHeap();

            if (minHeap.Count != heapifiedMinHeap.Count)
            {
                Assert.Fail();
            }

            var min1 = heapifiedMinHeap.PeekMin();
            var min2 = minHeap.PeekMin();

            int removedElements = 0;

            while (!heapifiedMinHeap.IsEmpty && !minHeap.IsEmpty)
            {
                if (min1 > heapifiedMinHeap.PeekMin())
                {
                    Assert.Fail();
                }
                if (min2 > minHeap.PeekMin())
                {
                    Assert.Fail();
                }

                min1 = heapifiedMinHeap.PopMin();
                min2 = minHeap.PopMin();
                removedElements++;

                if (min1 != min2)
                {
                    Assert.Fail();
                }
            }

            Assert.IsTrue(heapifiedMinHeap.IsEmpty &&
                          minHeap.IsEmpty &&
                          heapifiedMinHeap.Count == 0 &&
                          minHeap.Count == 0 &&
                          addedElements == removedElements);
        }