Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Friday, February 11, 2011

# Check whether a binary tree is a Binary search tree

If the given binary tree is a Binary search tree,then the inorder traversal should output the elements in increasing order.We make use of this property of inorder traversal to check whether the given binary tree is a BST or not.We make note of the latest element that could have been printed and compare it with the current element.Given below is a C function to check it.


bool flag=true;
void inorder(tree T,int *lastprinted)
{
if(T==NULL)
  {
   printf("the tree is empty .Hence, it is a BST\n");
  }
else
{
  if(T->left!=NULL)
  {
      inorder(T->left,lastprinted);
  }
  if(T->data > *lastprinted)
  {
     *lastprinted=T->data;
  }
  else
  {
     printf("the given binary tree is not a BST\n");
     flag=false;
     exit(0);
  }
  inorder(T->right,lastprinted);
  }
}


♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥
http://placementsindia.blogspot.com/2007/12/c-program-to-check-whether-binary-tree.html
http://crackinterviewtoday.wordpress.com/2010/03/12/check-whether-given-binary-tree-is-a-bst-or-not/
http://tarunsays.wordpress.com/2010/01/19/binary-search-tree-bst/

No comments:

Post a Comment