public static void DoTest()
        {
            int i = 37;
            int numberOfItems = 100000;
            BinomialMinHeap<int> firstHeap = new BinomialMinHeap<int>();
            BinomialMinHeap<int> secondHeap = new BinomialMinHeap<int>();
            BinomialMinHeap<int> thirdHeap = new BinomialMinHeap<int>();

            Console.WriteLine("Begin Test.");

            for (i = 37; i != 0; i = (i + 37) % numberOfItems)
            {
                if (i % 2 == 0)
                    secondHeap.Add(i);
                else
                    firstHeap.Add(i);
            }

            firstHeap.Merge(secondHeap);
            thirdHeap = firstHeap;

            for (i = 1; i <= thirdHeap.Count; i++)
            {
                var min = thirdHeap.ExtractMin();
                Debug.Assert(min == i, "WRONG MIN");
            }

            Debug.Assert(secondHeap.IsEmpty, "SECOND HEAP SHOULD BE EMPTY");

            Console.WriteLine("END OF TEST");

            Console.ReadLine();
        }
        public static void DoTest()
        {
            int i             = 37;
            int numberOfItems = 100000;
            BinomialMinHeap <int> firstHeap  = new BinomialMinHeap <int>();
            BinomialMinHeap <int> secondHeap = new BinomialMinHeap <int>();
            BinomialMinHeap <int> thirdHeap  = new BinomialMinHeap <int>();

            for (i = 37; i != 0; i = (i + 37) % numberOfItems)
            {
                if (i % 2 == 0)
                {
                    secondHeap.Add(i);
                }
                else
                {
                    firstHeap.Add(i);
                }
            }

            firstHeap.Merge(secondHeap);
            thirdHeap = firstHeap;

            for (i = 1; i <= thirdHeap.Count; i++)
            {
                var min = thirdHeap.ExtractMin();
                Assert.True(min == i, "WRONG MIN");
            }

            Assert.True(secondHeap.IsEmpty, "SECOND HEAP SHOULD BE EMPTY");
        }
예제 #3
0
        public static void DoTest()
        {
            int i             = 37;
            int numberOfItems = 100000;
            BinomialMinHeap <int> firstHeap  = new BinomialMinHeap <int>();
            BinomialMinHeap <int> secondHeap = new BinomialMinHeap <int>();
            BinomialMinHeap <int> thirdHeap  = new BinomialMinHeap <int>();

            Console.WriteLine("Begin Test.");

            for (i = 37; i != 0; i = (i + 37) % numberOfItems)
            {
                if (i % 2 == 0)
                {
                    secondHeap.Add(i);
                }
                else
                {
                    firstHeap.Add(i);
                }
            }

            firstHeap.Merge(secondHeap);
            thirdHeap = firstHeap;

            for (i = 1; i <= thirdHeap.Count(); i++)
            {
                var min = thirdHeap.ExtractMin();
                Debug.Assert(min == i, "WRONG MIN");
            }

            Debug.Assert(secondHeap.IsEmpty(), "SECOND HEAP SHOULD BE EMPTY");

            Console.WriteLine("END OF TEST");

            Console.ReadLine();
        }
예제 #4
0
        public void BinomialMinHeap_Test()
        {
            int nodeCount = 1000 * 10;
            //insert test
            var tree = new BinomialMinHeap <int>();

            var nodePointers = new List <BinomialHeapNode <int> >();

            for (int i = 0; i <= nodeCount; i++)
            {
                var node = tree.Insert(i);
                nodePointers.Add(node);
                var theoreticalTreeCount = Convert.ToString(i + 1, 2).Replace("0", "").Length;
                var actualTreeCount      = tree.heapForest.Count();

                Assert.AreEqual(theoreticalTreeCount, actualTreeCount);
            }

            for (int i = 0; i <= nodeCount; i++)
            {
                nodePointers[i].Value--;
                tree.DecrementKey(nodePointers[i]);
            }
            int min = 0;

            for (int i = 0; i <= nodeCount; i++)
            {
                min = tree.ExtractMin();
                Assert.AreEqual(min, i - 1);
            }

            nodePointers.Clear();

            var rnd        = new Random();
            var testSeries = Enumerable.Range(0, nodeCount - 1).OrderBy(x => rnd.Next()).ToList();


            foreach (var item in testSeries)
            {
                nodePointers.Add(tree.Insert(item));
            }

            min          = tree.ExtractMin();
            nodePointers = nodePointers.Where(x => x.Value != min).ToList();
            var resultSeries = new List <int>();

            for (int i = 0; i < nodePointers.Count; i++)
            {
                nodePointers[i].Value = nodePointers[i].Value - rnd.Next(0, 1000);
                tree.DecrementKey(nodePointers[i]);
            }

            foreach (var item in nodePointers)
            {
                resultSeries.Add(item.Value);
            }

            resultSeries.Sort();

            for (int i = 0; i < nodeCount - 2; i++)
            {
                min = tree.ExtractMin();
                Assert.AreEqual(resultSeries[i], min);
            }
        }