void main() { FILE *pipe; NODE H=NULL; int n,k,j; int m=0; H=Create_Leaf(0,H); H->left=H->right=H; printf("\n Enter number of nodes "); scanf("%d",&n); OutputFile = fopen ("avltree.dot", "w"); fclose (OutputFile); for(j=0;j<n;j++) { printf("Enter element"); scanf("%d",&k); //redirect from file "avlIP" H=insert(k,H); } OutputFile=fopen("avltree.dot","a"); if(OutputFile != NULL) { if(H->left!=NULL) dotDump(H->left,OutputFile); else dotDump(H->right,OutputFile); } fclose(OutputFile); pipe=popen("dot -Tps avltree.dot -o avltree.ps","w"); pclose(pipe); pipe=popen("evince avltree.ps","r"); pclose(pipe); }
NODE insert(int key,NODE H) { NODE NN; if(H->left==H) { NN=Create_Leaf(key,NN); H->left=NN; return H; } lvl=0; dir[lvl]='L'; path[lvl]=H; T=H->left; while(1) { if(key<T->key) { if(T->left!=NULL) { lvl=lvl+1; path[lvl]=T; dir[lvl]='L'; T=T->left; } else { NN=Create_Leaf(key,NN); T->left=NN; lvl=lvl+1; path[lvl]=T; dir[lvl]='L'; break; } } else if(key>T->key) { if(T->right!=NULL) { lvl=lvl+1; path[lvl]=T; dir[lvl]='R'; T=T->right; } else { NN=Create_Leaf(key,NN); T->right=NN; lvl=lvl+1; path[lvl]=T; dir[lvl]='R'; break; } } else { printf("\n Key already exists "); return H; } } mark=0; for(i=lvl;i>=1;i--) { P=path[i]; if(P->BI!='B') { mark=i; break; } } for(i=mark+1;i<=lvl;i++) { if(key<path[i]->key) { path[i]->BI='L'; } else path[i]->BI='R'; } if(mark==0) { return H; } D=dir[mark]; X=path[mark]; Y=path[mark+1]; if(X->BI!=D) { X->BI='B'; return H; } //rebalancing 1 if(Y->BI==D) { if(D=='L') { X->left=Y->right; Y->right=X; } else { X->right=Y->left; Y->left=X; } X->BI=Y->BI='B'; F=path[mark-1]; if(X==F->left) { F->left=Y; } else F->right=Y; return H; } //rebalancing 2 //changing structure link if(D=='L') { Z=Y->right; Y->right=Z->left; Z->left=Y; X->left=Z->right; Z->right=X; } else { Z=Y->left; Y->left=Z->right; Z->right=Y; X->right=Z->left; Z->left=X; } F=path[mark-1]; if(X==F->left) { F->left=Z; } else F->right=Z; //changing balance indicators if(Z->BI==D) { Y->BI=Z->BI='B'; if(D=='L') { X->BI='R'; } else X->BI='L'; } else if(Z->BI=='B') { X->BI=Y->BI=Z->BI='B'; } else { X->BI=Z->BI='B'; Y->BI=D; } return H; }
//Program to insert a node into the tree NODE Insert(int key,NODE H) { NODE NN; if(H->llink==H) { NN=Create_Leaf(key,NN); H->llink=NN; return H; } level=0; dir[level]='L'; path[level]=H; t=H->llink; while(1) { //if the key entered is lesser than the present key if(key<t->key) { if(t->llink!=NULL) { level=level+1; path[level]=t; dir[level]='L'; t=t->llink; } else { NN=Create_Leaf(key,NN); t->llink=NN; level=level+1; path[level]=t; dir[level]='L'; break; } } else if(key>t->key) { if(t->rlink!=NULL) { level=level+1; path[level]=t; dir[level]='R'; t=t->rlink; } else { NN=Create_Leaf(key,NN); t->rlink=NN; level=level+1; path[level]=t; dir[level]='R'; break; } }//if the keys are equal else { key = key + 1; return H; } } mark=0; //to maintain the balance factor of the nodes for(i=level;i>=1;i--) { p=path[i]; if(p->BI!='B') { mark=1; break; } } //To maintain the path of the tree for(i=mark+1;i<=level;i++) { if(key<path[i]->key) { path[i]->BI='L'; } else path[i]->BI='R'; } if(mark==0) { return H; } d=dir[mark]; x=path[mark]; y=path[mark+1]; if(x->BI!=d) { x->BI='B'; return H; } if(y->BI==d) { if(d=='L') { x->llink=y->rlink; y->rlink=x; } else { x->rlink=y->llink; y->llink=x; } x->BI=y->BI='B'; f=path[mark-1]; if(x==f->llink) { f->llink=y; } else f->rlink=y; return H; } if(d=='L') { z=y->rlink; y->rlink=z->llink; z->llink=y; x->llink=z->rlink; z->rlink=x; } else { z=y->llink; y->llink=z->rlink; z->rlink=y; x->rlink=z->llink; z->llink=x; } f=path[mark-1]; if(x==f->llink) { f->llink=z; } else f->rlink=z; if(z->BI==d) { y->BI=z->BI='B'; if(d=='L') { x->BI='R'; } else x->BI='L'; } else { if(z->BI=='B') { x->BI=y->BI=z->BI='B'; } else { x->BI=z->BI='B'; y->BI=d; } } return H; }
//Program to create a header node NODE Create_Head(int key,NODE H) { H=Create_Leaf(0,H); H->llink=H->rlink=H; return H; }