示例#1
0
        public void ReplacingMinElementAndCheckingIfExtractedInSortedOrder()
        {
            var heap = new BinaryMinHeap <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);
        }