Пример #1
0
        public void BuildHeap_Iteratively()
        {
            var heap = new MinBinaryHeap <int, string>(_keyValues);

            heap.BuildHeap_Iteratively(heap.HeapArray.Count);

            Assert.AreEqual(9, heap.HeapArray.Count);
            Assert.IsTrue(HasMinOrderPropertyForHeap(_keyValues.Count, heap));
        }
Пример #2
0
        public void TryRemoveRoot_RemoveRoot_SeveralTimes_ExpectsAscendingOrderInResults()
        {
            var keyValues = new List <KeyValuePair <int, string> > {
                new KeyValuePair <int, string>(150, "A"),
                new KeyValuePair <int, string>(70, "B"),
                new KeyValuePair <int, string>(202, "C"),
                new KeyValuePair <int, string>(34, "D"),
                new KeyValuePair <int, string>(42, "E"),
                new KeyValuePair <int, string>(1, "F"),
                new KeyValuePair <int, string>(3, "G"),
                new KeyValuePair <int, string>(10, "H"),
                new KeyValuePair <int, string>(21, "I")
            };

            var heap = new MinBinaryHeap <int, string>(keyValues);

            heap.BuildHeap_Iteratively(heap.HeapArray.Count);

            // The values in the array are expected to be removed in ascending order.
            bool result1 = heap.TryRemoveRoot(out KeyValuePair <int, string> min1, heap.HeapArray.Count);

            Assert.IsTrue(result1);
            Assert.AreEqual(1, min1.Key);

            bool result2 = heap.TryRemoveRoot(out KeyValuePair <int, string> min2, heap.HeapArray.Count);

            Assert.IsTrue(result2);
            Assert.AreEqual(3, min2.Key);

            bool result3 = heap.TryRemoveRoot(out KeyValuePair <int, string> min3, heap.HeapArray.Count);

            Assert.IsTrue(result3);
            Assert.AreEqual(10, min3.Key);

            bool result4 = heap.TryRemoveRoot(out KeyValuePair <int, string> min4, heap.HeapArray.Count);

            Assert.IsTrue(result4);
            Assert.AreEqual(21, min4.Key);

            bool result5 = heap.TryRemoveRoot(out KeyValuePair <int, string> min5, heap.HeapArray.Count);

            Assert.IsTrue(result5);
            Assert.AreEqual(34, min5.Key);

            bool result6 = heap.TryRemoveRoot(out KeyValuePair <int, string> min6, heap.HeapArray.Count);

            Assert.IsTrue(result6);
            Assert.AreEqual(42, min6.Key);

            bool result7 = heap.TryRemoveRoot(out KeyValuePair <int, string> min7, heap.HeapArray.Count);

            Assert.IsTrue(result7);
            Assert.AreEqual(70, min7.Key);

            bool result8 = heap.TryRemoveRoot(out KeyValuePair <int, string> min8, heap.HeapArray.Count);

            Assert.IsTrue(result8);
            Assert.AreEqual(150, min8.Key);

            bool result9 = heap.TryRemoveRoot(out KeyValuePair <int, string> min9, heap.HeapArray.Count);

            Assert.IsTrue(result9);
            Assert.AreEqual(202, min9.Key);
        }