// assign 1 /*Implement a Binary search tree (BST) library (btree.
h) with
operations – create, search,
//insert, inorder, preorder and postorder. Write a menu driven program
that performs the above
//operations.*/
#include<stdio.h>
#include<stdlib.h>
Typedef struct BSTnode
Int data;
Struct BSTnode *left,*right;
}BSTnode;
BSTnode *search(BSTnode *,int);
BSTnode *insert(BSTnode *,int);
BSTnode *create();
Void inorder(BSTnode *T);
Void preorder(BSTnode *T);
Void postorder(BSTnode *T);
Void main()
BSTnode *root=NULL,*p;
Int x,op;
Do
{ printf(“\n\n1)Create\n2)Insert\n3)Search\
n4)Preorder(recursive)”);
Printf(“\n5)Inorder(recursive)\n6)Posorder(Recursive)”);
Printf(“\n7)Quit”);
Printf(“\nEnter Your Choice :”);
Scanf(“%d”,&op);
Switch(op)
Case 1: root=create();break;
Case 2: printf(“\nEnter the key to be inserted :”);
Scanf(“%d”,&x);
Root=insert(root,x);
Break;
Case 3:printf(“\nEnter the key to be searched :”);
Scanf(“%d”,&x);
P=search(root,x);
If(p==NULL)
Printf(“\n ***** Not Found****”);
Else
Printf(“\n ***** Found*****”);
Break;
Case 4: preorder(root);break;
Case 5: inorder(root);break;
Case 6: postorder(root);break;
}while(op!=7);
Void inorder(BSTnode *T)
If(T!=NULL)
{
Inorder(T->left);
Printf(“%d\t”,T->data);
Inorder(T->right);
Void preorder(BSTnode *T)
{ if(T!=NULL)
{ printf(“%d\t”,T->data);
Preorder(T->left);
Preorder(T->right);
Void postorder(BSTnode *T)
{ if(T!=NULL)
Postorder(T->left);
Postorder(T->right);
Printf(“%d\t”,T->data);
BSTnode *search(BSTnode *root,int x)
While(root!=NULL)
If(x==root->data)
Return(root);
If(x>root->data)
Root=root->right;
Else
Root=root->left;
Return(NULL);
BSTnode *insert(BSTnode *T,int x)
BSTnode *r;
// acquire memory for the new node
If(T==NULL)
R=(BSTnode*)malloc(sizeof(BSTnode));
r->data=x;
r->left=NULL;
r->right=NULL;
return(r);
If(x>T->data)
T->right=insert(T->right,x);
Return(T);
Else
If(x<T->data)
{
T->left=insert(T->left,x);
Return(T);
Else //duplicate data
Return(T);
BSTnode *create()
Int n,x,i;
BSTnode *root;
Root=NULL;
Printf(“\nEnter no. Of nodes :”);
Scanf(“%d”,&n);
Printf(“\nEnter tree values :”);
For(i=0;i<n;i++)
Scanf(“%d”,&x);
Root=insert(root,x);
Return(root);