public void BuildHeap_Recursively()
        {
            var heap = new MaxBinaryHeap <int, string>(_keyValues);

            heap.BuildHeap_Recursively(heap.HeapArray.Count);

            Assert.AreEqual(9, heap.HeapArray.Count);
            Assert.IsTrue(HasMaxOrderPropertyForHeap(_keyValues.Count, heap));
        }
        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));
        }