You are on page 1of 5

#include<stdio.

h> typedef struct bst { int data; struct bst *lchild,*rchild; }bst; bst *fmax(bst *p) { if(p==NULL) return NULL; else if(p->rchild==NULL) return p; else return fmax(p->rchild); } bst *fmin(bst *p) { if(p==NULL) return NULL; else if(p->lchild==NULL) return p; else return fmin(p->lchild); } bst *ins(bst *p,int x) { bst *n; n=(bst*)malloc(sizeof(n)); n->lchild=NULL; n->rchild=NULL; n->data=x; if(p==NULL) { p=n; return p; } else if(x<=p->data) { p->lchild=ins(p->lchild,x); } else p->rchild=ins(p->rchild,x); return p; } bst *find(bst *p,int x) { if(p==NULL) return NULL;

else if(x<p->data) return find(p->lchild,x); else if(x>p->data) return find(p->rchild,x); else return p; } bst *del(bst *p,int x) { bst *t,*t1; int temp; if(p==NULL) return NULL; else if(x<p->data) p->lchild=del(p->lchild,x); else if(x>p->data) p->rchild=del(p->rchild,x); else if(p->lchild!=NULL&&p->rchild!=NULL) { t1=fmin(p->rchild); p->data=t1->data; p->rchild=del(p->rchild,p->data); } else { t=p; if(p->lchild==NULL) p=p->rchild; else if(p->rchild==NULL) p=p->lchild; free(t); } return p; } void in(bst *p) { if(p!=NULL) { in(p->lchild); printf("%d\t",p->data); in(p->rchild); } } void pre(bst *p) { if(p!=NULL) {

printf("\t%d",p->data); pre(p->lchild); pre(p->rchild); } } void post(bst *p) { if(p!=NULL) { post(p->lchild); post(p->rchild); printf("\t%d",p->data); } } main() { int x,ch; bst *n=NULL,*p=NULL; int a; while(1) { printf("\n1.insert\n2.find maximum element\n3.find minimum element\n4.delete element\n5.find an element\n6.display\n7.exit\nenter choice"); scanf("%d",&ch); switch(ch) { case 1:printf("enter element to be inserted"); scanf(" %d",&x); n=ins(n,x); break; case 2: p=fmax(n); if(p!=NULL) printf("the maximum value is %d",p->data); else printf("no elements"); break; case 3: p=fmin(n); if(p!=NULL) printf("the minimum value is %d",p->data); else printf("no elements"); break; case 4: printf("enter element to be deleted"); scanf("%d",&x); p=find(n,x);

if(p!=NULL) { n=del(n,x); printf("deletd"); in(n); } else printf("no element"); break; case 5:printf("enter element to search"); scanf("%d",&x); p=find(n,x); if(p!=NULL) printf("the found value is %d",p->data); else printf("no elements"); break; case 6: do { printf("\n1.inorder \n2.preorder\n3.postorder\nenter choice"); scanf(" %d",&a); switch(a) { case 1:in(n); break; case 2:pre(n); break; case 3:post(n); break; } }while(a<4); break; case 7:exit(0); break; default:printf("enter valid choice"); } } } Expression tree: #include<stdio.h> typedef struct etree { char data; struct etree *lchild,*rchild; }node; node *getnode(char);

void inorder(node *); main() { int i=0; char postfix[20]; node *stack[20],*t,*t1,*t2; int top=-1; printf("enter postfix expression"); scanf("%s",postfix); while(postfix[i]!='\0') { t=getnode(postfix[i]); if(isalpha(postfix[i])||isdigit(postfix[i])) stack[++top]=t; else { t1=stack[top--]; t2=stack[top--]; t->rchild=t1; t->lchild=t2; stack[++top]=t; } i++; } t=stack[top]; printf("\nthe expression tree traversed in inorder"); inorder(t); } node *getnode(char ch) { node *t; t=(node *)malloc(sizeof(node)); t->data=ch; t->lchild=NULL; t->rchild=NULL; return t; } void inorder(node *present) { if(present!=NULL) { inorder(present->lchild); printf(" %c",present->data); inorder(present->rchild); } }

You might also like