コード例 #1
0
        public void TreeInsertsAscending4()
        {
            var value = new object();
            var red   = BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red;
            var black = BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black;

            var tree = new BalancedBoundingTree <double, object>(0, 0.1, value);

            tree.Insert(0.1, 0.2, value);
            tree.Insert(0.2, 0.3, value);
            tree.Insert(0.3, 0.4, value);

            Assert.AreEqual(black, tree.Root.NodeColour);
            Assert.AreEqual(0.1, tree.Root.LowerBound);
            Assert.AreEqual(0.2, tree.Root.UpperBound);

            var one = tree.Root.LowerTree;

            Assert.AreEqual(black, one.NodeColour);
            Assert.AreEqual(0, one.LowerBound);
            Assert.AreEqual(0.1, one.UpperBound);

            var three = tree.Root.UpperTree;

            Assert.AreEqual(black, three.NodeColour);
            Assert.AreEqual(0.2, three.LowerBound);
            Assert.AreEqual(0.3, three.UpperBound);

            var four = three.UpperTree;

            Assert.AreEqual(red, four.NodeColour);
            Assert.AreEqual(0.3, four.LowerBound);
            Assert.AreEqual(0.4, four.UpperBound);
        }
コード例 #2
0
        public static Cdf GenerateFromHistogram(Tuple <double, int>[] graph)
        {
            if (graph.Length == 0)
            {
                throw new ArgumentException("Argument is empty collection", nameof(graph));
            }

            var min = graph.Select(i => i.Item1).Min();

            if (min != 0)
            {
                throw new ArgumentOutOfRangeException(nameof(graph), $"Graph must contain a 0 value as it is of range 0 and 1:{min}");
            }

            var max = graph.Select(i => i.Item1).Max();

            if (max != 1)
            {
                throw new ArgumentOutOfRangeException(nameof(graph), $"Graph must contain a 1 value as it is of range 0 and 1: {max}");
            }

            BalancedBoundingTree <double, int> tree = new BalancedBoundingTree <double, int>(Double.MinValue, graph[0].Item1, graph[0].Item2);

            for (int i = 0; i < graph.Length - 1; i++)
            {
                tree.Insert(graph[i].Item1, graph[i + 1].Item1, graph[i].Item2);
            }

            var maxTuple = graph[graph.Length - 1];

            tree.Insert(maxTuple.Item1, double.MaxValue, maxTuple.Item2);

            return(new Cdf(tree));
        }
コード例 #3
0
        public void MaximumCase()
        {
            var tree = new BalancedBoundingTree <double, object>(100, 200, new object());

            tree.Insert(50, 100, new object());
            tree.Insert(25, 50, new object());
            tree.Insert(250, 300, new object());
            tree.Insert(200, 250, new object());

            Assert.AreEqual(300, tree.Maximum().UpperBound);
        }
コード例 #4
0
        public void MinimumTest()
        {
            var tree = new BalancedBoundingTree <double, object>(100, 200, new object());

            tree.Insert(50, 100, new object());
            tree.Insert(200, 250, new object());
            tree.Insert(25, 50, new object());
            tree.Insert(15, 25, new object());

            Assert.AreEqual(15, tree.Minimum().LowerBound);
        }
コード例 #5
0
        public void SearchTests(double searchValue, int?foundValue)
        {
            var tree = new BalancedBoundingTree <double, int>(0, 0.1, 1);

            tree.Insert(0.1, 0.2, 2);
            tree.Insert(0.2, 0.3, 3);
            tree.Insert(0.3, 0.4, 4);

            if (!foundValue.HasValue)
            {
                Assert.AreSame(BalancedBoundingTree <double, int> .Nil, tree.Search(searchValue));
            }
            else
            {
                var actual = tree.Search(searchValue);
                Assert.AreEqual(foundValue.Value, actual.Value);
            }
        }
コード例 #6
0
        public void AddSmallNodeToTree()
        {
            var ut   = new BalancedBoundingTree <double, object>(100, 200, new object());
            var node = ut.Insert(50, 100, new object());

            Assert.AreEqual(50, ut.Root.LowerTree.LowerBound);
            Assert.AreEqual(100, ut.Root.LowerTree.UpperBound);

            Assert.AreSame(ut.Root, ut.Root.LowerTree.Parent);
        }
コード例 #7
0
        public void AddLargeNodeToTree()
        {
            var ut = new BalancedBoundingTree <double, object>(100, 200, new object());

            ut.Insert(200, 300, new object());

            Assert.AreEqual(200, ut.Root.UpperTree.LowerBound);
            Assert.AreEqual(300, ut.Root.UpperTree.UpperBound);

            Assert.AreSame(ut.Root, ut.Root.UpperTree.Parent);
        }
コード例 #8
0
        public void TreeInsertsAscending2()
        {
            var value = new object();
            var red   = BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Red;
            var black = BalancedBoundingTree <double, object> .BalancedBoundingNode.Colour.Black;

            var tree = new BalancedBoundingTree <double, object>(0, 0.1, value);

            tree.Insert(0.1, 0.2, value);

            Assert.AreEqual(black, tree.Root.NodeColour);
            Assert.AreEqual(0, tree.Root.LowerBound);
            Assert.AreEqual(0.1, tree.Root.UpperBound);

            var two = tree.Root.UpperTree;

            Assert.AreEqual(red, two.NodeColour);
            Assert.AreEqual(0.1, two.LowerBound);
            Assert.AreEqual(0.2, two.UpperBound);
        }
コード例 #9
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);
        }
コード例 #10
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);
        }