Esempio n. 1
0
        public static void AddTest <T>(IEnumerable <T> collection)
        {
            var mmh = new MinMaxHeap <T>();

            foreach (var item in collection)
            {
                mmh.Add(item);
            }
            var minValue = mmh.GetMin();
            var maxValue = mmh.GetMax();

            Assert.AreEqual(collection.Min(), minValue);
            Assert.AreEqual(collection.Max(), maxValue);
            Assert.AreEqual(collection.Count(), mmh.Count);
        }
Esempio n. 2
0
        public static void CustomComparerTest()
        {
            var arr      = new [] { "aaaa", "c", "dd", "bbb" };
            var comparer = Comparer <string> .Create((a, b) => Comparer <int> .Default.Compare(a.Length, b.Length));

            var mmh = new MinMaxHeap <string>(comparer: comparer);

            foreach (var s in arr)
            {
                mmh.Add(s);
            }

            Assert.AreEqual(comparer, mmh.Comparer);
            Assert.AreEqual("c", mmh.GetMin());
            Assert.AreEqual("aaaa", mmh.GetMax());
        }
Esempio n. 3
0
        static void HeapTest()
        { 
            MinMaxHeap<int> heapRemoveMin = new MinMaxHeap<int>();
            heapRemoveMin.DebugCheckHeapProperty = true;
            MinMaxHeap<int> heapRemoveMax = new MinMaxHeap<int>();
            heapRemoveMax.DebugCheckHeapProperty = true;
            List<int> doubleCheckMin = new List<int>();
            List<int> doubleCheckMax = new List<int>();
            Random r = new Random();

            // Generate the list of numbers to populate the heaps and to check against
            if (true)
            {
                for (int i = 0; i < 700; i++)
                {
                    int randInt = r.Next(-10000, 10000);
                    doubleCheckMin.Add(randInt);
                }
            }
            else
            {
                // Manually test degenerate cases
                doubleCheckMin.Add(-55);
                doubleCheckMin.Add(31);
                doubleCheckMin.Add(-93);
                //doubleCheckMin.Add(64);
            }

            for (int i = 0; i < doubleCheckMin.Count; i++)
            {
                int randInt = doubleCheckMin[i];
                // heap.Add("" + i, i);
                heapRemoveMin.Add(randInt, randInt);
                heapRemoveMax.Add(randInt, randInt);
                doubleCheckMax.Add(randInt);
            }
            doubleCheckMin.Sort(); // Default. Ascending
            doubleCheckMax.Sort(delegate (int x, int y)
            {
                if (x == y) return 0;
                if (x > y) return -1;
                if (x < y) return 1;
                return 0;
            });
            Console.WriteLine(" -- NOW REMOVE MIN --");
            int checkCount = 0;
            while (heapRemoveMin.Count > 0)
            {
                int min = heapRemoveMin.FindMin();
                if (doubleCheckMin[checkCount] != min)
                {
                    throw new Exception("WRONG!");
                }
                heapRemoveMin.RemoveMin();
                checkCount++;
                Console.WriteLine(min);
            }
            Console.WriteLine(" -- NOW REMOVE MAX --");
            checkCount = 0;
            while (heapRemoveMax.Count > 0)
            {
                //Console.WriteLine("iteration " + checkCount);
                //Console.WriteLine(heapRemoveMax.PrintTree());
                int max = heapRemoveMax.RemoveMax();
                if (doubleCheckMax[checkCount] != max)
                {
                    throw new Exception("WRONG!");
                }
                checkCount++;
                Console.WriteLine(max);
            }
        }