Get Latest Greetings,Scraps
Thursday, December 30, 2010
<<<<<<<<<......Happy New Year.......>>>>>>>>>>
A very HAPPY NEW YEAR to all the visitors.
Its been a pleasure for me working in C.
I am very happy to see the visitors from different countries all over the world.
I am very optimistic towards this and hope more people follow my blog and get benefitted..........
Once again ..............
Have a Prosperous NEW YEAR 2011.
New Year Spl: "GUESS it RIGHT" - The Game
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
/* This program plays simple guessing game . The computer picks a
random number from 0 to 100 , and the user tries to guess the number */
const int totchan = 7;
void main()
{
int number ; // computers rabdom number
int guess; // the users guess number
int chances=0,score=0,chanscor;// chanscor stores score for 1 successful chance
char ans;
do
{
clrscr();
chances=score=0;
printf("\n\t\t\t\t Welcome to the HIGH/LOW GAME ");
printf("\n\t\t\t\t I will pick a random number from 0 to 100 ");
printf("\n\t\t\t\t You must try to guess the number ");
randomize();
number =(int)(rand()%100);
chanscor=100/totchan;
do
{
printf("\n\t What is your GUESS ?? ");
scanf("%d",&guess);
if((guess<0) || (guess>100))
{
printf(" Sorry but your guess %d must be from 0 to 100 ");
}
else if (guess < number )
{
printf(" %d is LOW . Try a higher number ",guess);
}
else if(guess>number)
{
printf("%d is HIGH . Try a lower number ",guess);
}
else
{
printf("%d is correct . Congratulations !!!! ");
score = chanscor*(totchan-chances);
printf("\n\t Your score is %d \n",score);
break;
}
chances++;
if(guess!=number)
printf("\n\n Now you have %d chances left \n",totchan-chances);
if(chances == totchan)
{
printf("\n\n Only %d chances are allowed . Better luck next time",totchan);
printf("\n The actual number was %d \n",number);
break;
}
}while(guess!=number);
printf("\n\n Thank You for playing this game !!!!!!!!! ");
printf("\n\n Want to play it again ?(y/n)..");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y' || ans=='Y');
}
#include<conio.h>
#include<stdlib.h>
/* This program plays simple guessing game . The computer picks a
random number from 0 to 100 , and the user tries to guess the number */
const int totchan = 7;
void main()
{
int number ; // computers rabdom number
int guess; // the users guess number
int chances=0,score=0,chanscor;// chanscor stores score for 1 successful chance
char ans;
do
{
clrscr();
chances=score=0;
printf("\n\t\t\t\t Welcome to the HIGH/LOW GAME ");
printf("\n\t\t\t\t I will pick a random number from 0 to 100 ");
printf("\n\t\t\t\t You must try to guess the number ");
randomize();
number =(int)(rand()%100);
chanscor=100/totchan;
do
{
printf("\n\t What is your GUESS ?? ");
scanf("%d",&guess);
if((guess<0) || (guess>100))
{
printf(" Sorry but your guess %d must be from 0 to 100 ");
}
else if (guess < number )
{
printf(" %d is LOW . Try a higher number ",guess);
}
else if(guess>number)
{
printf("%d is HIGH . Try a lower number ",guess);
}
else
{
printf("%d is correct . Congratulations !!!! ");
score = chanscor*(totchan-chances);
printf("\n\t Your score is %d \n",score);
break;
}
chances++;
if(guess!=number)
printf("\n\n Now you have %d chances left \n",totchan-chances);
if(chances == totchan)
{
printf("\n\n Only %d chances are allowed . Better luck next time",totchan);
printf("\n The actual number was %d \n",number);
break;
}
}while(guess!=number);
printf("\n\n Thank You for playing this game !!!!!!!!! ");
printf("\n\n Want to play it again ?(y/n)..");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y' || ans=='Y');
}
New Year Spl: The "CROSS & NOT ' Game
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
char matrix[3][3];
void cou(void);
void cou(void)
{
printf("\n\t\t 1 2 3 \n\n");
printf("\t\t 1 %c | %c | %c \n",matrix[0][0],matrix[0][1],matrix[0][2]);
printf("\t\t ---|---|---\n");
printf("\t\t 2 %c | %c | %c \n",matrix[1][0],matrix[1][1],matrix[1][2]);
printf("\t\t ---|---|---\n");
printf("\t\t 3 %c | %c | %c \n",matrix[2][0],matrix[2][1],matrix[2][2]);
}
void main()
{
int m,n;
int i,j,sum=0;
char ch='y';
while(ch=='Y' || ch=='y')
{
for(m=0;m<3;m++)
for(n=0;n<3;n++)
matrix[m][n]='\0';
while(sum<10)
{
if(sum==0)
cou();
printf(" Player1 is 'X' : choose the row and column \n");
printf(" Row : ");
scanf("%d",&i);
printf(" Column :L ");
scanf("%d",&j);
for(;i>3||i<1||j>3||j<1||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);)
{
printf(" Sorry but you gotta choose another place .\n");
printf(" ROW :");
scanf("%d",&i);
printf(" COLUMN : ");
scanf("%d",&j);
}
matrix[i-1][j-1]='X';
sum++;
cou(); // calling function to display game setup
//check if player 1 wins
if(matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(sum==9) // because there are only 9 boxes
{
printf(" The game is over and no one wins , haha lols \n");
break;
}
//player 2 's turn
printf(" Pkayer2 is 'O' : choose the row and column \n");
printf(" Row ");
scanf("%d",&i);
printf(" Column : ");
scanf("%d",&j);
for(;i>3||i<1||j>3||j<1||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);)
{
printf(" Sorry but you gotta choose another place .\n");
printf(" ROW :");
scanf("%d",&i);
printf(" COLUMN : ");
scanf("%d",&j);
}
matrix[i-1][j-1]='O';
sum++;
cou(); // calling function to display game setup
//check if player 1 wins
if(matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
printf(" \n Would you like to play again ??? (Y-N)\n");
scanf("%c",&ch);
}
system("PAUSE");
}
}
#include<stdio.h>
#include<stdlib.h>
char matrix[3][3];
void cou(void);
void cou(void)
{
printf("\n\t\t 1 2 3 \n\n");
printf("\t\t 1 %c | %c | %c \n",matrix[0][0],matrix[0][1],matrix[0][2]);
printf("\t\t ---|---|---\n");
printf("\t\t 2 %c | %c | %c \n",matrix[1][0],matrix[1][1],matrix[1][2]);
printf("\t\t ---|---|---\n");
printf("\t\t 3 %c | %c | %c \n",matrix[2][0],matrix[2][1],matrix[2][2]);
}
void main()
{
int m,n;
int i,j,sum=0;
char ch='y';
while(ch=='Y' || ch=='y')
{
for(m=0;m<3;m++)
for(n=0;n<3;n++)
matrix[m][n]='\0';
while(sum<10)
{
if(sum==0)
cou();
printf(" Player1 is 'X' : choose the row and column \n");
printf(" Row : ");
scanf("%d",&i);
printf(" Column :L ");
scanf("%d",&j);
for(;i>3||i<1||j>3||j<1||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);)
{
printf(" Sorry but you gotta choose another place .\n");
printf(" ROW :");
scanf("%d",&i);
printf(" COLUMN : ");
scanf("%d",&j);
}
matrix[i-1][j-1]='X';
sum++;
cou(); // calling function to display game setup
//check if player 1 wins
if(matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(sum==9) // because there are only 9 boxes
{
printf(" The game is over and no one wins , haha lols \n");
break;
}
//player 2 's turn
printf(" Pkayer2 is 'O' : choose the row and column \n");
printf(" Row ");
scanf("%d",&i);
printf(" Column : ");
scanf("%d",&j);
for(;i>3||i<1||j>3||j<1||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);)
{
printf(" Sorry but you gotta choose another place .\n");
printf(" ROW :");
scanf("%d",&i);
printf(" COLUMN : ");
scanf("%d",&j);
}
matrix[i-1][j-1]='O';
sum++;
cou(); // calling function to display game setup
//check if player 1 wins
if(matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
printf(" \n Would you like to play again ??? (Y-N)\n");
scanf("%c",&ch);
}
system("PAUSE");
}
}
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<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);
}
Subscribe to:
Posts (Atom)