15 Jan 2015

eKhaliyan

Program to Check given Binary Tree is BST or not.

We should check whether all the left tree data is lesser than the minimum i.e. its parents data and same for the right tree as well.


Int check_bst(btptr t, int min, int max)
{
   if (t!= NULL)
   {
     if (t->left != NULL)
       if (t->data > t->left->data && t->left->data > min  )
        return(check_bst(t->left, min, t->data-1));
        else return 0;

     if (t->right != NULL)
            if (t->data <t->right->data && t->right->data < max )
            return (check_bst(t->right, t->data-1, max));
            else return 0;
   }
}