Sunteți pe pagina 1din 5

IMPLEMENTATION OF BINARY SEARCH TREE #include<stdio.h> #include<conio.h> #include<stdlib.

h> int x; struct tnode { struct tnode *left; int data; struct tnode *right; }*t=NULL,*s=NULL,*r=NULL; struct tnode *insert(int x,struct tnode *t) { if(t==NULL) { t=(struct tnode*)malloc(sizeof(struct tnode)); t->left=t->right=NULL; t->data=x; } else { if(x<t->data) t->left=insert(x,t->left); else if(x>t->data) t->right=insert(x,t->right); else printf("\nNode already exists"); } return(t); } int findmax(struct tnode *t) { if(t==NULL) printf("\nTree not exists"); else if(t->right==NULL) return(t->data); else return(findmax(t->right)); } int findmin(struct tnode *t) { if(t==NULL) printf("\nTree not exist");

else if(t->left==NULL) return(t->data); else return(findmin(t->left)); } int find(int x,struct tnode *t) { if(t==NULL) printf("\n Node not exist"); else if(x<t->data) return(find(x,t->left)); else if(x>t->data) return(find(x,t->right)); else printf("\n Node found"); } int del(int x,struct tnode *t) { int y; if(t==NULL) printf("\n Node not exist"); else { if(x<t->data) t->left =del(x,t->left); else if(x>t->data) t->right=del(x,t->right); else { if(t->left && t->right) { y=findmin(t->right); t->data=y; t->right=del(t->data,t->right); } else { r=t; if(t->left==NULL) t=t->right; else { if(t->right==NULL) t=t->left; } }

free(r); printf("\nNode deleted"); } return(t); } } struct tnode *makeempty(struct tnode*t) { if(t!=NULL) { makeempty(t->left); makeempty(t->right); free(t); } printf("\nThe list is made empty."); return NULL; } void inorder(struct tnode *t) { if(t!=NULL) { inorder(t->left); printf("\t%d",t->data); inorder(t->right); } } void preorder (struct tnode *t) { if(t!=NULL) { printf("\t%d",t->data); preorder(t->left); preorder(t->right); } } void postorder(struct tnode *t) { if(t!=NULL) { postorder(t->left); postorder(t->right); printf("\t%d",t->data); } } void main() {

int ch,a; clrscr(); printf("\t\tBINARY SEARCH TREE"); printf("\n1.Insertion\t2.Findmax\t3.Findmin\n4.Find\t\t5.Deletion\t6.Empty\n7.Inorder\t8.Preord er\t9.Postorder "); do { printf("\n Enter the choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter the data:"); scanf("%d",&a); s=insert(a,s); break; case 2: a=findmax(s); printf("\nThe maximum number is:%d",a); break; case 3: a=findmin(s); printf("\nThe minimum number is:%d",a); break; case 4: printf("\n Enter the node to be find:"); scanf("%d",&a); a=find(a,s); break; case 5: printf("\n Enter the node to be delete:"); scanf("%d",&a); s=del(a,s); break; case 6: printf("\n Make empty"); s=makeempty(s); break; case 7: inorder(s); break; case 8: preorder(s); break; case 9: postorder(s);

break; default: printf("\n Invalid choice"); break; } }while(ch>0 && ch<=9); getch(); }

OUTPUT:
BINARY SEARCH TREE 1.Insertion 2.Findmax 3.Findmin 4.Find 5.Deletion 6.Empty 7.Inorder 8.Preorder 9.Postorder Enter the choice:1 Enter the data:45 Enter the choice:1 Enter the data:56 Enter the choice:1 Enter the data:34 Enter the choice:1 Enter the data:2 Enter the choice:1 Enter the data:45 Node already exists Enter the choice:2 The maximum number is:56 Enter the choice:3 The minimum number is:2 Enter the choice:7 2 34 45 56 Enter the choice:8 45 34 2 56 Enter the choice:9 2 34 56 45 Enter the choice:4 Enter the node to be find:3 Node not exist Enter the choice:5 Enter the node to be delete:34 Node deleted Enter the choice:7 2 45 56 Enter the choice:56 Invalid choice.

S-ar putea să vă placă și