public static void CheckOrderInHeap_RandomOrder_ReturnsTrue()
        {
            BinaryMaxHeap <long> maxHeap = new BinaryMaxHeap <long>(Comparer <long> .Default);

            maxHeap.Add(23);
            maxHeap.Add(42);
            maxHeap.Add(4);
            maxHeap.Add(16);
            maxHeap.Add(8);
            maxHeap.Add(1);
            maxHeap.Add(3);
            maxHeap.Add(100);
            maxHeap.Add(5);
            maxHeap.Add(7);

            var isRightOrder = IsRightOrderInHeap <long>(maxHeap);

            Assert.True(isRightOrder);
        }
示例#2
0
        public static void CheckOrderInHeap_DecreasingOrder_ReturnsTrue()
        {
            BinaryMaxHeap <long> maxHeap = new BinaryMaxHeap <long>(Comparer <long> .Default);

            maxHeap.Add(10);
            maxHeap.Add(9);
            maxHeap.Add(8);
            maxHeap.Add(7);
            maxHeap.Add(6);
            maxHeap.Add(5);
            maxHeap.Add(4);
            maxHeap.Add(3);
            maxHeap.Add(2);
            maxHeap.Add(1);

            var isRightOrder = IsRightOrderInHeap <long>(maxHeap);

            Assert.IsTrue(isRightOrder);
        }
示例#3
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);
        }
示例#4
0
        public void AddingElementsWithCustomComparerAndCheckingIfExtractedInSortedOrder()
        {
            //Creating heap with reversed comparer
            var heap = new BinaryMaxHeap <int>(Comparer <int> .Create(
                                                   (x, y) => y.CompareTo(x)));

            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();
            }

            int removedElements = 0;
            // because of the reversed comparer
            var min = heap.PeekMax();

            while (!heap.IsEmpty)
            {
                if (min > heap.PeekMax())
                {
                    Assert.Fail();
                }

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

            Assert.IsTrue(heap.IsEmpty &&
                          heap.Count == 0 &&
                          addedElements == removedElements);
        }
示例#5
0
        public void Add_ZeroCapacity()
        {
            var tempBinaryMaxHeap = new BinaryMaxHeap <int>(0);

            Assert.Multiple(delegate
            {
                Assert.IsTrue(tempBinaryMaxHeap.IsEmpty);
                Assert.Zero(tempBinaryMaxHeap.Count, "Count before");
                Assert.Zero(tempBinaryMaxHeap.array.Capacity, "Capacity before");
            });

            tempBinaryMaxHeap.Add(10);

            Assert.Multiple(delegate
            {
                Assert.IsFalse(tempBinaryMaxHeap.IsEmpty);
                Assert.NotZero(tempBinaryMaxHeap.Count, "Count after");
                Assert.NotZero(tempBinaryMaxHeap.array.Capacity, "Capacity after");
            });
        }
示例#6
0
        public void Add_ArrayFull()
        {
            var tempBinaryMaxHeap = new BinaryMaxHeap <int>(4);
            var list = new List <int>()
            {
                1, 4, 3, 5
            };

            tempBinaryMaxHeap.Heapify(list);
            Assert.Multiple(delegate
            {
                Assert.AreEqual(4, tempBinaryMaxHeap.Count, "Count before");
                Assert.AreEqual(4, tempBinaryMaxHeap.array.Capacity, "Capacity before");
            });
            var before = tempBinaryMaxHeap.array.Capacity;

            tempBinaryMaxHeap.Add(10);

            Assert.Multiple(delegate
            {
                Assert.Greater(tempBinaryMaxHeap.Count, before, "Count after");
                Assert.Greater(tempBinaryMaxHeap.array.Capacity, before, "Capacity after");
            });
        }
示例#7
0
        public void ConvertingToBinaryMinHeap()
        {
            var maxHeap = 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)
                {
                    maxHeap.Add(el);
                    addedElements++;
                    el += i;
                }
            }

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

            // Binary max heap with reversed comparer. Have to be the same as the min heap
            var reversedMaxHeap = new BinaryMaxHeap <int>(Comparer <int> .Create(
                                                              (x, y) => y.CompareTo(x)));

            reversedMaxHeap.Heapify(maxHeap.ToArray());

            var minHeap = maxHeap.ToMinHeap();

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

            var min1 = reversedMaxHeap.PeekMax();
            var min2 = minHeap.PeekMin();

            int removedElements = 0;

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

                min1 = reversedMaxHeap.PopMax();
                min2 = minHeap.PopMin();
                removedElements++;

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

            Assert.IsTrue(reversedMaxHeap.IsEmpty &&
                          minHeap.IsEmpty &&
                          reversedMaxHeap.Count == 0 &&
                          minHeap.Count == 0 &&
                          addedElements == removedElements);
        }
示例#8
0
        public void MergingTwoHeapsAndCheckingIfExtractedInSortedOrder()
        {
            var heap1 = new BinaryMaxHeap <int>();
            var heap2 = new BinaryMaxHeap <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 oldHeap = new BinaryMaxHeap <int>();

            oldHeap.Heapify(heap1.ToArray());

            heap1.Merge(heap2);
            heap2.Merge(oldHeap);

            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 max1 = heap1.PeekMax();
            var max2 = heap2.PeekMax();

            if (max1 != max2)
            {
                Assert.Fail("merged heaps min element is different");
            }

            int removedElements = 0;

            while (!heap1.IsEmpty && !heap2.IsEmpty)
            {
                if (max1 < heap1.PeekMax())
                {
                    Assert.Fail();
                }
                if (max2 < heap2.PeekMax())
                {
                    Assert.Fail();
                }

                max1 = heap1.PopMax();
                max2 = heap2.PopMax();
                removedElements++;

                if (max1 != max2)
                {
                    Assert.Fail("merged heaps min element is different");
                }
            }

            Assert.IsTrue(heap1.IsEmpty &&
                          heap2.IsEmpty &&
                          heap1.Count == 0 &&
                          heap2.Count == 0 &&
                          mergedHeapElements == removedElements);
        }
示例#9
0
        public void AddingAfterClearingHeap()
        {
            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.Clear();

            if (heap.Count != 0)
            {
                Assert.Fail();
            }

            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();
            }

            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);
        }
示例#10
0
 public void IsEmpty_False()
 {
     _binaryMaxHeap.Add(5);
     Assert.IsFalse(_binaryMaxHeap.IsEmpty);
 }