Exemple #1
0
        public void PairingMinHeap_Test()
        {
            int nodeCount = 1000 * 10;
            //insert test
            var tree = new PairingMinHeap <int>();

            for (int i = 0; i <= nodeCount; i++)
            {
                tree.Insert(i);
            }

            for (int i = 0; i <= nodeCount; i++)
            {
                tree.DecrementKey(i, i - 1);
            }

            int min = 0;

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

            //IEnumerable tests.
            Assert.AreEqual(tree.Count, tree.Count());

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

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

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

            testSeries.Sort();

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

            //IEnumerable tests.
            Assert.AreEqual(tree.Count, tree.Count());
        }
Exemple #2
0
        public void PairingMinHeap_Test()
        {
            int nodeCount = 1000 * 10;
            //insert test
            var tree = new PairingMinHeap <int>();

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

            for (int i = 0; i <= nodeCount; i++)
            {
                var node = tree.Insert(i);
                nodePointers.Add(node);
            }

            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);
            }
        }