Ejemplo n.º 1
0
        public void MergeTest()
        {
            var heap1 = new[] { "How", "now," }.Aggregate(LeftistHeap <string> .Empty, (h, x) => LeftistHeap <string> .Insert(x, h));
            var heap2 = new[] { "brown", "cow?" }.Aggregate(LeftistHeap <string> .Empty, (h, x) => LeftistHeap <string> .Insert(x, h));
            var heap = LeftistHeap <string> .Merge(heap1, heap2);

            Assert.AreEqual("cow?, brown, now,, How, ", DumpHeap(heap));
            Assert.AreEqual("brown", LeftistHeap <string> .FindMin(heap));
        }
Ejemplo n.º 2
0
        public void DelMinTest()
        {
            var heap = new[] { 3, 2, 5, 1 }.Aggregate(LeftistHeap <int> .Empty, (h, x) => LeftistHeap <int> .Insert(x, h));

            heap = LeftistHeap <int> .DeleteMin(heap);

            Assert.AreEqual("3, 2, 5, ", DumpHeap(heap));
            Assert.AreEqual(2, LeftistHeap <int> .FindMin(heap));
        }
Ejemplo n.º 3
0
        public void InsertZeroTest()
        {
            var heap = new[] { 3, 2, 5, 1 }.Aggregate(LeftistHeap <int> .Empty, (h, x) => LeftistHeap <int> .Insert(x, h));

            heap = LeftistHeap <int> .Insert(0, heap);

            Assert.AreEqual("3, 2, 5, 1, 0, ", DumpHeap(heap));
            Assert.AreEqual(0, LeftistHeap <int> .FindMin(heap));
        }
Ejemplo n.º 4
0
        public void SingleMinTest()
        {
            var heap = LeftistHeap <int> .Empty;

            heap = LeftistHeap <int> .Insert(2, heap);

            var x = LeftistHeap <int> .FindMin(heap);

            Assert.AreEqual(2, x);
        }
Ejemplo n.º 5
0
        public void DeleteLotsOfMinsTest()
        {
            var random = new Random(3456);
            var heap   = LeftistHeap <int> .Empty;

            for (var i = 0; i < 100; i++)
            {
                heap = LeftistHeap <int> .Insert(random.Next(100), heap);
            }
            var last  = 0;
            var count = 0;

            while (!LeftistHeap <int> .IsEmpty(heap))
            {
                var next = LeftistHeap <int> .FindMin(heap);

                heap = LeftistHeap <int> .DeleteMin(heap);

                Assert.IsTrue(last <= next);
                last = next;
                count++;
            }
            Assert.AreEqual(100, count);
        }
Ejemplo n.º 6
0
        public void EmptyMinTest()
        {
            var heap = LeftistHeap <int> .Empty;

            AssertThrows <ArgumentNullException>(() => LeftistHeap <int> .FindMin(heap));
        }
Ejemplo n.º 7
0
        public void MinTreeTest()
        {
            var heap = new[] { 3, 2, 5, 1 }.Aggregate(LeftistHeap <int> .Empty, (h, x) => LeftistHeap <int> .Insert(x, h));

            Assert.AreEqual(1, LeftistHeap <int> .FindMin(heap));
        }