Exemplo n.º 1
0
        public void BinaryTreeMerge_random_Test()
        {
            var list       = new List <int>();
            var rnd        = new Random();
            var firstTree  = new RedBlackBinaryTree <int>();
            var secondTree = new RedBlackBinaryTree <int>();

            for (var i = 0; i < 500; i++)
            {
                var num = rnd.Next(-50, 50);
                firstTree.Add(num);
                list.Add(num);
            }

            for (var i = 500; i < 1000; i++)
            {
                var num = rnd.Next(-50, 50);
                firstTree.Add(num);
                list.Add(num);
            }

            firstTree.MergeWith(secondTree);

            list = list.Distinct()
                   .ToList();

            list.Sort();

            CollectionAssert.AreEqual(list,
                                      firstTree.Inorder()
                                      .ToArray());
        }
Exemplo n.º 2
0
        public void BinaryTreeMergeTime_1000_Test()
        {
            var list      = new List <int>();
            var firstTree = new RedBlackBinaryTree <int>();

            for (var i = 0; i < 500; i++)
            {
                firstTree.Add(i);
                list.Add(i);
            }

            var secondTree = new RedBlackBinaryTree <int>();

            for (var i = 500; i < 1000; i++)
            {
                firstTree.Add(i);
                list.Add(i);
            }

            firstTree.MergeWith(secondTree);

            CollectionAssert.AreEqual(list,
                                      firstTree.Inorder()
                                      .ToArray());
        }
Exemplo n.º 3
0
        public void BinaryTree_RemoveTest()
        {
            var tree = new RedBlackBinaryTree <int>();

            for (var i = 0; i < 10; i++)
            {
                tree.Add(i);
            }

            tree.Remove(5);

            CollectionAssert.AreEqual(tree.Inorder()
                                      .ToArray(),
                                      new[]
            {
                0,
                1,
                2,
                3,
                4,
                6,
                7,
                8,
                9
            });
        }
Exemplo n.º 4
0
        public void BinaryTree_AddTest()
        {
            var tree = new RedBlackBinaryTree <int>();

            for (var i = 0; i < 10; i++)
            {
                tree.Add(i);
            }

            Assert.IsTrue(tree.Count == 10);
            var array = new int[10];

            tree.CopyTo(array, 0);

            CollectionAssert.AreEqual(array,
                                      new[]
            {
                0,
                1,
                2,
                3,
                4,
                5,
                6,
                7,
                8,
                9
            });
        }
Exemplo n.º 5
0
        public void BinaryTree_Postorder_test()
        {
            var tree = new RedBlackBinaryTree <int>(6,
                                                    55,
                                                    -55,
                                                    -40,
                                                    8,
                                                    66,
                                                    554,
                                                    74,
                                                    12,
                                                    7);

            var ordered = new[]
            {
                -40,
                -55,
                7,
                12,
                8,
                66,
                554,
                74,
                55,
                6
            };

            var postorder = tree.Postorder()
                            .ToArray();

            CollectionAssert.AreEqual(ordered, postorder);
        }
Exemplo n.º 6
0
        public void Remove_RedBlackTree()
        {
            using var tree = new RedBlackBinaryTree <int>
                  {
                      1,
                      2
                  };

            tree.Remove(1);
        }
Exemplo n.º 7
0
        public void BinaryTreeMax_test()
        {
            var rnd = new Random();

            var listOfNums = Enumerable.Range(0, 1000)
                             .Select(x => rnd.Next(-10_000, 10_000))
                             .ToArray();

            var tree = new RedBlackBinaryTree <int>();

            tree.AddRange(listOfNums.ToArray());
            var expected = listOfNums.Max();
            var actual   = tree.Max();

            Assert.IsTrue(expected == actual);
        }
Exemplo n.º 8
0
        public void BinaryTreeMergeTest()
        {
            var firstTree = new RedBlackBinaryTree <int>();

            for (var i = 0; i < 10; i++)
            {
                firstTree.Add(i);
            }

            var secondTree = new RedBlackBinaryTree <int>();

            for (var i = 10; i < 20; i++)
            {
                firstTree.Add(i);
            }

            firstTree.MergeWith(secondTree);

            var checkArray = new[]
            {
                0,
                1,
                2,
                3,
                4,
                5,
                6,
                7,
                8,
                9,
                10,
                11,
                12,
                13,
                14,
                15,
                16,
                17,
                18,
                19
            };

            CollectionAssert.AreEqual(checkArray,
                                      firstTree.Inorder()
                                      .ToArray());
        }
Exemplo n.º 9
0
        public void BinaryTree_Inorder_10000_test()
        {
            var rnd = new Random();

            var numbers = Enumerable.Range(0, 10000)
                          .Select(x => rnd.Next(-30, 30))
                          .Distinct()
                          .ToArray();

            var tree          = new RedBlackBinaryTree <int>(numbers);
            var sortedNumbers = numbers.ToList();

            sortedNumbers.Sort();

            var inorder = tree.Inorder()
                          .ToArray();

            CollectionAssert.AreEqual(inorder, sortedNumbers);
        }
Exemplo n.º 10
0
        public void BinaryTree_Contains_positive()
        {
            var tree = new RedBlackBinaryTree <int>();

            tree.AddRange(new[]
            {
                1,
                2,
                3,
                4,
                5,
                6,
                -50,
                -99
            });

            var actual = tree.Contains(6);

            Assert.AreEqual(true, actual);
        }
        public void AddRange_RedBlackTree()
        {
            var tree = new RedBlackBinaryTree <int>();

            tree.AddRange(GetRandomItems());
        }