You are on page 1of 35

BRUTE FORCE #include<iostream.h> #include<conio.h> #include<string.

h> main() { char text[]="abcabcabcabxabacdabcabcxyyabcabczabcabcx"; char pat[]="abcabcx"; int found=0; clrscr(); int m=strlen(text); int n=strlen(pat); for(int i=0;i<=m-n;i++) { for(int j=0;j<n;j++) { if(pat[j]!=text[i+j]) break; } if(j==n) { cout<<"\nFound at Pos : "<<i; found=1; } } if(found == 0) cout<<"\nNot Found"; getch(); }

BOYER MOORE #include<iostream.h> #include<conio.h> #include<string.h> void main() { char text[]="this is a Boyer Moore algorithm, check the algorithm"; char pat[]="algorithm"; int found=0, i,j,m,n; clrscr(); m=strlen(text); n=strlen(pat); i=j=n-1; if(i>m-1) return; while(i<m) { if(pat[j] != text[i]) { int k=n-2; while(k>=0 && text[i]!=pat[k]) k--; if(k!=-1) i=i+n-1-k; else i=i+n; j=n-1;

} else { i--; j--; if(j==-1) { found=1; cout<<"\n Found at "<<i+1; j=n-1; i=i+n+1; } } } if(found == 0) cout<<"\nNot Found"; getch(); } KNUTH MORRIS -PROT void kmpPreprocess() { int i=0, j=-1; b[i]=j; while (i<m) { while (j>=0 && p[i]!=p[j]) j=b[j]; i++; j++; b[i]=j; } } Int kmpSearch() { int i=0, j=0; while (i<n) { while (j>=0 && t[i]!=p[j]) j=b[j]; i++; j++; if (j==m) { Return i-j; J=b[j]; } } Retrun -1 }

/ Modified code im sure it ll work with gcc // i compiled it using Bloodshed // This is really simple logic // accidentially the author & me implimented almost same logic // But this one is only a modified version of the original file // manuvs@msn.com

#include #include class prims { private: int n; //no of nodes int graph_edge[250][4]; //edges in the graph int g; //no of edges in the graph int tree_edge[250][4]; //edges in the tree int t; //no of edges in the tree int s; //source node int i; //Partition the graph in to two sets int T1[50],t1; // Set 1 int T2[50],t2; // Set 2 public: void input(); int findset(int); void algorithm(); void output(); }; void prims::input() { cout<<*************************************************\n <<This program implements the prims algorithm\n <<*************************************************\n; cout<>n; g=0; cout<<Enter the weights for the following edges ::\n; for(i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { cout<< < <<i<< , <<j< ::; int w; cin>>w; if(w!=0) { g++;

graph_edge[g][1]=i; graph_edge[g][2]=j; graph_edge[g][3]=w; } } } // print the graph edges cout<<\n\nThe edges in the given graph are::\n; for(i=1;i<=g;i++) cout<< < <<graph_edge[i][1] << , <<graph_edge[i][2] < ::<<graph_edge[i][3]<<endl; } int prims::findset(int x) { for(i=1;i<=t1;i++) if(x==T1[i]) return 1; for(i=1;i<=t2;i++) if(x==T2[i]) return 2; return -1; } void prims::algorithm() { t=0; t1=1; T1[1]=1; //The source node t2=n-1; int i; for(i=1;i<=n-1;i++) T2[i]=i+1; //The reamining nodes cout<<\n*****The algorithm starts*****\n\n; while(g!=0 && t!=n-1) {

// Find the least cost edge int min=9999; int p; int u,v,w; for(i=1;igraph_edge[i][3]) { min=graph_edge[i][3]; u=graph_edge[i][1]; v=graph_edge[i][2]; w=graph_edge[i][3]; p=i; } } } //break if there is no such edge cout<<The edge included in the tree is ::; cout<< < <<u<< , <<v< <<endl; //delete the edge from graph edges for(int l=p;l<g;l++) { graph_edge[l][1]=graph_edge[l+1][1]; graph_edge[l][2]=graph_edge[l+1][2]; graph_edge[l][3]=graph_edge[l+1][3]; } g; //add the edge to the tree t++; tree_edge[t][1]=u; tree_edge[t][2]=v; tree_edge[t][3]=w; //Alter the set partitions t1++; int m; if(findset(v)==2) {

T1[t1]=v; m=v; } else if(findset(u)==2) { T1[t1]=u; m=u; } int x; for(x=1;T2[x]!=m;x++); for(;x<t2;x++) T2[x]=T2[x+1]; t2; // Print the sets int k; cout<<NOW\nT1 :: ; for(k=1;k<=t1;k++) cout<<T1[k]<< ; cout<<endl; cout<<T2 :: ; for(k=1;k<=t2;k++) cout<<T2[k]<< ; cout<<endl; cout<<The graph edges are ::\n; for(i=1;i<=g;i++) cout<< < <<graph_edge[i][1] << , <<graph_edge[i][2] < ::<<graph_edge[i][3]<<endl; cout<<endl<<endl; } } void prims::output() { cout<<\nThe selected edges are ::\n; for(int i=1;i<=t;i++)

cout<< < <<tree_edge[i][1] << , <<tree_edge[i][2] < ::<<tree_edge[i][3]<<endl; } int main() { prims obj; obj.input(); obj.algorithm(); obj.output(); system(PAUSE); return 0; }

> This Program is to implement Kruskal algorithm. -> This program is to find minimum spanning tree for undirected weighted graphs -> Data Structers used: Graph:Adjacency Matrix -> This program works in microsoft vc++ 6.0 environment. **************************************************************/ #include<iostream.h> class kruskal { private: int n; //no of nodes int noe; //no edges in the graph int graph_edge[100][4]; int tree[10][10]; int sets[100][10]; int top[100]; public: void read_graph(); void initialize_span_t(); void sort_edges(); void algorithm();

int find_node(int ); void print_min_span_t(); }; void kruskal::read_graph() { cout<<*************************************************\n <<This program implements the kruskal algorithm\n <<*************************************************\n; cout<<Enter the no. of nodes in the undirected weighted graph ::; cin>>n; noe=0; cout<<Enter the weights for the following edges ::\n; for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { cout<< < <<i<< , <<j<< > ::; int w; cin>>w; if(w!=0) { noe++; graph_edge[noe][1]=i; graph_edge[noe][2]=j; graph_edge[noe][3]=w; } } } // print the graph edges cout<<\n\nThe edges in the given graph are::\n; for(i=1;i<=noe;i++) cout<< < <<graph_edge[i][1] << , <<graph_edge[i][2] << > ::<<graph_edge[i][3]<<endl; }

void kruskal::sort_edges() { /**** Sort the edges using bubble sort in increasing order**************/ for(int i=1;i<=noe-1;i++) { for(int j=1;j<=noe-i;j++) { if(graph_edge[j][3]>graph_edge[j+1][3]) { int t=graph_edge[j][1]; graph_edge[j][1]=graph_edge[j+1][1]; graph_edge[j+1][1]=t; t=graph_edge[j][2]; graph_edge[j][2]=graph_edge[j+1][2]; graph_edge[j+1][2]=t; t=graph_edge[j][3]; graph_edge[j][3]=graph_edge[j+1][3]; graph_edge[j+1][3]=t; } } } // print the graph edges cout<<\n\nAfter sorting the edges in the given graph are::\n; for(i=1;i<=noe;i++) cout<< < <<graph_edge[i][1] << , <<graph_edge[i][2] << > ::<<graph_edge[i][3]<<endl; } void kruskal::algorithm() { // ->make a set for each node for(int i=1;i<=n;i++) { sets[i][1]=i; top[i]=1; } cout<<\nThe algorithm starts ::\n\n;

for(i=1;i<=noe;i++) { int p1=find_node(graph_edge[i][1]); int p2=find_node(graph_edge[i][2]); if(p1!=p2) { cout<<The edge included in the tree is :: << < <<graph_edge[i][1]<< , <<graph_edge[i][2]<< > <<endl<<endl; tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3]; tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3]; // Mix the two sets for(int j=1;j<=top[p2];j++) { top[p1]++; sets[p1][top[p1]]=sets[p2][j]; } top[p2]=0; } else { cout<<Inclusion of the edge << < <<graph_edge[i][1]<< , <<graph_edge[i][2]<< > <<forms a cycle so it is removed\n\n; } } } int kruskal::find_node(int n) { for(int i=1;i<=noe;i++) { for(int j=1;j<=top[i];j++) { if(n==sets[i][j]) return i; } }

return -1; } int main() { kruskal obj; obj.read_graph(); obj.sort_edges(); obj.algorithm(); return 0; }

> This Program is to implement Prims algorithm. -> This program is to find minimum spanning tree for undirected weighted graphs -> Data Structers used: Graph:Adjacency Matrix -> This program works in microsoft vc++ 6.0 environment. **************************************************************/ #include<iostream.h> class prims { private: int n; //no of nodes int graph_edge[250][4]; //edges in the graph int g; //no of edges in the graph int tree_edge[250][4]; //edges in the tree int t; //no of edges in the tree int s; //source node //Partition the graph in to two sets int T1[50],t1; // Set 1 int T2[50],t2; // Set 2 public: void input(); int findset(int);

void algorithm(); void output(); }; void prims::input() { cout<<*************************************************\n <<This program implements the prims algorithm\n <<*************************************************\n; cout<<Enter the no. of nodes in the undirected weighted graph ::; cin>>n; g=0; cout<<Enter the weights for the following edges ::\n; for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { cout<< < <<i<< , <<j<< > ::; int w; cin>>w; if(w!=0) { g++; graph_edge[g][1]=i; graph_edge[g][2]=j; graph_edge[g][3]=w; } } } // print the graph edges cout<<\n\nThe edges in the given graph are::\n; for(i=1;i<=g;i++) cout<< < <<graph_edge[i][1] << , <<graph_edge[i][2] << > ::<<graph_edge[i][3]<<endl; } int prims::findset(int x) {

for(int i=1;i<=t1;i++) if(x==T1[i]) return 1; for(i=1;i<=t2;i++) if(x==T2[i]) return 2; return -1; } void prims::algorithm() { t=0; t1=1; T1[1]=1; //The source node t2=n-1; int i; for(i=1;i<=n-1;i++) T2[i]=i+1; //The reamining nodes cout<<\n*****The algorithm starts*****\n\n; while(g!=0 && t!=n-1) { // Find the least cost edge int min=9999; int p; int u,v,w; for(i=1;i<=g;i++) { bool flag1=false,flag2=false; //if u and v are in different sets if(findset(graph_edge[i][1])!=findset(graph_edge[i][2])) { if(min>graph_edge[i][3]) { min=graph_edge[i][3]; u=graph_edge[i][1]; v=graph_edge[i][2]; w=graph_edge[i][3];

p=i; } } } //break if there is no such edge cout<<The edge included in the tree is ::; cout<< < <<u<< , <<v<< > <<endl; //delete the edge from graph edges for(int l=p;l<g;l++) { graph_edge[l][1]=graph_edge[l+1][1]; graph_edge[l][2]=graph_edge[l+1][2]; graph_edge[l][3]=graph_edge[l+1][3]; } g; //add the edge to the tree t++; tree_edge[t][1]=u; tree_edge[t][2]=v; tree_edge[t][3]=w; //Alter the set partitions t1++; int m; if(findset(v)==2) { T1[t1]=v; m=v; } else if(findset(u)==2) { T1[t1]=u; m=u; } int x; for(x=1;T2[x]!=m;x++);

for(;x<t2;x++) T2[x]=T2[x+1]; t2; // Print the sets int k; cout<<NOW\nT1 :: ; for(k=1;k<=t1;k++) cout<<T1[k]<< ; cout<<endl; cout<<T2 :: ; for(k=1;k<=t2;k++) cout<<T2[k]<< ; cout<<endl; cout<<The graph edges are ::\n; for(i=1;i<=g;i++) cout<< < <<graph_edge[i][1] << , <<graph_edge[i][2] << > ::<<graph_edge[i][3]<<endl; cout<<endl<<endl; } } void prims::output() { cout<<\nThe selected edges are ::\n; for(int i=1;i<=t;i++) cout<< < <<tree_edge[i][1] << , <<tree_edge[i][2] << > ::<<tree_edge[i][3]<<endl; } int main() { prims obj; obj.input(); obj.algorithm(); obj.output(); return 0; }

#include <iostream.h> #include <stdlib.h> #include<constream.h> #define FALSE 0 #define TRUE 1 struct AVLNode { int data ; int balfact ; AVLNode *left ; AVLNode *right ; } ; class avltree { private : AVLNode *root ; public : avltree( ) ; AVLNode* insert ( int data, int *h ) ; static AVLNode* buildtree ( AVLNode *root, int data, int *h ) ; void display( AVLNode *root ) ; AVLNode* deldata ( AVLNode* root, int data, int *h ) ; static AVLNode* del ( AVLNode *node, AVLNode* root, int *h ) ; static AVLNode* balright ( AVLNode *root, int *h ) ; static AVLNode* balleft ( AVLNode* root, int *h ) ; void setroot ( AVLNode *avl ) ; ~avltree( ) ; static void deltree ( AVLNode *root ) ; } ; avltree :: avltree( ) { root = NULL ; } AVLNode* avltree :: insert ( int data, int *h ) { root = buildtree ( root, data, h ) ; return root ; } AVLNode* avltree :: buildtree ( AVLNode *root, int data, int *h ) { AVLNode *node1, *node2 ; if ( root == NULL ) { root = new AVLNode ; root -> data = data ; root -> left = NULL ; root -> right = NULL ; root -> balfact = 0 ; *h = TRUE ; return ( root ) ; } if ( data < root -> data ) { root -> left = buildtree ( root -> left, data, h ) ; // If left subtree is higher if ( *h ) { switch ( root -> balfact ) { case 1 : node1 = root -> left ; if ( node1 -> balfact == 1 ) { cout << "\nRight rotation." ;

root -> left = node1 -> right ; node1 -> right = root ; root -> balfact = 0 ; root = node1 ; } else { cout << "\nDouble rotation, left then right." ; node2 = node1 -> right ; node1 -> right = node2 -> left ; 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 = FALSE ; break ; case 0 : root -> balfact = 1 ; break ; case -1 : root -> balfact = 0 ; *h = FALSE ; } } } if ( data > root -> data ) { root -> right = buildtree ( root -> right, data, h ) ; if ( *h ) { switch ( root -> balfact ) { case 1 : root -> balfact = 0 ; *h = FALSE ; break ; case 0 : root -> balfact = -1 ; break ; case -1 : node1 = root -> right ; if ( node1 -> balfact == -1 ) { cout << "\nLeft rotation." ; root -> right = node1 -> left ; node1 -> left = root ; root -> balfact = 0 ; root = node1 ; } else { cout << "\nDouble rotation, right then left." ; node2 = node1 -> left ; node1 -> left = node2 -> right ; node2 -> right = node1 ; 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 = FALSE ; } } } return ( root ) ; } void avltree :: display ( AVLNode* root ) { if ( root != NULL ) { display ( root -> left ) ; cout << root -> data << "\t" ; display ( root -> right ) ; } } AVLNode* avltree :: deldata ( AVLNode *root, int data, int *h ) { AVLNode *node ; if ( root -> data == 13 ) cout << root -> data ; if ( root == NULL ) { cout << "\nNo such data." ; return ( root ) ; } else { if ( data < root -> data ) { root -> left = deldata ( root -> left, data, h ) ; if ( *h ) root = balright ( root, h ) ; } else { if ( data > root -> data ) { root -> right = deldata ( root -> right, data, h ) ; if ( *h ) root = balleft ( root, h ) ; } else { node = root ; if ( node -> right == NULL ) { root = node -> left ; *h = TRUE ; delete ( node ) ; } else { if ( node -> left == NULL ) { root = node -> right ; *h = TRUE ; delete ( node ) ; } else { node -> right = del ( node -> right, node, h ) ; if ( *h ) root = balleft ( root, h ) ; } }

} } } return ( root ) ; } AVLNode* avltree :: del ( AVLNode *succ, AVLNode *node, int *h ) { 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 ; delete ( temp ) ; *h = TRUE ; } return ( succ ) ; } AVLNode* avltree :: balright ( AVLNode *root, int *h ) { AVLNode *temp1, *temp2 ; switch ( root -> balfact ) { case 1 : root -> balfact = 0 ; break ; case 0 : root -> balfact = -1 ; *h = FALSE ; break ; case -1 : temp1 = root -> right ; if ( temp1 -> balfact <= 0 ) { cout << "\nLeft rotation." ; root -> right = temp1 -> left ; temp1 -> left = root ; if ( temp1 -> balfact == 0 ) { root -> balfact = -1 ; temp1 -> balfact = 1 ; *h = FALSE ; } else { root -> balfact = temp1 -> balfact = 0 ; } root = temp1 ; } else { cout << "\nDouble rotation, right then left." ; temp2 = temp1 -> left ; temp1 -> left = temp2 -> right ; temp2 -> right = temp1 ; root -> right = temp2 -> left ; temp2 -> left = root ; if ( temp2 -> balfact == -1 ) root -> balfact = 1 ; else root -> balfact = 0 ; if ( temp2 -> balfact == 1 ) temp1 -> balfact = -1 ; else temp1 -> balfact = 0 ; root = temp2 ;

temp2 -> balfact = 0 ; } } return ( root ) ; } AVLNode* avltree :: balleft ( AVLNode *root, int *h ) { AVLNode *temp1, *temp2 ; switch ( root -> balfact ) { case -1 : root -> balfact = 0 ; break ; case 0 : root -> balfact = 1 ; *h = FALSE ; break ; case 1 : temp1 = root -> left ; if ( temp1 -> balfact >= 0 ) { cout << "\nRight rotation." ; root -> left = temp1 -> right ; temp1 -> right = root ; if ( temp1 -> balfact == 0 ) { root -> balfact = 1 ; temp1 -> balfact = -1 ; *h = FALSE ; } else { root -> balfact = temp1 -> balfact = 0 ; } root = temp1 ; } else { cout << "\nDouble rotation, left then right." ; temp2 = temp1 -> right ; temp1 -> right = temp2 -> left ; temp2 -> left = temp1 ; root -> left = temp2 -> right ; temp2 -> right = root ; if ( temp2 -> balfact == 1 ) root -> balfact = -1 ; else root -> balfact = 0 ; if ( temp2-> balfact == -1 ) temp1 -> balfact = 1 ; else temp1 -> balfact = 0 ; root = temp2 ; temp2 -> balfact = 0 ; } } return ( root ) ; } void avltree :: setroot ( AVLNode *avl ) { root = avl ; } avltree :: ~avltree( ) { deltree ( root ) ; } void avltree :: deltree ( AVLNode *root ) {

if ( root != NULL ) { deltree ( root -> left ) ; deltree ( root -> right ) ; } delete ( root ) ; } void main( ) { avltree at ; AVLNode *avl = NULL ; int h ; clrscr(); avl = at.insert ( 20, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 6, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 29, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 5, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 12, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 25, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 32, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 10, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 15, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 27, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 13, &h ) ; at.setroot ( avl ) ; cout << endl << "AVL tree:\n" ; at.display ( avl ) ; avl = at.deldata ( avl, 20, &h ) ; at.setroot ( avl ) ; avl = at.deldata ( avl, 12, &h ) ; at.setroot ( avl ) ; cout << endl << "AVL tree after deletion of a node:\n" ; at.display ( avl ) ; getch(); }

C Program to insert and delete elements in an AVL Tree


#include<stdio.h> #include<conio.h> #include<alloc.h> #define FALSE 0 #define TRUE 1 struct AVLNode { int data; int balfact; struct AVLNode *left; struct AVLNode *right; }; struct AVLNode * insert(struct AVLNode *,int,int *); struct AVLNode * deldata(struct AVLNode *,int,int *); struct AVLNode * del(struct AVLNode *,struct AVLNode *,int *); struct AVLNode * balr(struct AVLNode *,int *); struct AVLNode * ball(struct AVLNode *,int *); void display(struct AVLNode *); void deltree(struct AVLNode *); void main() { struct AVLNode *avl=NULL; int h; clrscr(); avl=insert(avl,20,&h); avl=insert(avl,6,&h); avl=insert(avl,29,&h); avl=insert(avl,5,&h); avl=insert(avl,12,&h); avl=insert(avl,25,&h); avl=insert(avl,32,&h); avl=insert(avl,10,&h); avl=insert(avl,15,&h); avl=insert(avl,27,&h); // avl=insert(avl,13,&h); printf("\n AVL Tree : \n"); display(avl); avl=deldata(avl,5,&h); avl=deldata(avl,12,&h); printf("\n AVL tree after deletion of a node : \n"); display(avl); deltree(avl); getch(); }

/* Inserts an element intp tree */ 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=TRUE; return(root); } if(data < root->data) { root->left=insert(root->left,data,h); /* If left subtree is higher */ if(*h) { switch(root->balfact) { case 1: node1=root->left; if(node1->balfact==1) { printf("\n Right Rotation alond %d. ",root->data); root->left=node1->right; node1->right=root; root->balfact=0; root=node1; } else { printf("\n Double rotation , left along %d",node1->data); node2=node1->right; node1->right=node2->left; printf(" then right along %d. \n",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=FALSE; break; case 0: root->balfact=1; break;

case -1: root->balfact=0; *h=FALSE; } } } if(data > root->data) { root->right=insert(root->right,data,h); /* If the right subtree is higher */ if(*h) { switch(root->balfact) { case 1: root->balfact=0; *h=FALSE; break; case 0: root->balfact=-1; break; case -1: node1=root->right; if(node1->balfact==-1) { printf("\n Left rotation along %d. ",root->data); root->right=node1->left; node1->left=root; root->balfact=0; root=node1; } else { printf("\n Double rotation , right along %d",node1->data); node2=node1->left; node1->left=node2->right; node2->right=node1; printf(" then left 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=FALSE; }

} } return(root); }

/* Deletes an item from the tree */ struct AVLNode * deldata(struct AVLNode *root,int data,int *h) { struct AVLNode *node; if(!root) { printf("\n No such data. "); return (root); } else { if(data < root->data) { root->left=deldata(root->left,data,h); if(*h) root=balr(root,h); } else { if(data > root->data) { root->right=deldata(root->right,data,h); if(*h) root=ball(root,h); } else { node=root; if(node->right==NULL) { root=node->left; *h=TRUE; free(node); } else { node->right=del(node->right,node,h); if(*h) root=ball(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=balr(succ,h); } else { temp=succ; node->data=succ->data; succ=succ->right; free(temp); *h=TRUE; } return(succ); }

/* Balance the tree , if right subtree is higher */ struct AVLNode * balr(struct AVLNode *root,int *h) { struct AVLNode *node1,*node2; switch(root->balfact) { case 1: root->balfact=0; break; case 0: root->balfact=-1; *h=FALSE; break; case -1: node1=root->right; if(node1->balfact <= 0) { printf("\n Left rotation along %d. ",root->data); root->right=node1->left; node1->left=root; if(node1->balfact==0) { root->balfact=-1; node1->balfact=1; *h=FALSE; } else { root->balfact=node1->balfact=0; } root=node1; } else {

printf("\n Double rotation , right along %d ",node1->data); node2=node1->left; node1->left=node2->right; node2->right=node1; printf(" then left 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; node2->balfact=0; } } return (root); }

/* Balances the tree , if the left subtree is higher */ struct AVLNode * ball(struct AVLNode * root,int *h) { struct AVLNode *node1,*node2; switch(root->balfact) { case -1: root->balfact=0; break; case 0: root->balfact=1; *h=FALSE; break; case 1: node1=root->left; if(node1->balfact >= 0) { printf("]n Right rotation along %d. ",root->data); root->left=node1->right; node1->right=root; if(node1->balfact==0) { root->balfact=1; node1->balfact=-1; *h=FALSE; } else { root->balfact=node1->balfact=0; } root=node1;

} else { printf("\n Double rotation , left along %d ",node1->data); node2=node1->right; node1->right=node2->left; node2->left=node1; printf(" then right along %d .\n",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); }

/*n Displays the tree in-order */ void display(struct AVLNode *root) { if(root!=NULL) { display(root->left); printf("%d\t",root->data); display(root->right); } }

/* Deletes the tree */ void deltree(struct AVLNode * root) { if(root!=NULL) { deltree(root->left); deltree(root->right); } free(root); }

# include<stdio.h> # include<malloc.h> # define F 0 # define T 1 struct NODE { char Info; int Flag; struct NODE *Left_Child; struct NODE *Right_Child; }; struct NODE *Binary_Tree (char , struct NODE *, int *); void Output(struct NODE *, int ); struct NODE *Balance_Right_Heavy(struct NODE *, int *); struct NODE *Balance_Left_Heavy(struct NODE *, int *); struct NODE *DELETE(struct NODE *, struct NODE *, int *); struct NODE *Delete_Element(struct NODE *, char , int *); /* Function to insert an element into tree */ struct NODE * Binary_Tree (char Info, struct NODE *Parent, int *H) { struct NODE *Node1; struct NODE *Node2; if(!Parent) { Parent = (struct NODE *) malloc(sizeof(struct NODE)); Parent->Info = Info; Parent->Left_Child = NULL; Parent->Right_Child = NULL; Parent->Flag = 0; *H = T; return (Parent); } if(Info < Parent->Info) { Parent->Left_Child = Binary_Tree(Info, Parent->Left_Child, H); if(*H) /* Left branch has grown higher */ { switch(Parent->Flag) { case 1: /* Right heavy */ Parent->Flag = 0; *H = F; break; case 0: /* Balanced tree */ Parent->Flag = -1; break; case -1: /* Left heavy */ Node1 = Parent->Left_Child; if(Node1->Flag == -1) { printf("\n Left to Left Rotation\n"); Parent->Left_Child= Node1->Right_Child;

Node1->Right_Child = Parent; Parent->Flag = 0; Parent = Node1; } else { printf("\n Left to right rotation\n"); Node2 = Node1->Right_Child; Node1->Right_Child = Node2->Left_Child; Node2->Left_Child = Node1; Parent->Left_Child = Node2->Right_Child; Node2->Right_Child = Parent; if(Node2->Flag == -1) Parent->Flag = 1; else Parent->Flag = 0; if(Node2->Flag == 1) Node1->Flag = -1; else Node1->Flag = 0; Parent = Node2; } Parent->Flag = 0; *H = F; } } } if(Info > Parent->Info) { Parent->Right_Child = Binary_Tree(Info, Parent->Right_Child, H); if(*H) /* Right branch has grown higher */ { switch(Parent->Flag) { case -1: /* Left heavy */ Parent->Flag = 0; *H = F; break; case 0: /* Balanced tree */ Parent->Flag = 1; break; case 1: /* Right heavy */ Node1 = Parent->Right_Child; if(Node1->Flag == 1) { printf("\n Right to Right Rotation\n"); Parent->Right_Child= Node1->Left_Child; Node1->Left_Child = Parent; Parent->Flag = 0; Parent = Node1; } else { printf("\n Right to Left Rotation\n"); Node2 = Node1->Left_Child;

Node1->Left_Child = Node2->Right_Child; Node2->Right_Child = Node1; Parent->Right_Child = Node2->Left_Child; Node2->Left_Child = Parent; if(Node2->Flag == 1) Parent->Flag = -1; else Parent->Flag = 0; if(Node2->Flag == -1) Node1->Flag = 1; else Node1->Flag = 0; Parent = Node2; } Parent->Flag = 0; *H = F; } } } return(Parent); } /* Output function */ void Output(struct NODE *Tree,int Level) { int i; if (Tree) { Output(Tree->Right_Child, Level+1); printf("\n"); for (i = 0; i < Level; i++) printf(" "); printf("%c", Tree->Info); Output(Tree->Left_Child, Level+1); } } /* Balancing Right Heavy */ struct NODE * Balance_Right_Heavy(struct NODE *Parent, int *H) { struct NODE *Node1, *Node2; switch(Parent->Flag) { case -1: Parent->Flag = 0; break; case 0: Parent->Flag = 1; *H= F; break; case 1: /* Rebalance */ Node1 = Parent->Right_Child;

if(Node1->Flag >= 0) { printf("\n Right to Right Rotation\n"); Parent->Right_Child= Node1->Left_Child; Node1->Left_Child = Parent; if(Node1->Flag == 0) { Parent->Flag = 1; Node1->Flag = -1; *H = F; } else { Parent->Flag = Node1->Flag = 0; } Parent = Node1; } else { printf("\n Right to Left Rotation\n"); Node2 = Node1->Left_Child; Node1->Left_Child = Node2->Right_Child; Node2->Right_Child = Node1; Parent->Right_Child = Node2->Left_Child; Node2->Left_Child = Parent; if(Node2->Flag == 1) Parent->Flag = -1; else Parent->Flag = 0; if(Node2->Flag == -1) Node1->Flag = 1; else Node1->Flag = 0; Parent = Node2; Node2->Flag = 0; } } return(Parent); } /* Balancing Left Heavy */ struct NODE * Balance_Left_Heavy(struct NODE *Parent, int *H) { struct NODE *Node1, *Node2; switch(Parent->Flag) { case 1: Parent->Flag = 0; break; case 0: Parent->Flag = -1; *H= F; break; case -1: /* Rebalance */

Node1 = Parent->Left_Child; if(Node1->Flag <= 0) { printf("\n Left to Left Rotation\n"); Parent->Left_Child= Node1->Right_Child; Node1->Right_Child = Parent; if(Node1->Flag == 0) { Parent->Flag = -1; Node1->Flag = 1; *H = F; } else { Parent->Flag = Node1->Flag = 0; } Parent = Node1; } else { printf("\n Left to Right Rotation\n"); Node2 = Node1->Right_Child; Node1->Right_Child = Node2->Left_Child; Node2->Left_Child = Node1; Parent->Left_Child = Node2->Right_Child; Node2->Right_Child = Parent; if(Node2->Flag == -1) Parent->Flag = 1; else Parent->Flag = 0; if(Node2->Flag == 1) Node1->Flag = -1; else Node1->Flag = 0; Parent = Node2; Node2->Flag = 0; } } return(Parent); } /* Replace the node at which key is found with last right key of a left child */ struct NODE * DELETE(struct NODE *R, struct NODE *Temp, int *H) { struct NODE *Dnode = R; if( R->Right_Child != NULL) { R->Right_Child = DELETE(R->Right_Child, Temp, H); if(*H) R = Balance_Left_Heavy(R, H); } else { Dnode = R; Temp->Info = R->Info; R = R->Left_Child;

free(Dnode); *H = T; } return(R); } /* Delete the key element from the tree */ struct NODE * Delete_Element(struct NODE *Parent, char Info, int *H) { struct NODE *Temp; if(!Parent) { printf("\n Information does not exist"); return(Parent); } else { if (Info < Parent->Info ) { Parent->Left_Child = Delete_Element(Parent->Left_Child, Info, H); if(*H) Parent = Balance_Right_Heavy(Parent, H); } else if(Info > Parent->Info) { Parent->Right_Child = Delete_Element(Parent->Right_Child, Info, H); if(*H) Parent = Balance_Left_Heavy(Parent, H); } else { Temp= Parent; if(Temp->Right_Child == NULL) { Parent = Temp->Left_Child; *H = T; free(Temp); } else if(Temp->Left_Child == NULL) { Parent = Temp->Right_Child; *H = T; free(Temp); } else { Temp->Left_Child = DELETE(Temp->Left_Child, Temp, H); if(*H) Parent = Balance_Right_Heavy(Parent, H); } } } return(Parent); } /* Function main */

void main() { int H; char Info ; char choice; struct NODE *Tree = (struct NODE *)malloc(sizeof(struct NODE)); Tree = NULL; printf("\n Input choice 'b' to break:"); choice = getchar(); while(choice != 'b') { fflush(stdin); printf("\n Input information of the node: "); scanf("%c", &Info); Tree = Binary_Tree(Info, Tree, &H); printf("\n Tree is:\n"); Output(Tree, 1); fflush(stdin); printf("\n Input choice 'b' to break:"); choice = getchar(); } fflush(stdin); while(1) { printf("\n Input choice 'b' to break:"); printf("\n Input the key value want to deletedir:"); scanf("%c", &Info); if (Info == 'b') break; Tree = Delete_Element(Tree, Info, &H); printf("\n Tree is:\n"); Output(Tree, 1); } }

You might also like