Esempio n. 1
0
        public void RotateLeft()
        {
            var value = new object();
            var ut    = new BalancedBoundingTree <double, object>(100, 200, value); //x
            var x     = ut.Root;

            var y = new BalancedBoundingTree <double, object> .BalancedBoundingNode(300, 400, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            ut.Root.UpperTree = y;
            y.Parent          = ut.Root;

            var lambda = new BalancedBoundingTree <double, object> .BalancedBoundingNode(400, 500, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            y.UpperTree   = lambda;
            lambda.Parent = y;

            var beta = new BalancedBoundingTree <double, object> .BalancedBoundingNode(200, 300, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            y.LowerTree = beta;
            beta.Parent = y;

            var alpha = new BalancedBoundingTree <double, object> .BalancedBoundingNode(50, 100, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            x.LowerTree  = alpha;
            alpha.Parent = x;

            // log before
            string directory      = TestGraphDirectory;
            string beforeFileName = @"balancedBoundingTreeTests.RotateLeft.png";

            ut.Write(10, directory, beforeFileName, Log.Logger).Wait();

            ut.LeftRotate(ut.Root);

            //log after
            string afterFileName = @"balancedBoundingTreeTests.RotateLeft.Result.png";

            ut.Write(10, directory, afterFileName, Log.Logger).Wait();

            Assert.AreSame(y, ut.Root);
            Assert.AreSame(BalancedBoundingTree <double, object> .Nil, y.Parent);

            Assert.AreSame(lambda, y.UpperTree);
            Assert.AreSame(y, lambda.Parent);

            Assert.AreSame(x, y.LowerTree);
            Assert.AreSame(y, x.Parent);

            Assert.AreSame(beta, x.UpperTree);
            Assert.AreSame(x, beta.Parent);

            Assert.AreSame(alpha, x.LowerTree);
            Assert.AreSame(x, alpha.Parent);
        }
Esempio n. 2
0
        public void InsertFixupCase2And3ReflectedTest()
        {
            var value = new object();
            var tree  = new BalancedBoundingTree <double, object>(9.5, 10.5, value);
            var c     = tree.Root;

            var red   = BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red;
            var black = BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black;

            var a = new BalancedBoundingTree <double, object> .BalancedBoundingNode(14.5, 15.5, value, red);

            a.Parent    = c;
            c.UpperTree = a;

            var b = new BalancedBoundingTree <double, object> .BalancedBoundingNode(12.5, 13.5, value, red);

            b.Parent    = a;
            a.LowerTree = b;

            var alpha = new BalancedBoundingTree <double, object> .BalancedBoundingNode(16.5, 17.5, value, black);

            alpha.Parent = a;
            a.UpperTree  = alpha;

            var beta = new BalancedBoundingTree <double, object> .BalancedBoundingNode(13.5, 14.5, value, black);

            beta.Parent = b;
            b.UpperTree = beta;

            var lambda = new BalancedBoundingTree <double, object> .BalancedBoundingNode(11.5, 12.5, value, black);

            lambda.Parent = b;
            b.LowerTree   = lambda;

            var theta = new BalancedBoundingTree <double, object> .BalancedBoundingNode(8.5, 9.5, value, black);

            theta.Parent = c;
            c.LowerTree  = theta;

            // log before
            string beforeFileName = @"balancedBoundingTreeTests.InsertFixupCase2And3ReflectTest.png";

            tree.Write(10, TestGraphDirectory, beforeFileName, Log.Logger).Wait();

            tree.FixupNode(b);

            // log after
            string afterFileName = @"balancedBoundingTreeTests.InsertFixupCase2And3ReflectTest.Result.png";

            tree.Write(10, TestGraphDirectory, afterFileName, Log.Logger).Wait();

            Assert.AreSame(b, tree.Root);
            Assert.AreSame(a, b.UpperTree);
            Assert.AreSame(c, b.LowerTree);
            Assert.AreSame(alpha, a.UpperTree);
            Assert.AreSame(beta, a.LowerTree);
            Assert.AreSame(lambda, c.UpperTree);
            Assert.AreSame(theta, c.LowerTree);

            Assert.AreEqual(black, tree.Root.NodeColour);
            Assert.AreEqual(red, a.NodeColour);
            Assert.AreEqual(red, c.NodeColour);
        }
Esempio n. 3
0
        public void RbInsertFixupRightHandVersion()
        {
            var value = new object();
            var tree  = new BalancedBoundingTree <double, object>(2.5, 3.5, value);
            var red   = BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red;
            var black = BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black;
            var three = tree.Root;
            var two   = new BalancedBoundingTree <double, object> .BalancedBoundingNode(1.5, 2.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black);

            var one = new BalancedBoundingTree <double, object> .BalancedBoundingNode(0.5, 1.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            var nine = new BalancedBoundingTree <double, object> .BalancedBoundingNode(8.5, 9.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            var ten = new BalancedBoundingTree <double, object> .BalancedBoundingNode(9.5, 10.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black);

            var eleven = new BalancedBoundingTree <double, object> .BalancedBoundingNode(10.5, 11.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            var fourteen = new BalancedBoundingTree <double, object> .BalancedBoundingNode(13.5, 14.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            var fifteen = new BalancedBoundingTree <double, object> .BalancedBoundingNode(14.5, 15.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black);

            var twelve = new BalancedBoundingTree <double, object> .BalancedBoundingNode(11.5, 12.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);


            tree.Root.LowerTree = two;
            two.Parent          = tree.Root;

            two.LowerTree = one;
            one.Parent    = two;

            tree.Root.UpperTree = fourteen;
            fourteen.Parent     = tree.Root;

            fourteen.UpperTree = fifteen;
            fifteen.Parent     = fourteen;

            fourteen.LowerTree = ten;
            ten.Parent         = fourteen;

            ten.LowerTree = nine;
            nine.Parent   = ten;

            ten.UpperTree = eleven;
            eleven.Parent = ten;

            // log before
            string beforeFileName = @"balancedBoundingTreeTests.RbInsertFixupLeftHandVersion.png";

            tree.Write(10, TestGraphDirectory, beforeFileName, Log.Logger).Wait();

            tree.Insert(twelve);

            // log after
            string afterFileName = @"balancedBoundingTreeTests.RbInsertFixupRightHandVersion.Result.png";

            tree.Write(10, TestGraphDirectory, afterFileName, Log.Logger).Wait();

            Assert.AreSame(ten, tree.Root);
            Assert.AreSame(three, ten.LowerTree);
            Assert.AreSame(ten, three.Parent);

            Assert.AreSame(two, three.LowerTree);
            Assert.AreSame(three, two.Parent);

            Assert.AreSame(one, two.LowerTree);
            Assert.AreSame(two, one.Parent);

            Assert.AreSame(nine, three.UpperTree);
            Assert.AreSame(three, nine.Parent);

            Assert.AreSame(fourteen, ten.UpperTree);
            Assert.AreSame(ten, fourteen.Parent);

            Assert.AreSame(eleven, fourteen.LowerTree);
            Assert.AreSame(fourteen, eleven.Parent);

            Assert.AreSame(fifteen, fourteen.UpperTree);
            Assert.AreSame(fourteen, fifteen.Parent);

            Assert.AreSame(twelve, eleven.UpperTree);
            Assert.AreSame(eleven, twelve.Parent);

            Assert.AreEqual(black, ten.NodeColour);
            Assert.AreEqual(red, three.NodeColour);
            Assert.AreEqual(red, fourteen.NodeColour);
            Assert.AreEqual(black, two.NodeColour);
            Assert.AreEqual(black, nine.NodeColour);
            Assert.AreEqual(black, eleven.NodeColour);
            Assert.AreEqual(black, fifteen.NodeColour);
            Assert.AreEqual(red, one.NodeColour);
            Assert.AreEqual(red, twelve.NodeColour);
        }
Esempio n. 4
0
        public void RbInsertFixupLeftHandVersion()
        {
            var value  = new object();
            var tree   = new BalancedBoundingTree <double, object>(10.5, 11.5, value);
            var red    = BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red;
            var black  = BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black;
            var eleven = tree.Root;
            var two    = new BalancedBoundingTree <double, object> .BalancedBoundingNode(1.5, 2.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            var one = new BalancedBoundingTree <double, object> .BalancedBoundingNode(0.5, 1.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black);

            var seven = new BalancedBoundingTree <double, object> .BalancedBoundingNode(6.5, 7.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black);

            var five = new BalancedBoundingTree <double, object> .BalancedBoundingNode(4.5, 5.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            var eight = new BalancedBoundingTree <double, object> .BalancedBoundingNode(7.5, 8.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            var fourteen = new BalancedBoundingTree <double, object> .BalancedBoundingNode(13.5, 14.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black);

            var fifteen = new BalancedBoundingTree <double, object> .BalancedBoundingNode(14.5, 15.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);

            var four = new BalancedBoundingTree <double, object> .BalancedBoundingNode(3.5, 4.5, value, BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red);


            tree.Root.UpperTree = fourteen;
            fourteen.Parent     = tree.Root;
            fourteen.UpperTree  = fifteen;
            fifteen.Parent      = fourteen;

            tree.Root.LowerTree = two;
            two.Parent          = tree.Root;
            two.LowerTree       = one;
            one.Parent          = two;
            two.UpperTree       = seven;
            seven.Parent        = two;

            seven.LowerTree = five;
            five.Parent     = seven;
            seven.UpperTree = eight;
            eight.Parent    = seven;

            // log before
            string beforeFileName = @"balancedBoundingTreeTests.RbInsertFixupLeftHandVersion.png";

            tree.Write(10, TestGraphDirectory, beforeFileName, Log.Logger).Wait();

            tree.Insert(four);

            // log after
            string afterFileName = @"balancedBoundingTreeTests.RbInsertFixupLeftHandVersion.Result.png";

            tree.Write(10, TestGraphDirectory, afterFileName, Log.Logger).Wait();

            Assert.AreSame(seven, tree.Root);

            Assert.AreSame(eleven, seven.UpperTree);
            Assert.AreSame(seven, eleven.Parent);
            Assert.AreEqual(eleven.NodeColour, red);

            Assert.AreSame(eight, eleven.LowerTree);
            Assert.AreSame(eleven, eight.Parent);
            Assert.AreEqual(black, eight.NodeColour);

            Assert.AreSame(fourteen, eleven.UpperTree);
            Assert.AreSame(eleven, fourteen.Parent);
            Assert.AreEqual(black, fourteen.NodeColour);

            Assert.AreSame(fifteen, fourteen.UpperTree);
            Assert.AreSame(fourteen, fifteen.Parent);
            Assert.AreEqual(red, fifteen.NodeColour);

            Assert.AreSame(two, seven.LowerTree);
            Assert.AreSame(seven, two.Parent);
            Assert.AreEqual(red, two.NodeColour);

            Assert.AreSame(one, two.LowerTree);
            Assert.AreSame(two, one.Parent);
            Assert.AreEqual(black, one.NodeColour);

            Assert.AreSame(five, two.UpperTree);
            Assert.AreEqual(two, five.Parent);
            Assert.AreEqual(black, five.NodeColour);

            Assert.AreSame(four, five.LowerTree);
            Assert.AreSame(five, four.Parent);
            Assert.AreEqual(red, four.NodeColour);
        }