Vous êtes sur la page 1sur 6

#include<stdio.h> #include<conio.

h> struct bst { int data; struct bst *left; struct bst *right; }; void search(struct bst*,int,struct bst**,struct bst**); void insert(struct bst**,int); void displaypre(struct bst*); void displaypost(struct bst*); void displayin(struct bst*); void traversal(struct bst *); void deletebst(struct bst**,int); void main() { struct bst *root=NULL,*loc=NULL,*parloc=NULL; int item,i,j,l; char ch='y',ch1='y'; do { printf("\n1.Creation"); printf("\n2.Insertion"); printf("\n3.Deletion"); printf("\n4.Search"); printf("\n5.Traversal"); fflush(stdin); printf("\nEnter choice: "); scanf("%d",&i); switch(i) { case 1:do { printf("\nEnter the element of bst: \n"); scanf("%d",&item); insert(&root,item); printf("\nDo u want to enter more elements\n"); fflush(stdin); scanf("%c",&ch); }while((ch=='y'||ch=='Y')); traversal(root); break; case 2: printf("\nEnter the element of bst: \n"); scanf("%d",&item); insert(&root,item); traversal(root);

break; case 3: printf("\nEnter the element to be deleted: \n"); scanf("%d",&item); deletebst(&root,item); printf("\nThe tree after deletion is\n"); traversal(root); break; case 4: if(root!=NULL) { printf("\nEnter the element to be searched: \n"); scanf("%d",&item); loc=NULL; parloc=NULL ; search(root,item,&loc,&parloc); if(loc==NULL) printf("\nElement not found"); else if(loc!=NULL && parloc==NULL) printf("\nElement is the root element"); else printf("\nthe parent is %d",parloc->data); } else printf("\ntree is empty"); break; case 5: traversal(root); break; default:printf("\nwrong choice"); } fflush(stdin); printf("\ndo u want to continue??\n"); scanf("%c",&ch1); }while(ch1=='y'||ch1=='Y'); } void search(struct bst *root,int item,struct bst **loc,struct bst **parloc) { struct bst *par=NULL,*curr=NULL; if((root)==NULL) return; if((root)->data==item) { (*loc)=(root); return; } curr=(root); while(curr!=NULL && curr->data!=item) { par=curr; if(curr->data<item) curr=curr->right;

else if(curr->data>item) curr=curr->left; } if(curr==NULL) { (*parloc)=par; return; } else { (*parloc)=par; (*loc)=curr; return; } } void insert(struct bst **root,int item) { struct bst *p=NULL,*loc=NULL,*parloc=NULL; p=(struct bst*)malloc(sizeof(struct bst)); p->data=item; p->left=NULL; p->right=NULL; if((*root)==NULL) { (*root)=p; } search((*root),item,&loc,&parloc); if(parloc->data<item) parloc->right=p; else parloc->left=p; } void displaypre(struct bst *root) { struct bst *temp=root; printf(" %d ",temp->data); if(temp==NULL) return; if(temp->left!=NULL) displaypre(temp->left); if(temp->right!=NULL) displaypre(temp->right); } void displayin(struct bst *root) { struct bst *temp=root; if(temp==NULL)

return; if(temp->left!=NULL) displayin(temp->left); printf(" %d ",temp->data); if(temp->right!=NULL) displayin(temp->right); } void displaypost(struct bst *root) { struct bst *temp=root; if(temp==NULL) return; if(temp->left!=NULL) displaypost(temp->left); if(temp->right!=NULL) displaypost(temp->right); printf(" %d ",temp->data); } void deletebst(struct bst**root,int item) { struct bst *loc=NULL,*parloc=NULL,*child=NULL,*ptr=NULL; search((*root),item,&loc,&parloc); if(loc->left==NULL && loc->right==NULL) { if(parloc==NULL) *root=NULL; else if(parloc->data>loc->data) parloc->left=NULL; else parloc->right=NULL; free(loc); //printf("\nend of c1"); } else if((loc->left==NULL && loc->right!=NULL)||(loc->left!=NULL && loc>right==NULL)) { if(loc->left==NULL && loc->right!=NULL) child=loc->right; else if(loc->left!=NULL && loc->right==NULL) child=loc->left; if(parloc==NULL) *root=child; else { if(parloc->left==loc) parloc->left=child;

else parloc->right=child; } } else if(loc->left!=NULL && loc->right!=NULL) { parloc=loc; ptr=loc->right; while(ptr->left!=NULL) { parloc=ptr; ptr=ptr->left; } loc->data=ptr->data; if(ptr->left==NULL && ptr->right==NULL) { if(parloc==NULL) *root=NULL; else if(parloc->data>ptr->data) parloc->left=NULL; else parloc->right=NULL; free(ptr); } else if((ptr->left==NULL && ptr->right!=NULL)||(ptr->left!=NULL && ptr>right==NULL)) { if(ptr->left==NULL && ptr->right!=NULL) child=ptr->right; else if(ptr->left!=NULL && ptr->right==NULL) child=ptr->left; if(parloc==NULL) *root=child; else { if(parloc->left==ptr) parloc->left=child; else parloc->right=child; } } } } void traversal(struct bst *root) {

if(root!=NULL) { printf("\nthe inorder traversal is :"); displayin(root); printf("\nthe preorder traversal is :"); displaypre(root); printf("\nthe postorder traversal is :"); displaypost(root); } else printf("\nThe tree is empty\n"); }

Vous aimerez peut-être aussi