Exemple #1
0
        static int getTreeHeight(Program1.BST tree, int height)
        {
            if (tree == null)
            {
                return(height);
            }
            int leftTreeHeight  = getTreeHeight(tree.left, height + 1);
            int rightTreeHeight = getTreeHeight(tree.right, height + 1);

            return(Math.Max(leftTreeHeight, rightTreeHeight));
        }
Exemple #2
0
 static List <int> inOrderTraverse(Program1.BST tree, List <int> array)
 {
     if (tree.left != null)
     {
         inOrderTraverse(tree.left, array);
     }
     array.Add(tree.value);
     if (tree.right != null)
     {
         inOrderTraverse(tree.right, array);
     }
     return(array);
 }
Exemple #3
0
        //[Test]
        public void TestCase1()
        {
            var root = new Program1.BST(10);

            root.left           = new Program1.BST(5);
            root.left.left      = new Program1.BST(2);
            root.left.left.left = new Program1.BST(1);
            root.left.right     = new Program1.BST(5 - 2);
            root.right          = new Program1.BST(15);
            root.right.right    = new Program1.BST(22);

            //Utils.AssertFalse(Program1.ValidateBst(root));
            Program1.ValidateBst(root);
        }
Exemple #4
0
 static bool validateBst(Program1.BST tree, int minValue, int maxValue)
 {
     if (tree.value < minValue || tree.value >= maxValue)
     {
         return(false);
     }
     if (tree.left != null && !validateBst(tree.left, minValue, tree.value))
     {
         return(false);
     }
     if (tree.right != null && !validateBst(tree.right, tree.value, maxValue))
     {
         return(false);
     }
     return(true);
 }
Exemple #5
0
 static int getTreeHeight(Program1.BST tree)
 {
     return(getTreeHeight(tree, 0));
 }
Exemple #6
0
 static bool validateBst(Program1.BST tree)
 {
     return(validateBst(tree, Int32.MinValue, Int32.MaxValue));
 }