Beispiel #1
0
        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);
        }