Example #1
0
        public void TestIsBalancedFalse()
        {
            var bst  = new BalancedBST();
            var root = new BSTNode(9, null);
            var four = new BSTNode(4, root);

            root.LeftChild = four;
            var seventeen = new BSTNode(17, root);

            root.RightChild = seventeen;
            var three = new BSTNode(3, four);

            four.LeftChild = three;
            var six = new BSTNode(6, four);

            four.RightChild = six;
            var five = new BSTNode(5, six);

            six.LeftChild = five;
            var seven = new BSTNode(7, six);

            six.RightChild = seven;
            var twentytwo = new BSTNode(22, seventeen);

            seventeen.RightChild = twentytwo;
            var twenty = new BSTNode(20, twentytwo);

            twentytwo.LeftChild = twenty;
            Assert.IsFalse(bst.IsBalanced(root));
        }
        public void Test_TreeGenerates_1()
        {
            BalancedBST bbst = new BalancedBST();

            int[] ar = new int[] { 16, 23, 91, 0, -3, 5, 11 };
            bbst.GenerateTree(ar);

            Assert.AreEqual(true, bbst.IsBalanced(bbst.Root));

            /*
             *      -3
             *    -5
             */
            BSTNode node_1 = bbst.Root.LeftChild.LeftChild;

            node_1.LeftChild       = new BSTNode(-5, node_1);
            node_1.LeftChild.Level = node_1.Level + 1;

            Assert.AreEqual(true, bbst.IsBalanced(node_1.Parent));


            /*          0    23
             *       -3  5     91
             *     -5        56   100
             *                66
             */
            BSTNode node_2 = bbst.Root.RightChild.RightChild;

            node_2.LeftChild                  = new BSTNode(56, node_2);
            node_2.RightChild                 = new BSTNode(100, node_2);
            node_2.LeftChild.Level            = node_2.Level + 1;
            node_2.RightChild.Level           = node_2.Level + 1;
            node_2.LeftChild.RightChild       = new BSTNode(66, node_2.LeftChild);
            node_2.LeftChild.RightChild.Level = node_2.Level + 2;

            Assert.AreEqual(false, bbst.IsBalanced(bbst.Root));

            /*          0         23
             *      -3    5     16    91
             *   -5 -1  3  7   8  18 56   100
             *                        66
             */
            node_1.RightChild       = new BSTNode(-1, node_1);
            node_1.RightChild.Level = node_1.Level + 1;
            BSTNode node_3 = node_1.Parent.RightChild;

            node_3.LeftChild        = new BSTNode(3, node_3);
            node_3.RightChild       = new BSTNode(7, node_3);
            node_3.RightChild.Level = node_3.Level + 1;
            node_3.LeftChild.Level  = node_3.Level + 1;

            BSTNode node_4 = bbst.Root.RightChild.LeftChild;

            node_4.RightChild       = new BSTNode(18, node_4);
            node_4.LeftChild        = new BSTNode(8, node_4);
            node_4.LeftChild.Level  = node_4.Level + 1;
            node_4.RightChild.Level = node_4.Level + 1;

            Assert.AreEqual(true, bbst.IsBalanced(bbst.Root));
        }
Example #3
0
 public void TestMethod_3()
 {
     int[] a = new int[] { -93, 54, 3 };
     int[] b = BalancedBST.GenerateBBSTArray(a);
     ElementByIndex(b, 0);
     Assert.AreEqual(3, b.Length);
 }
Example #4
0
 public void TestMethod_1()
 {
     int[] a = new int[] { 16, 8, 19, 6, 10, 17, 22, 5, 7, 9, 11, 16, 18, 21, 23 };
     int[] b = BalancedBST.GenerateBBSTArray(a);
     ElementByIndex(b, 0);
     Assert.AreEqual(15, b.Length);
 }
Example #5
0
 public void TestMethod_2()
 {
     int[] a = new int[] { 31, 22, 11, 1, 52, 55, 32 };
     int[] b = BalancedBST.GenerateBBSTArray(a);
     ElementByIndex(b, 0);
     Assert.AreEqual(7, b.Length);
 }
        public void IsBalanced_If_Tree_Has_Root_Only()
        {
            BalancedBST bsTree = new BalancedBST();

            bsTree.Root       = new BSTNode(8, null);
            bsTree.Root.Level = 1;

            Assert.IsTrue(bsTree.IsBalanced(bsTree.Root));
        }
        public void Test_TreeGenerates_3()
        {
            BalancedBST bbst = new BalancedBST();

            int[] ar = new int[] { 16, 91, -10 };
            bbst.GenerateTree(ar);
            Assert.AreEqual(true, bbst.IsBalanced(bbst.Root));

            bbst.Root.LeftChild.RightChild       = new BSTNode(-6, bbst.Root.LeftChild);
            bbst.Root.LeftChild.RightChild.Level = bbst.Root.LeftChild.Level + 1;
            Assert.AreEqual(true, bbst.IsBalanced(bbst.Root));
        }
Example #8
0
 public void TestMethod_4()
 {
     int[] a = new int[] { 16, 8, 19, 6,
                           10, 17, 22, 5,
                           7, 9, 11, 2,
                           18, 21, 23, 54,
                           36, 31, 4, 25,
                           71, 1, 0, 83,
                           39, 74, 57, 30,
                           28, 84, 55 };
     int[] b = BalancedBST.GenerateBBSTArray(a);
     ElementByIndex(b, 0);
     Assert.AreEqual(31, b.Length);
 }
Example #9
0
        public void TestCreateFromArray()
        {
            var arr = new int[] { 10, 12, 8, 14, 7, 6, 15, 13, 11, 1, 9, 2, 3, 4, 5 };
            var bst = new BalancedBST();

            bst.CreateFromArray(arr);
            var ethalon = new int[] { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };

            Assert.AreEqual(ethalon.Length, bst.BSTArray.Length);
            for (var i = 0; i < bst.BSTArray.Length; i++)
            {
                Assert.AreEqual(ethalon[i], bst.BSTArray[i]);
            }
        }
Example #10
0
        public void TestIsBalanced()
        {
            var arr = new int[] { 10, 12, 8, 14, 7, 6, 15, 13, 11, 1, 9, 2, 3, 4, 5 };
            var bst = new BalancedBST();

            bst.CreateFromArray(arr);
            var ethalon = new int[] { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };

            Assert.AreEqual(ethalon.Length, bst.BSTArray.Length);
            for (var i = 0; i < bst.BSTArray.Length; i++)
            {
                Assert.AreEqual(ethalon[i], bst.BSTArray[i]);
            }
            bst.GenerateTree();
            CheckNodes(bst.Root);
            Assert.IsTrue(bst.IsBalanced(bst.Root));
        }
        public void IsBalanced_if_3_level_on_Left_SubTree_and_Right_SubTree()
        {
            BalancedBST bsTree = new BalancedBST();

            bsTree.Root       = new BSTNode(8, null);
            bsTree.Root.Level = 1;

            bsTree.Root.LeftChild        = new BSTNode(4, bsTree.Root);  // левый потомок корня
            bsTree.Root.LeftChild.Level  = 2;
            bsTree.Root.RightChild       = new BSTNode(12, bsTree.Root); // правый потомок корня
            bsTree.Root.RightChild.Level = 2;

            bsTree.Root.LeftChild.LeftChild         = new BSTNode(2, bsTree.Root.LeftChild);  // узел 3 уровня
            bsTree.Root.LeftChild.LeftChild.Level   = 3;
            bsTree.Root.RightChild.RightChild       = new BSTNode(13, bsTree.Root.LeftChild); // узел 3 уровня
            bsTree.Root.RightChild.RightChild.Level = 3;

            Assert.IsTrue(bsTree.IsBalanced(bsTree.Root));
        }
        public void IsBalanced_if_4_level_on_Left_SubTree_2_Level_on_Right_SubTree()
        {
            BalancedBST bsTree = new BalancedBST();

            bsTree.Root       = new BSTNode(8, null);
            bsTree.Root.Level = 1;

            bsTree.Root.LeftChild        = new BSTNode(4, bsTree.Root); // потомки корня
            bsTree.Root.LeftChild.Level  = 2;
            bsTree.Root.RightChild       = new BSTNode(12, bsTree.Root);
            bsTree.Root.RightChild.Level = 2;

            bsTree.Root.LeftChild.LeftChild       = new BSTNode(2, bsTree.Root.LeftChild); // узел 3 уровня
            bsTree.Root.LeftChild.LeftChild.Level = 3;

            bsTree.Root.LeftChild.LeftChild.RightChild       = new BSTNode(3, bsTree.Root.LeftChild.LeftChild); // узел 4 уровня
            bsTree.Root.LeftChild.LeftChild.RightChild.Level = 4;

            Assert.IsFalse(bsTree.IsBalanced(bsTree.Root));
        }
        static void Main(string[] args)
        {
            BalancedBST bsTree = new BalancedBST();

            bsTree.CreateFromArray(new int[] { 15, 14, 13, 12, 11, 10, 9, 8,
                                               7, 6, 5, 4, 3, 2, 1 });

            Console.WriteLine("массив в формате BST:");
            foreach (var item in bsTree.BSTArray)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            bsTree.GenerateTree();
            Console.WriteLine("Узлы сформированного BST:");
            bsTree.PrintNodes(bsTree.WideAllNodes());
            //bsTree.PrintNodes(bsTree.DeepAllNodes(0));

            Console.WriteLine(bsTree.IsBalanced(bsTree.Root));
            Console.ReadKey();
        }
Example #14
0
        public void IsBalancedTest()
        {
            BalancedBST b1 = new BalancedBST();
            BSTNode     a1 = new BSTNode(6, null);

            a1.Level = 1;
            if (!b1.IsBalanced(a1))
            {
                Assert.Fail();
            }

            a1.LeftChild       = new BSTNode(3, a1);
            a1.LeftChild.Level = a1.Level + 1;
            if (!b1.IsBalanced(a1))
            {
                Assert.Fail();
            }

            a1.LeftChild.LeftChild       = new BSTNode(2, a1.LeftChild);
            a1.LeftChild.LeftChild.Level = a1.Level + 2;
            if (b1.IsBalanced(a1))
            {
                Assert.Fail();
            }

            a1.RightChild       = new BSTNode(7, a1);
            a1.RightChild.Level = a1.Level + 1;
            if (!b1.IsBalanced(a1))
            {
                Assert.Fail();
            }

            a1.RightChild.LeftChild       = new BSTNode(10, a1.RightChild);
            a1.RightChild.LeftChild.Level = 3;
            if (b1.IsBalanced(a1))
            {
                Assert.Fail();
            }
        }
        static void Main(string[] args)
        {
            int[] result = BalancedBST.GenerateBBSTArray(new int[] {
                31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
                20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
                9, 8, 7, 6, 5, 4, 3, 2, 1
            });
            PrintArray(result);

            result = BalancedBST.GenerateBBSTArray(new int[] {
                15, 14, 13, 12, 11, 10, 9, 8,
                7, 6, 5, 4, 3, 2, 1
            });
            PrintArray(result);

            result = BalancedBST.GenerateBBSTArray(new int[] { 7, 6, 5, 4, 3, 2, 1 });
            PrintArray(result);

            result = BalancedBST.GenerateBBSTArray(new int[] { 3, 2, 1 });
            PrintArray(result);

            Console.ReadKey();
        }
Example #16
0
 public void TestMethod_6()
 {
     int[] a = new int[] { 1, 5, 13, 6, 3 };
     int[] b = BalancedBST.GenerateBBSTArray(a);
     Assert.AreEqual(null, b);
 }
Example #17
0
 public void TestMethod_5()
 {
     int[] a = null;
     int[] b = BalancedBST.GenerateBBSTArray(a);
     Assert.AreEqual(null, b);
 }