Esempio n. 1
0
 public void ReplaceMax_Empty()
 {
     Assert.Throws <InvalidOperationException>(delegate
     {
         _binaryMaxHeap.ReplaceMax(2);
     });
 }
Esempio n. 2
0
        public void ReplacingMinElementAndCheckingIfExtractedInSortedOrder()
        {
            var heap = new BinaryMaxHeap <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.ReplaceMax(int.MinValue);
            heap.ReplaceMax(int.MinValue);
            heap.ReplaceMax(int.MinValue);

            int removedElements = 0;
            var max             = heap.PeekMax();

            while (!heap.IsEmpty)
            {
                if (max < heap.PeekMax())
                {
                    Assert.Fail();
                }

                max = heap.PopMax();
                removedElements++;
            }

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