You are on page 1of 5

IMPLEMENTATION OF AVL TREE #include<stdio.h> #include<conio.

h> struct avlnode { struct avlnode *left,*right; int data,height; }*t=NULL,*s=NULL; struct avlnode *SRWL(struct avlnode *k2) { struct avlnode *k1; k1=k2->left; k2->left=k1->right; k1->right=k2; k2->height=max(k2->left->height,k2->right->height)+1; k1->height=max(k1->left->height,k1->right->height)+1; return k1; } struct avlnode *SRWR(struct avlnode *k1) { struct avlnode *k2; k2=k1->right; k1->right=k2->left; k2->left=k1; k1->height=max(k1->left->height,k1->right->height)+1; k2->height=max(k2->left->height,k2->right->height)+1; return k2; } int max(int x,int y) { if(x>y) return x; else return y; } struct avlnode *DRWL(struct avlnode *k3) { k3->left=SRWR(k3->left); return(SRWL(k3)); } struct avlnode *DRWR(struct avlnode *k1) { k1->right=SRWL(k1->right); return(SRWR(k1)); }

struct avlnode *insert(struct avlnode *t,int x) { if(t==NULL) { t=(struct avlnode *)malloc(sizeof(struct avlnode)); t->data=x; t->left=t->right=NULL; t->height=NULL; } else if(x<t->data) { t->left=insert(t->left,x); if((t->left->height)-(t->right->height)==2) { if(x<t->left->data) t=SRWL(t); else t=DRWL(t); } } else if(x>t->data) { t->right=insert(t->right,x); if((t->right->height)-(t->left->height)==2) { if(x>t->right->data) t=SRWR(t); else t=DRWR(t); } } else printf("\nNode already exists"); t->height=max(t->left->height,t->right->height)+1; return t; } struct avlnode *makeempty(struct avlnode *t) { if(t!=NULL) { makeempty(t->left); makeempty(t->right); free(t); } return NULL;

} void inorder(struct avlnode *t) { if(t!=NULL) { inorder(t->left); printf("%d",t->data); inorder(t->right); } } void preorder(struct avlnode *t) { if(t!=NULL) { printf("%d",t->data); preorder(t->left); preorder(t->right); } } void postorder(struct avlnode *t) { if(t!=NULL) { postorder(t->left); postorder(t->right); printf("%d",t->data); } } int findmax(struct avlnode *t) { if(t==NULL) printf("\nTree not exists"); else if(t->right==NULL) return(t->data); else return(findmax(t->right)); } int findmin(struct avlnode *t) { if(t==NULL) printf("\nTree not exists"); else if(t->left==NULL) return(t->data); else return(findmin(t->left)); }

int find(int x,struct avlnode *t) { if(t==NULL) printf("\nNode not exists"); else if(x<t->data) return(find(x,t->left)); else if(x>t->data) return(find(x,t->right)); else printf("\nNode found"); } void main() { int ch,a; clrscr(); printf("\n1.Insertion\n2.Findmax\n3.Findmin\n4.Find\n5.Makeempty\n6.Inorder\n7.Preorder\n8. Postorder"); do { printf("\nEnter the choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the data"); scanf("%d",&a); s=insert(s,a); break; case 2: a=findmax(s); printf("%d",a); break; case 3: a=findmin(s); printf("%d",a); break; case 4: printf("\nEnter node to be find"); scanf("%d",&a); a=find(a,s); break; case 5: printf("\nMake empty"); s=makeempty(s); break;

case 6: inorder(s); break; case 7: preorder(s); break; case 8: postorder(s); break; default: printf("\nInvalid choice."); break; } }while(ch>0&&ch<9); getch(); } OUTPUT: Enter the choice:1 Enter the data 40 Enter the choice:1 Enter the data 41 Enter the choice:1 Enter the data:42 Enter the choice:7 Preorder:41 40 42 Enter the choice:6 Inorder:40 41 42 Enter the choice:8 Postorder:40 42 41 Enter the choice:2 Maximum value is:42 Enter the choice:3 Minimum value is:40 Enter the choice:4 Enter node to be find:40 Node found Enter the choice:5 Make empty Enter the choice:45 Invalid choice.

You might also like