diff --git a/binarySerchTree in c b/binarySerchTree in c new file mode 100644 index 0000000..a4f14fa --- /dev/null +++ b/binarySerchTree in c @@ -0,0 +1,75 @@ +#include +#include + +struct node{ + int data; + struct node* left; + struct node* right; +}; + +struct node* creatNode(int data){ + struct node *n; //creating a node pointer + n=(struct node *)malloc(sizeof(struct node)); //allocation memory in heap + n->data=data; + n->left=NULL; + n->right=NULL; + return n; +} + +void preOrder(struct node *root){ + if(root!=NULL){ + printf("%d ",root->data); + preOrder(root->left); + preOrder(root->right); + } +} + +void postOrder(struct node *root){ + if(root!=NULL){ + postOrder(root->left); + postOrder(root->right); + printf("%d ",root->data); + } +} + +void inOrder(struct node *root){ + if(root!=NULL){ + inOrder(root->left); + printf("%d ",root->data); + inOrder(root->right); + } +} +int isBST(struct node *root){ + static struct node *prev=NULL; + if(root!=NULL){ + if(!isBST(root->left)){ + return 0; + } + if(prev!=NULL && root->data <= prev->data){ + return 0; + } + prev=root; + return isBST(root->right); + } else { + return 1; + } +} + + +int main(){ + struct node *p=(creatNode(5)); + struct node *p1=(creatNode(3)); + struct node *p2=(creatNode(6)); + struct node *p3=(creatNode(1)); + struct node *p4=(creatNode(4)); + p->left=p1; + p->right=p2; + p1->left=p3; + p1->right=p4; + printf("In order: "); + inOrder(p); + printf("\n"); + printf("%d",isBST(p)); + + return 0; +}