Exemplo 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))));
        }
Exemplo n.º 2
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);
 }