How to check a tree is a binary tree of not

This is continuation of my post How to create binary tree in c#.In this post I will show you how to check a given tree is a binary tree or not.

  public bool isBST()
            return (IsBST(_root));
private bool IsBST(Node node)
            if (node == null) return (true);

            // do the subtrees contain values that do not 
            // agree with the node? 
            if (node.Left != null && FindMaxValue(node.Left) > node.Data) return (false);
            if (node.Right != null && FindMinValue(node.Right) <= node.Data) return (false);

            // check that the subtrees themselves are ok 
            return (IsBST(node.Left) && IsBST(node.Right));

No comments:

Post a Comment