public void It_should_return_false_if_BT_is_not_balanced()
 {
     var bt = new BinaryTreeNode(1);
     bt.Left = new BinaryTreeNode(2);
     bt.Left.Left = new BinaryTreeNode(3);
     Obj.IsBalanced(bt).Should().BeFalse();
 }
Ejemplo n.º 2
0
        private int minDepth(BinaryTreeNode root)
        {
            if (root == null)
            {
                return 0;
            }

            return 1 + Math.Min(maxDepth(root.Left), maxDepth(root.Right));
        }
Ejemplo n.º 3
0
 public void BuildTrees()
 {
     T1= new BinaryTreeNode(6);
     T1.Left = new BinaryTreeNode(271);
     T1.Left.Left = new BinaryTreeNode(28);
     T1.Left.Right = new BinaryTreeNode(0);
     T1.Right = new BinaryTreeNode(561);
     T1.Right.Right = new BinaryTreeNode(3);
     T1.Right.Right.Left = new BinaryTreeNode(17);
 }
Ejemplo n.º 4
0
        private bool Check(BinaryTreeNode root, int min, int max)
        {
            if (root == null) {
                return true;
            } else if (root.Value < min || root.Value > max) {
                return false;
            }

            return Check(root.Left, min, root.Value) && Check(root.Right, root.Value, max);
        }
Ejemplo n.º 5
0
 public bool Check(BinaryTreeNode T1, BinaryTreeNode T2)
 {
     if (T2 == null)
     {
         return true;
     }
     else
     {
         return IsSubTree(T1, T2);
     }
 }
Ejemplo n.º 6
0
        public void InOrderTraverseRecursive(BinaryTreeNode root)
        {
            if (root == null) {
                return;
            }

            InOrderTraverseRecursive(root.Left);
            Result += root.Value.ToString();
            Result += ",";
            InOrderTraverseRecursive(root.Right);
        }
 public void It_should_return_true_if_root_is_a_BST()
 {
     var root = new BinaryTreeNode(7);
     root.Left = new BinaryTreeNode(3);
     root.Left.Left = new BinaryTreeNode(2);
     root.Left.Right = new BinaryTreeNode(5);
     root.Right = new BinaryTreeNode(11);
     root.Right.Right = new BinaryTreeNode(17);
     root.Right.Right.Left = new BinaryTreeNode(13);
     Obj.Check(root).Should().BeTrue();
 }
 public void It_should_return_false_if_root_is_not_a_BST()
 {
     var root = new BinaryTreeNode(6);
     root.Left = new BinaryTreeNode(271);
     root.Left.Left = new BinaryTreeNode(28);
     root.Left.Right = new BinaryTreeNode(0);
     root.Right = new BinaryTreeNode(561);
     root.Right.Right = new BinaryTreeNode(3);
     root.Right.Right.Left = new BinaryTreeNode(17);
     Obj.Check(root).Should().BeFalse();
 }
        private BinaryTreeNode BuildBSTFromSortedArrayHelper(int[] a, int start, int end)
        {
            if (start > end) {
                return null;
            }

            var m = start + (end - start) / 2;
            var node = new BinaryTreeNode(a[m]);
            node.Left = BuildBSTFromSortedArrayHelper(a, start, m - 1);
            node.Right = BuildBSTFromSortedArrayHelper(a, m + 1, end);
            return node;
        }
Ejemplo n.º 10
0
        private bool IsSubTree(BinaryTreeNode R1, BinaryTreeNode R2)
        {
            if (R1 == null)
            {
                return false;
            }

            if (R1.Value == R2.Value)
            {
                if (IsMatchTree(R1, R2))
                {
                    return true;
                }
            }

            return (IsSubTree(R1.Left, R2) || IsSubTree(R1.Right, R2));
        }
Ejemplo n.º 11
0
        private bool IsMatchTree(BinaryTreeNode R1, BinaryTreeNode R2)
        {
            if (R2 == null && R1 == null)
            {
                return true; // nothing left in the subtree
            }

            if (R1 == null || R2 == null)
            {
                return false; // big tree empty & subtree still not found
            }

            if (R1.Value != R2.Value)
            {
                return false; // data doesn't match
            }

            return (IsMatchTree(R1.Left, R2.Left) &&
            IsMatchTree(R1.Right, R2.Right));
        }
Ejemplo n.º 12
0
        public void LevelOrderTraverse(BinaryTreeNode root)
        {
            var q = new Queue<BinaryTreeNode>();
            var curr = root;

            q.Enqueue(curr);

            while (q.Any()) {
                var temp = q.Dequeue();

                Result += temp.Value;
                Result += ",";

                if (temp.Left != null) {
                    q.Enqueue(temp.Left);
                }

                if (temp.Right != null) {
                    q.Enqueue(temp.Right);
                }
            }
        }
Ejemplo n.º 13
0
        public void InOrderTraverseIterative(BinaryTreeNode root)
        {
            var s = new Stack<BinaryTreeNode>();
            var curr = root;
            bool done = false;

            while (!done) {
                if (curr != null) {
                    // Go to the left-most leaf
                    s.Push(curr);
                    curr = curr.Left;
                } else {
                    if (!s.Any()) {
                        done = true;
                    } else {
                        // Pop go to the right and do left subtree again
                        curr = s.Pop();
                        Result += curr.Value;
                        Result += ",";
                        curr = curr.Right;
                    }
                }
            }
        }
Ejemplo n.º 14
0
 /// <summary>
 /// no two leaf nodes differ in distance from the root by more than one
 /// </summary>
 /// <returns><c>true</c> if the BT is balanced; otherwise, <c>false</c>.</returns>
 /// <param name="root">Root</param>
 public bool IsBalanced(BinaryTreeNode root)
 {
     return (maxDepth(root) - minDepth(root)) <= 1;
 }
Ejemplo n.º 15
0
 public bool Check(BinaryTreeNode root)
 {
     return Check(root, int.MinValue, int.MaxValue);
 }
Ejemplo n.º 16
0
        private BinaryTreeNode ReconstructBSTFromInOrderPreOrder(
			int[] inorder, int[]preorder, 
			int inorderStart, int inorderEnd, int preIndex)
        {
            if (inorderStart > inorderEnd) {
                return null;
            }

            var root = new BinaryTreeNode(preorder[preIndex++]);

            if (inorderStart == inorderEnd) {
                return root;
            }

            var rootIndex = Array.FindIndex(inorder, x => x == root.Value);

            root.Left = ReconstructBSTFromInOrderPreOrder(
                inorder, preorder,
                inorderStart, rootIndex - 1, preIndex);
            root.Right = ReconstructBSTFromInOrderPreOrder(
                inorder, preorder,
                rootIndex + 1, inorderEnd, preIndex);

            return root;
        }