You are on page 1of 15

Ex No: 01 08-06-2012 CODING: #include<stdio.h> #include<stdlib.

h>

BINARY SEARCH TREE

struct tree { int no,n; struct tree *left,*right,*ptr; }; int count=0; struct tree *parent,*del,*pwalk,*start,*new,*end; struct tree *p,*fop,*rop,*lrop,*frop,*ptr; main() { int ch1,ch2,n,num,sear; do { printf("\n1-Create\n2-Insert\n3-Traverse\n4-Search\n5-Delete\n6-Exit"); printf("\nEnter your choice : "); scanf("%d",&ch1); switch(ch1) { case 1: create(); printf("\nAfter Creation.....\n\n"); inorder(start); break; case 2: insert(); printf("\nAfter Insertion.....\n\n"); inorder(start); break; case 3: printf("\nIn Order Traversal.....\n\n"); inorder(start); printf("\nPre Order Traversal.....\n\n"); preorder(start); printf("\nPost Order Traversal.....\n\n"); postorder(start); break;

case 4: printf("\nFind\n\t1-Smallest,\n\t2-Largest\n\t3-Requested Number."); printf("\n\n\tEnter your choice : "); scanf("%d",&ch2); if(ch2==3) { printf("Enter the number Search : "); scanf("%d",&sear); search(start,sear); } if(ch2==1) small(start); if(ch2==2) large(start); break; case 5: printf("\nEnter the Number for Deleting : "); scanf("%d",&n); delete(start,n); printf("\nAfter Deletion.....\n"); inorder(start); break; } /* End of Switch */ }while(ch1!=6); /* End of Do While */ /* End of Main */

create() { start=NULL; new=(struct tree*)malloc(sizeof(struct tree)); printf("Enter a Number : "); scanf("%d",&new->no); new->left=NULL; new->right=NULL; start=new; end=new; } insert() { new=(struct tree*)malloc(sizeof(struct tree)); printf("Enter a Number : "); scanf("%d",&new->no); count=count++;

pwalk=start; while(pwalk!=NULL) { parent=pwalk; if(new->no<pwalk->no) pwalk=pwalk->left; else pwalk=pwalk->right; } if(new->no<parent->no) parent->left=new; else parent->right=new; } inorder(struct tree *start) { if(start!=NULL) { inorder(start->left); printf(" %d ",start->no); inorder(start->right); } } preorder(struct tree *start) { if(start!=NULL) { printf(" %d ",start->no); preorder(start->left); preorder(start->right); } } postorder(struct tree *start) { if(start!=NULL) { postorder(start->left); postorder(start->right); printf(" %d ",start->no); } }

small(struct tree *start) { if(start->left==NULL) printf("%d Number Is \"FOUND\"",start->no); else small(start->left); } large(struct tree *start) { if(start->right==NULL) printf("%d Number Is \"FOUND\"",start->no); else large(start->right); } search(struct tree *start,int num) { if(num<start->no) search(start->left,num); else if(num>start->no) search(start->right,num); else if(start->no==num) printf("%d Number Is \"FOUND\" at %d-th Position",start->no,count); else printf("Number Is \"NOT FOUND\""); } delete(struct tree *start,int n) { p=start; fop=NULL; while(p!=NULL&&p->no!=n) { fop=p; p=(n<p->no)?p->left:p->right; } if(p==NULL) printf("key is not \"FOUND\""); if(p->left==NULL) rop=p->right; else if(p->right==NULL) rop=p->left; else {

frop=p; rop=p->right; lrop=rop->left; while(lrop!=NULL) { frop=rop; rop=lrop; lrop=p->left; } if(frop!=p) { frop->left=rop->right; rop->right=p->right; } rop->left=p->left; } if(fop==NULL) start=rop; else if(p==fop->left) fop->left=rop; else fop->right=rop; free(p); }

OUPUT:

Ex No: 02 -06-2012 CODING:

IMPLEMENTATION OF AVL TREE

#include <stdio.h> #include <stdlib.h> int h; struct AVLnode { int data; int balfact; struct AVLnode *left,*right; }; struct AVLnode *insert(struct AVLnode *,int,int*); struct AVLnode *delete(struct AVLnode *,int,int*); struct AVLnode *del(struct AVLnode *,struct AVLnode *,int *); struct AVLnode *balright(struct AVLnode *,int *); struct AVLnode *balleft(struct AVLnode *,int *); void display(struct AVLnode *); int main() { int ch,key; struct AVLnode *avl = NULL; do { printf("\n\t1.Insertion\n\t2.Deletion\n\t3.Display\n\t4.Exit"); printf("\nEnter the Choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("\n\t\tAVL INSERTION\nEnter the Element : "); scanf("%d",&key); avl = insert(avl,key,&h); printf("\n\nCurrent AVL Tree Elements are : "); display(avl); printf("\n\n"); break; case 2: printf("\n\t\tAVL DELETION\nEnter the Element : "); scanf("%d",&key); avl = delete(avl,key,&h); printf("\n\nCurrent AVL Tree Elements are : ");

display(avl); printf("\n\n"); break; case 3: printf("\n\nCurrent AVL Tree Elements are : "); display(avl); printf("\n\n"); break; default: printf("\n\n\t\tEnd of the Program...\n\n"); break; } }while(ch!=4); } struct AVLnode *insert(struct AVLnode *root, int data, int *h) { struct AVLnode *node1,*node2; if ( !root) { root = (struct AVLnode *)malloc(sizeof (struct AVLnode)); root->data=data; root->left=NULL; root->right=NULL; root->balfact = 0; *h = 1; return(root); } if (data < root->data) { root->left = insert(root->left,data,h); if (*h) { switch(root->balfact) { case 1: node1=root->left; if(node1->balfact==1) { printf("\n\t\t*****BALANCING*****"); printf("\n\tSINGLE ROTATION\nRight Rotation along %d ",root->data); root->left = node1->right; node1->right = root; root->balfact = 0; root = node1;

} else { printf("\n\t\t*****BALANCING*****"); printf("\n\tDOUBLE ROTATION\nLeft Rotation along %d ",node1->data); node2 = node1->right; node1->right = node2->left; printf("\nRight Rotation along %d ",root->data); node2->left = node1; root->left = node2->right; node2->right = root; if(node2->balfact == 1) root->balfact = -1; else root->balfact = 0; if(node2->balfact == -1) node1->balfact = 1; else node1->balfact = 0; root = node2; } root->balfact = 0; *h=0; break; } } } if(data > root->data) { root->right=insert(root->right,data,h); if(*h) { switch(root->balfact) { case 1: root->balfact=0; *h=0; break; case 0: root->balfact=-1; break; case -1: node1=root->right; if(node1->balfact==-1) {

printf("\n\t\t*****BALANCING*****"); printf("\n\tSINGLE ROTATION\nLeft Rotation along %d",root->data); root->right=node1->left; node1->left=root; root->balfact=0; root=node1; } else { printf("\n\t\t*****BALANCING*****"); printf("\n\tDOUBLE ROTATION\nRight Rotation along %d",node1->data); node2=node1->left; node1->left=node2->right; node2->right=node1; printf("\nLeft Rotation along %d\n",root->data); root->right=node2->left; node2->left=root; if(node2->balfact==-1) root->balfact=1; else root->balfact=0; if(node2->balfact==1) node1->balfact=-1; else node1->balfact=0; root=node2; } root->balfact=0; *h = 0; } } } return (root); } struct AVLnode *delete(struct AVLnode *root,int data,int *h) { struct AVLnode *node,*tp; node = root; if((root)&&(node->left == NULL)&&(node->right==NULL)) { *h = 1; free(node); root = NULL; return(root); }

if(!root) { printf("\nData not Found..."); return(root); } else { if(data < root->data) { root->left = delete(root->left,data,h); if(*h) root = balright(root,h); } else { if(data > root->data) { root->right = delete(root->right,data,h); if(*h) root = balleft(root,h); } else { node = root; if((node->right == NULL)&&(node->data==data)) { tp = node; root = node->left; tp->left=NULL; free(tp); *h=1; return (root); } else { if(node->left==NULL) { root = node->right; *h=1; free(node); } else { node->right = del(node->right,node,h); if(*h)

root = balleft(root,h); } } } } } return (root); } struct AVLnode *del(struct AVLnode *succ,struct AVLnode *node,int *h) { struct AVLnode *temp = succ; if(succ->left!=NULL) { succ->left = del(succ->left,node,h); if(*h) succ = balright(succ,h); } else { temp = succ; node->data = succ->data; succ = succ->right; free(temp); *h=1; } return(succ); } struct AVLnode *balright(struct AVLnode *root,int *h) { struct AVLnode *node1, *node2; switch(root->balfact) { case 1: root->balfact = 0; break; case 0: root->balfact = -1; break; case -1: node1 = root->right; if(node1->balfact <= 0) { printf("\n\t\t*****BALANCING*****"); printf("\n\tSINGLE ROTATION\nLeft Rotation along %d",root->data);

root->right = node1->left; node1->left=root; if(node1->balfact == 0) { root->balfact =-1; node1->balfact = 1; *h=0; } else root->balfact = node1->balfact = 0; root = node1; } else { printf("\n\t\t*****BALANCING*****"); printf("\n\tDOUBLE ROTATION\nRight Rotation along %d",node1->data); node2 = node1->left; node1->left = node2->right; node2->right = node1; printf("\nLeft Rotation along %d",root->data); root->right = node2->left; node2->left = root; if(node2->balfact == -1) root->balfact = 1; else root->balfact = 0; if(node2->balfact = 1) node1->balfact = -1; else node1->balfact = 0; root = node2; node2->balfact = 0; } } return (root); } struct AVLnode *balleft(struct AVLnode *root,int *h) { struct AVLnode *node1,*node2; switch(root->balfact) { case -1: root->balfact = 0; break;

case 0: root->balfact = 1; case 1: node1=root->left; if(node1->balfact>=0) { printf("\n\t\t*****BALANCING*****"); printf("\n\tSINGLE ROTATION\nRight Rotation Along %d",root->data); root->left = node1->right; node1->right=root; if (node1->balfact == 0) { root->balfact=1; node1->balfact=-1; *h=0; } else root->balfact = node1->balfact=0; root=node1; } else { printf("\n\t\t*****BALANCING*****"); printf("\n\tDOUBLE ROTATION\nLeft Rotation Along %d",node1->data); node2=node1->right; node1->right=node2->left; node2->left=node1; printf("\nRight Rotation Along %d",root->data); root->left=node2->right; node2->right=root; if(node2->balfact == 1) root->balfact=-1; else root->balfact=0; if(node2->balfact == -1) node1->balfact=1; else node1->balfact=0; root=node2; node2->balfact=0; } } return(root); }

void display(struct AVLnode *root) { if(root!=NULL) { display(root->left); printf("\t%d",root->data); display(root->right); } }

You might also like