Ejemplo n.º 1
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);
        }