Esempio n. 1
0
        public void TestInsertAtBack()
        {
            var tree = new RBTree <char>();
            var last = tree.InsertSuccessor(null, 'L');

            last = tree.InsertSuccessor(last, 'o');
            last = tree.InsertSuccessor(last, 'g');
            last = tree.InsertSuccessor(last, 'a');
            last = tree.InsertSuccessor(last, 'n');
            last = tree.InsertSuccessor(last, '!');
            Assert.AreEqual('o', tree.Root.Data);
            Assert.AreEqual('L', tree.Root.Left.Data);
            Assert.AreEqual('a', tree.Root.Right.Data);
            Assert.AreEqual('g', tree.Root.Right.Left.Data);
            Assert.AreEqual('n', tree.Root.Right.Right.Data);
            Assert.AreEqual('!', tree.Root.Right.Right.Right.Data);
            var traverse = RBTree <char> .GetFirst(tree.Root);

            Assert.AreEqual('L', traverse.Data);
            traverse = traverse.Next;
            Assert.AreEqual('o', traverse.Data);
            traverse = traverse.Next;
            Assert.AreEqual('g', traverse.Data);
            traverse = traverse.Next;
            Assert.AreEqual('a', traverse.Data);
            traverse = traverse.Next;
            Assert.AreEqual('n', traverse.Data);
            traverse = traverse.Next;
            Assert.AreEqual('!', traverse.Data);
            traverse = traverse.Next;
            Assert.AreEqual(null, traverse);
        }
Esempio n. 2
0
        public void TestRemove()
        {
            var tree = new RBTree <int>();

            for (var i = 0; i < 500; i++)
            {
                tree.InsertSuccessor(RBTree <int> .GetLast(tree.Root), i);
                for (var j = 0; j <= i; j++)
                {
                    var traverse = RBTree <int> .GetFirst(tree.Root);

                    for (var k = 0; k < j; k++)
                    {
                        traverse = traverse.Next;
                    }
                    //remove jth element
                    tree.RemoveNode(traverse);
                    var check = RBTree <int> .GetFirst(tree.Root);

                    for (var k = 0; k < j; k++)
                    {
                        Assert.AreEqual(k, check.Data);
                        check = check.Next;
                    }
                    for (var k = j; k < i; k++)
                    {
                        Assert.AreEqual(k + 1, check.Data);
                        check = check.Next;
                    }
                    //readd
                    tree.InsertSuccessor(traverse.Previous, traverse.Data);
                }
            }
        }
Esempio n. 3
0
        public void TestInsertInMiddle()
        {
            var tree  = new RBTree <int>();
            var first = tree.InsertSuccessor(null, 1);

            tree.InsertSuccessor(first, -1);
            tree.InsertSuccessor(first, 2);
            Assert.AreEqual(2, tree.Root.Data);
            Assert.AreEqual(1, tree.Root.Left.Data);
            Assert.AreEqual(-1, tree.Root.Right.Data);
            first = RBTree <int> .GetFirst(tree.Root);

            Assert.AreEqual(1, first.Data);
            first = first.Next;
            Assert.AreEqual(2, first.Data);
            first = first.Next;
            Assert.AreEqual(-1, first.Data);
            first = first.Next;
            Assert.AreEqual(null, first);
        }
Esempio n. 4
0
        public void TestInsertAtFront()
        {
            var tree = new RBTree <int>();

            tree.InsertSuccessor(null, 4);
            tree.InsertSuccessor(null, 5);
            tree.InsertSuccessor(null, 3);
            tree.InsertSuccessor(null, 4);
            Assert.AreEqual(5, tree.Root.Data);
            Assert.AreEqual(4, tree.Root.Right.Data);
            Assert.AreEqual(3, tree.Root.Left.Data);
            Assert.AreEqual(4, tree.Root.Left.Left.Data);
            var traverse = RBTree <int> .GetFirst(tree.Root);

            Assert.AreEqual(4, traverse.Data);
            traverse = traverse.Next;
            Assert.AreEqual(3, traverse.Data);
            traverse = traverse.Next;
            Assert.AreEqual(5, traverse.Data);
            traverse = traverse.Next;
            Assert.AreEqual(4, traverse.Data);
            traverse = traverse.Next;
            Assert.AreEqual(null, traverse);
        }