Esempio n. 1
0
        public void BuildHeapRecursively_DistinctValues()
        {
            var nodeA     = new KeyValuePair <int, string>(70, "A");
            var nodeB     = new KeyValuePair <int, string>(21, "B");
            var nodeC     = new KeyValuePair <int, string>(220, "C");
            var nodeD     = new KeyValuePair <int, string>(10, "D");
            var nodeE     = new KeyValuePair <int, string>(50, "E");
            var nodeF     = new KeyValuePair <int, string>(34, "F");
            var nodeG     = new KeyValuePair <int, string>(300, "G");
            var nodeH     = new KeyValuePair <int, string>(150, "H");
            var nodeI     = new KeyValuePair <int, string>(2, "I");
            var keyValues = new List <KeyValuePair <int, string> > {
                nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH, nodeI
            };

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

            heap.BuildHeap_Recursively(heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, keyValues.Count));

            Assert.IsFalse(heap.IsMinLevel(heap.GetNodeLevel(keyValues.IndexOf(nodeA))));
            Assert.IsFalse(heap.IsMinLevel(heap.GetNodeLevel(keyValues.IndexOf(nodeB))));
            Assert.IsTrue(heap.IsMinLevel(heap.GetNodeLevel(keyValues.IndexOf(nodeC))));
            Assert.IsTrue(heap.IsMinLevel(heap.GetNodeLevel(keyValues.IndexOf(nodeD))));
            Assert.IsTrue(heap.IsMinLevel(heap.GetNodeLevel(keyValues.IndexOf(nodeE))));
            Assert.IsTrue(heap.IsMinLevel(heap.GetNodeLevel(keyValues.IndexOf(nodeF))));
            Assert.IsFalse(heap.IsMinLevel(heap.GetNodeLevel(keyValues.IndexOf(nodeG))));
            Assert.IsFalse(heap.IsMinLevel(heap.GetNodeLevel(keyValues.IndexOf(nodeH))));
            Assert.IsTrue(heap.IsMinLevel(heap.GetNodeLevel(keyValues.IndexOf(nodeI))));
        }
Esempio n. 2
0
        public void TryRemoveRoot_RemoveRoot_SeveralTimes_ExpectAscendingOrderInResults()
        {
            var nodeA     = new KeyValuePair <int, string>(70, "A");
            var nodeB     = new KeyValuePair <int, string>(21, "B");
            var nodeC     = new KeyValuePair <int, string>(220, "C");
            var nodeD     = new KeyValuePair <int, string>(10, "D");
            var nodeE     = new KeyValuePair <int, string>(50, "E");
            var nodeF     = new KeyValuePair <int, string>(34, "F");
            var nodeG     = new KeyValuePair <int, string>(300, "G");
            var nodeH     = new KeyValuePair <int, string>(150, "H");
            var nodeI     = new KeyValuePair <int, string>(2, "I");
            var keyValues = new List <KeyValuePair <int, string> > {
                nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH, nodeI
            };

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

            heap.BuildHeap_Recursively(heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 9));

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> value1, heap.HeapArray.Count));
            Assert.AreEqual(2, value1.Key);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 8));

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

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

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

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

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

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

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue8, heap.HeapArray.Count));
            Assert.AreEqual(220, maxValue8.Key);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 0));

            Assert.IsTrue(heap.TryRemoveRoot(out KeyValuePair <int, string> maxValue9, heap.HeapArray.Count));
            Assert.AreEqual(300, maxValue9.Key);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 0));
        }
Esempio n. 3
0
 public static bool HasMinMaxHeapProperty <TKey, TValue>(MinMaxBinaryHeap <TKey, TValue> heap, int expectedKeyCount) where TKey : IComparable <TKey>
 {
     for (int i = 0; i < expectedKeyCount; i++)
     {
         int level = heap.GetNodeLevel(i);
         if (heap.IsMinLevel(level))
         {
             Assert.IsTrue(HasMinMaxOrderPropertyForMinLevel(heap, i));
         }
         else
         {
             Assert.IsTrue(HasMinMaxOrderPropertyForMaxLevel(heap, i));
         }
     }
     return(true);
 }
Esempio n. 4
0
        public void Insert_SeveralValues_ExpectCorrectMinMaxBinaryHeapAfterEachInsert()
        {
            var values = new List <KeyValuePair <int, string> >();
            var heap   = new MinMaxBinaryHeap <int, string>(values);

            heap.Insert(new KeyValuePair <int, string>(70, "A"), heap.HeapArray.Count);
            Assert.AreEqual(1, heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 1));

            heap.Insert(new KeyValuePair <int, string>(21, "B"), heap.HeapArray.Count);
            Assert.AreEqual(2, heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 2));

            heap.Insert(new KeyValuePair <int, string>(220, "C"), heap.HeapArray.Count);
            Assert.AreEqual(3, heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 3));

            heap.Insert(new KeyValuePair <int, string>(10, "D"), heap.HeapArray.Count);
            Assert.AreEqual(4, heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 4));

            heap.Insert(new KeyValuePair <int, string>(50, "E"), heap.HeapArray.Count);
            Assert.AreEqual(5, heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 5));

            heap.Insert(new KeyValuePair <int, string>(34, "F"), heap.HeapArray.Count);
            Assert.AreEqual(6, heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 6));

            heap.Insert(new KeyValuePair <int, string>(300, "G"), heap.HeapArray.Count);
            Assert.AreEqual(7, heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 7));

            heap.Insert(new KeyValuePair <int, string>(150, "H"), heap.HeapArray.Count);
            Assert.AreEqual(8, heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 8));

            heap.Insert(new KeyValuePair <int, string>(2, "I"), heap.HeapArray.Count);
            Assert.AreEqual(9, heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, 9));
        }
Esempio n. 5
0
        public void BuildHeapRecursively_DuplicateValues()
        {
            var keyValues = new List <KeyValuePair <int, string> > {
                new KeyValuePair <int, string>(39, "A"),
                new KeyValuePair <int, string>(45, "B"),
                new KeyValuePair <int, string>(37, "C"),
                new KeyValuePair <int, string>(45, "D"),
                new KeyValuePair <int, string>(38, "E"),
                new KeyValuePair <int, string>(50, "F"),
                new KeyValuePair <int, string>(59, "G"),
                new KeyValuePair <int, string>(65, "H"),
                new KeyValuePair <int, string>(27, "I"),
                new KeyValuePair <int, string>(25, "J"),
                new KeyValuePair <int, string>(36, "K"),
                new KeyValuePair <int, string>(30, "L"),
                new KeyValuePair <int, string>(57, "M"),
                new KeyValuePair <int, string>(28, "N")
            };

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

            heap.BuildHeap_Recursively(heap.HeapArray.Count);
            Assert.IsTrue(HasMinMaxHeapProperty(heap, keyValues.Count));
        }