Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions binarySerchTree in c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <stdio.h>
#include <stdlib.h>

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;
}