/// <summary>Copies the elements stored in the queue to a new array.</summary>
 /// <returns>A new array containing a snapshot of elements copied from the queue.</returns>
 public KeyValuePair <TKey, TValue>[] ToArray()
 {
     lock (_syncLock)
     {
         if (_useMinHeap)
         {
             var clonedHeap = new MinBinaryHeap(_minHeap);
             var result     = new KeyValuePair <TKey, TValue> [_minHeap.Count];
             for (int i = 0; i < result.Length; i++)
             {
                 result[i] = clonedHeap.Remove();
             }
             return(result);
         }
         else
         {
             var clonedHeap = new MaxBinaryHeap(_maxHeap);
             var result     = new KeyValuePair <TKey, TValue> [_maxHeap.Count];
             for (int i = 0; i < result.Length; i++)
             {
                 result[i] = clonedHeap.Remove();
             }
             return(result);
         }
     }
 }
예제 #2
0
        public void TopElementIsInsertedWhenHeapIsEmpty()
        {
            var sut = new MaxBinaryHeap <int>();

            sut.Insert(10);
            Assert.Equal(10, sut.GetTop());
        }
        public void Insert_SeveralValues_ExpectCorrectMaxBinaryHeapAfterEachInsert()
        {
            var heap = new MaxBinaryHeap <int, string>(new List <KeyValuePair <int, string> > {
            });

            heap.Insert(_nodeA, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(1, heap));

            heap.Insert(_nodeB, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(2, heap));

            heap.Insert(_nodeC, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(3, heap));

            heap.Insert(_nodeD, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(4, heap));

            heap.Insert(_nodeE, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(5, heap));

            heap.Insert(_nodeF, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(6, heap));

            heap.Insert(_nodeG, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(7, heap));

            heap.Insert(_nodeH, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(8, heap));

            heap.Insert(_nodeI, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(9, heap));
        }
예제 #4
0
        static MaxBinaryHeap <int, int> CreateMaxBinaryHeap()
        {
            MaxBinaryHeap <int, int> heap = new MaxBinaryHeap <int, int>();
            var heapData = GetMaxHeapTestData();

            heapData.ForEach(x => heap.Insert(x.Key, x.Value));
            return(heap);
        }
 /// <summary>
 /// Checks whether the heap is a proper Max heap.
 /// </summary>
 /// <typeparam name="TKey">Type of the keys stored in the heap. </typeparam>
 /// <typeparam name="TValue">Type of the values stored in the heap. </typeparam>
 /// <param name="arraySize">Size of the heap array. </param>
 /// <param name="heap">A Max binary heap. </param>
 /// <returns>True if the heap is a proper Max binary heap, and false otherwise. </returns>
 public static bool HasMaxOrderPropertyForHeap <TKey, TValue>(int arraySize, MaxBinaryHeap <TKey, TValue> heap) where TKey : IComparable <TKey>
 {
     for (int i = 0; i < arraySize; i++)
     {
         HasMaxOrderPropertyForNode(heap, i);
     }
     return(true);
 }
예제 #6
0
        public void Add_ExpectCountIncrement()
        {
            var heap = new MaxBinaryHeap<int>();

            heap.Add(10);

            Assert.AreEqual(1, heap.Count);
        }
예제 #7
0
        public void TestMaxBinaryHeap()
        {
            MaxBinaryHeap mbh = new MaxBinaryHeap();

            TestInsert(mbh, HeapType.MAX_HEAP);

            mbh = new MaxBinaryHeap();
            TestRemove(mbh, HeapType.MAX_HEAP);
        }
예제 #8
0
        public void Add_SecondAddedItemIsGreater_ExpectGreaterItemIsMovedToArrayBeginning()
        {
            var heap = new MaxBinaryHeap<int>();

            heap.Add(10);
            heap.Add(15);

            CollectionAssert.AreEqual(new[] { 15, 10 }, heap.ToArray());
        }
예제 #9
0
        public void Add_ExpectHeapContainsItem()
        {
            var heap = new MaxBinaryHeap<int>();
            var item = 10;

            heap.Add(item);

            CollectionAssert.AreEquivalent(new[] { item }, heap.ToArray());
        }
        public void BuildHeap_Itratively()
        {
            var heap = new MaxBinaryHeap <int, string>(_keyValues);

            heap.BuildHeap_Iteratively(heap.HeapArray.Count);

            Assert.AreEqual(9, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(_keyValues.Count, heap));
        }
예제 #11
0
        public void MaxBinaryHeapSizeTest()
        {
            MaxBinaryHeap <int, int> heap = new MaxBinaryHeap <int, int>();

            Assert.AreEqual(0, heap.Size);
            heap.Insert(1, 11);
            heap.Insert(2, 22);
            Assert.AreEqual(2, heap.Size);
        }
예제 #12
0
        public void MaxValueElementIsOnTheTop()
        {
            var sut = new MaxBinaryHeap <int>();

            sut.Insert(10);
            sut.Insert(20);

            Assert.Equal(20, sut.GetTop());
            Assert.Equal(10, sut.GetTop());
        }
예제 #13
0
        public void MaxBinaryHeapConstructorTest()
        {
            var minHeapDefault = new MaxBinaryHeap <string, int>();
            var minHeapCustom  = new MaxBinaryHeap <string, int>(16);

            Assert.NotNull(minHeapDefault);
            Assert.NotNull(minHeapCustom);
            Assert.AreEqual(32, minHeapDefault.Capacity);
            Assert.AreEqual(32, minHeapDefault.Capacity);
        }
예제 #14
0
        public void Add_MoreThanOneSwap_ExpectMaxItemIsMovedToArrayBeginning()
        {
            var heap = new MaxBinaryHeap<int>();

            heap.Add(1);
            heap.Add(3);
            heap.Add(5);
            heap.Add(8);

            CollectionAssert.AreEqual(new[] { 8, 5, 3, 1 }, heap.ToArray());
        }
예제 #15
0
        public void MaxBinaryHeapIsEmptyTest()
        {
            MaxBinaryHeap <int, int> heap = new MaxBinaryHeap <int, int>();

            Assert.IsTrue(heap.IsEmpty);
            heap.Insert(1, 1);
            heap.Insert(2, 2);
            Assert.IsFalse(heap.IsEmpty);
            heap.Clear();
            Assert.IsTrue(heap.IsEmpty);
        }
        public void Insert_Should_ReturnArray()
        {
            //arrange
            var heap = new MaxBinaryHeap();

            //act
            heap.Insert(55);

            //assert
            Assert.That(heap.values[0], Is.EqualTo(55));
        }
예제 #17
0
        public void MaxBinaryHeapGetMaximumValueTest()
        {
            MaxBinaryHeap <int, int> heap = new MaxBinaryHeap <int, int>();

            heap.Insert(2, 22);
            heap.Insert(3, 33);
            heap.Insert(1, 11);
            Assert.AreEqual(3, heap.Size);
            Assert.AreEqual(33, heap.GetMaximumValue());
            Assert.AreEqual(3, heap.Size);
        }
예제 #18
0
        private static MaxBinaryHeap <int> ReadBinaryHeap()
        {
            int[] nodes = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            MaxBinaryHeap <int> result = new MaxBinaryHeap <int>();

            foreach (int t in nodes)
            {
                result.Insert(t);
            }

            return(result);
        }
예제 #19
0
        public MaxBinaryHeap <int, int> TestMaxHeap()
        {
            var values = new List <int>()
            {
                3, 7, 10, 1, 4, 4, 0, 20, 15, 12
            };
            var maxHeap = new MaxBinaryHeap <int, int>(values.Count);

            values.ForEach(v => maxHeap.Add(v, v));

            return(maxHeap);
        }
        public void Insert_Should_BubbleLargestNumberToTop()
        {
            //arrange
            var heap = new MaxBinaryHeap()
            {
                values = { 41, 39, 33, 18, 27, 12 }
            };

            //act
            heap.Insert(55);

            //assert
            Assert.That(heap.values[0], Is.EqualTo(55));
        }
예제 #21
0
        public void DeleteMax_MoreThanOneSwap_ExpectHeapIsValid()
        {
            var heap = new MaxBinaryHeap<int>();

            //three complete layers in tree + one element on the forth layer
            var items = new[] { 1, 3, 5, 8, 13, 21, 34, 55 };
            foreach (var item in items)
            {
                heap.Add(item);
            }

            heap.DeleteMax();

            var expectedArray = new[] { 34, 8, 21, 1, 5, 3, 13 };
            CollectionAssert.AreEqual(expectedArray, heap.ToArray());
        }
예제 #22
0
        public void MaxBinaryHeapPercolateUpTest()
        {
            var heapData = GetMaxHeapTestData();

            Assert.AreEqual(0, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 0));
            Assert.AreEqual(1, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 1));
            Assert.AreEqual(2, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 2));
            heapData[5] = new BinaryHeapBase <int, int> .KeyValuePair(20, 20);

            Assert.AreEqual(0, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 5));
            CollectionAssertEx.AreEqual(new int[] { 20, 13, 17, 1, 4, 6, 5 }, heapData.Select(x => x.Key));
            heapData[6] = new BinaryHeapBase <int, int> .KeyValuePair(18, 18);

            Assert.AreEqual(2, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 6));
            CollectionAssertEx.AreEqual(new int[] { 20, 13, 18, 1, 4, 6, 17 }, heapData.Select(x => x.Key));
        }
예제 #23
0
        public void MaxBinaryHeapPercolateDownTest()
        {
            var heapData = GetMaxHeapTestData();

            Assert.AreEqual(3, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 3));
            Assert.AreEqual(4, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 4));
            Assert.AreEqual(5, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 5));
            Assert.AreEqual(6, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 6));
            heapData[0] = new BinaryHeapBase <int, int> .KeyValuePair(0, 0);

            Assert.AreEqual(4, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 0));
            CollectionAssertEx.AreEqual(new int[] { 13, 4, 6, 1, 0, 2, 5 }, heapData.Select(x => x.Key));
            heapData[0] = new BinaryHeapBase <int, int> .KeyValuePair(3, 3);

            Assert.AreEqual(6, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 0));
            CollectionAssertEx.AreEqual(new int[] { 6, 4, 5, 1, 0, 2, 3 }, heapData.Select(x => x.Key));
        }
예제 #24
0
        public void MaxBinaryHeapInsertTest()
        {
            MaxBinaryHeap <int, int> heap = new MaxBinaryHeap <int, int>();

            heap.Insert(1, 10);
            heap.Insert(4, 40);
            heap.Insert(2, 20);
            heap.Insert(5, 50);
            heap.Insert(13, 130);
            heap.Insert(6, 60);
            heap.Insert(17, 170);
            List <int> valueList = new List <int>();

            while (heap.Size != 0)
            {
                valueList.Add(heap.DeleteMaximumValue());
            }
            CollectionAssert.AreEqual(new int[] { 170, 130, 60, 50, 40, 20, 10 }, valueList);
        }
예제 #25
0
        public void MaxValueElementIsAlwaysOnTheTop()
        {
            var sut          = new MaxBinaryHeap <int>(1001);
            var randomValues = new int[1000];
            var random       = new Random();

            for (var i = 0; i < 1000; i++)
            {
                randomValues[i] = random.Next(10000);
                sut.Insert(randomValues[i]);
            }

            var orderedArray = randomValues.OrderByDescending(arg => arg).ToArray();

            for (var i = 0; i < 1000; i++)
            {
                Assert.Equal(orderedArray[i], sut.GetTop());
            }
        }
예제 #26
0
        public static List <T> Sort <T>(List <T> list) where T : IComparable <T>
        {
            // 1- re-arrange elements in the list into a max heap.
            var maxHeap = new MaxBinaryHeap <T, T>(ToHeapList(list));

            maxHeap.BuildHeap_Recursively(list.Count);

            // 2- Starting from last element in the list, repeat the following two steps for all the elements in the list, except the first one.
            for (int i = list.Count - 1; i > 0; i--)
            {
                /* Since the root element/node in a max heap is the maximum value in the list, putting it in the last position of the unsorted part of the array, determines its final position in an array that is eventually ordered ascending.*/
                Utils.Swap(maxHeap.HeapArray, 0, i);

                /* Since the new value in the root position of the heap (index :0) may not be in its correct position, heap-order-wise, then bubble it down, until it reaches its correct position.*/
                maxHeap.BubbleDown_Recursively(0, i);
            }

            return(ToList(maxHeap.HeapArray));
        }
        public void TryRemoveRoot_RemoveRoot_SeveralTimes_ExpectDescendingOrderInResults()
        {
            var heap = new MaxBinaryHeap <int, string>(_keyValues);

            heap.BuildHeap_Recursively(heap.HeapArray.Count);

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue1, heap.HeapArray.Count));
            Assert.AreEqual(100, maxValue1.Key);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(8, heap));

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue2, heap.HeapArray.Count));
            Assert.AreEqual(72, maxValue2.Key);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(7, heap));

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue3, heap.HeapArray.Count));
            Assert.AreEqual(56, maxValue3.Key);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(6, heap));

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue4, heap.HeapArray.Count));
            Assert.AreEqual(32, maxValue4.Key);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(5, heap));

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue5, heap.HeapArray.Count));
            Assert.AreEqual(20, maxValue5.Key);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(4, heap));

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue6, heap.HeapArray.Count));
            Assert.AreEqual(10, maxValue6.Key);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(3, heap));

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue7, heap.HeapArray.Count));
            Assert.AreEqual(5, maxValue7.Key);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(2, heap));

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue8, heap.HeapArray.Count));
            Assert.AreEqual(3, maxValue8.Key);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(1, heap));

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue9, heap.HeapArray.Count));
            Assert.AreEqual(1, maxValue9.Key);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(0, heap));
        }
예제 #28
0
        public void MaxBinaryHeapBuildTest()
        {
            MaxBinaryHeap <int, int> heap = new MaxBinaryHeap <int, int>();

            heap.Build(
                new BinaryHeapBase <int, int> .KeyValuePair(1, 10),
                new BinaryHeapBase <int, int> .KeyValuePair(4, 40),
                new BinaryHeapBase <int, int> .KeyValuePair(2, 20),
                new BinaryHeapBase <int, int> .KeyValuePair(5, 50),
                new BinaryHeapBase <int, int> .KeyValuePair(13, 130),
                new BinaryHeapBase <int, int> .KeyValuePair(6, 60),
                new BinaryHeapBase <int, int> .KeyValuePair(17, 170)
                );
            List <int> valueList = new List <int>();

            while (heap.Size != 0)
            {
                valueList.Add(heap.DeleteMaximumValue());
            }
            CollectionAssert.AreEqual(new int[] { 170, 130, 60, 50, 40, 20, 10 }, valueList);
        }
 /// <summary>Initializes a heap as a copy of another heap instance.</summary>
 /// <param name="heapToCopy">The heap to copy.</param>
 /// <remarks>Key/Value values are not deep cloned.</remarks>
 public MaxBinaryHeap(MaxBinaryHeap heapToCopy)
 {
     _items = new List <KeyValuePair <TKey, TValue> >(heapToCopy.Items);
 }
예제 #30
0
        public void DeleteMax_TwoNotEqualItems_ExpectCountDecrement()
        {
            var heap = new MaxBinaryHeap<int>();
            heap.Add(10);
            heap.Add(20);

            heap.DeleteMax();

            Assert.AreEqual(1, heap.Count);
        }
예제 #31
0
        public void DeleteMax_TwoNotEqualItems_ExpectReturnMax()
        {
            var heap = new MaxBinaryHeap<int>();
            heap.Add(10);
            heap.Add(20);

            var max = heap.DeleteMax();

            Assert.AreEqual(20, max);

            heap = new MaxBinaryHeap<int>();
            heap.Add(20);
            heap.Add(10);

            max = heap.DeleteMax();

            Assert.AreEqual(20, max);
        }
예제 #32
0
        public void PeekMax_ExpectCountNotChange()
        {
            var heap = new MaxBinaryHeap<int>();
            heap.Add(10);

            heap.PeekMax();

            Assert.AreEqual(1, heap.Count);
        }
예제 #33
0
        public void PeekMax_OneItem_ExpectReturnItem()
        {
            var heap = new MaxBinaryHeap<int>();
            heap.Add(10);

            var peekedItem = heap.PeekMax();

            Assert.AreEqual(10, peekedItem);
        }
예제 #34
0
        public void Create_ExpectCountToBeZero()
        {
            var heap = new MaxBinaryHeap<int>();

            Assert.AreEqual(0, heap.Count);
        }
예제 #35
0
        public void DeleteMax_EmptyHeap_ExpectThrowInvalidOperationException()
        {
            var heap = new MaxBinaryHeap<int>();

            heap.DeleteMax();
        }
예제 #36
0
 public void MaxBinaryHeapGetValueGuardCase3Test()
 {
     var heap   = new MaxBinaryHeap <int, int>();
     var result = heap.GetValue(0);
 }
예제 #37
0
        public void DeleteMax_OneItem_ExpectReturnItem()
        {
            var heap = new MaxBinaryHeap<int>();
            heap.Add(10);

            var max = heap.DeleteMax();

            Assert.AreEqual(10, max);
        }
예제 #38
0
        public void MaxBinaryHeapPercolateUpGuardCase2Test()
        {
            var heapData = GetMaxHeapTestData();

            MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 7);
        }
예제 #39
0
        public void MaxBinaryHeapInsertGuardTest()
        {
            MaxBinaryHeap <string, int> heap = new MaxBinaryHeap <string, int>();

            heap.Insert(null, 1);
        }
예제 #40
0
        public void MaxBinaryHeapPercolateDownGuardCase1Test()
        {
            var heapData = GetMaxHeapTestData();

            MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, -1);
        }
예제 #41
0
        public void DeleteMax_OneItem_ExpectCountDecrement()
        {
            var heap = new MaxBinaryHeap<int>();
            heap.Add(10);

            heap.DeleteMax();

            Assert.AreEqual(0, heap.Count);
        }
예제 #42
0
 public void MaxBinaryHeapDeleteMaximumValueGuardTest()
 {
     MaxBinaryHeap <int, int> heap = new MaxBinaryHeap <int, int>();
     int result = heap.DeleteMaximumValue();
 }
예제 #43
0
        public void MaxBinaryHeapBuildGuardTest()
        {
            MaxBinaryHeap <int, int> heap = new MaxBinaryHeap <int, int>();

            heap.Build(null);
        }