Comparision of binary trees, balance.
Hi! I got this little assignment on fundamentals of computer programming. Task is about creating two binary trees (simple binary tree and balanced one) and comparision of it. I've got a whole code. Everything went perfect until I added balance function.
Code:
#include <stdio.h>#include <minmax.h>
struct tree
{
int val;
struct tree *left;
struct tree *right;
};
typedef struct tree one;
typedef struct tree two;
int insert(one **, int);
//void inserttwo(two **, int);
int find(one *, int);
int height(one *);
int leftbalance(two *);
int rightbalance(two *);
void DSWbalance(two *);
int main()
{
int choice, value, val, c, d;
one *headone;
two *headtwo;
headone=(one*)malloc(sizeof(one));
headtwo=(two*)malloc(sizeof(two));
printf("Making a tree. Enter a value:\n");
scanf("%d",&val);
headone->val=val;
headtwo->val=val;
headone->left=NULL;
headone->right=NULL;
headtwo->left=NULL;
headtwo->right=NULL;
do
{
system("cls");
printf("1. Enter a value.\n2. Find height.\n3. Find element.\n4. Rotate left.\n5. Rotate right.\n6. Balance.\n9. EXIT.\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
system("cls");
printf("Enter a value: ");
scanf("%d", &value);
if(insert(&headone, value)==1)
{
printf("Value exists! Press any key to continue.\n");
}
else
{
insert(&headtwo, value);
DSWbalance(headtwo);
printf("Done. Press any key to continue.");
}
getchar();
getchar();
break;
case 2:
system("cls");
printf("Heigh of a binary tree is %d and balanced one %d",height(headone),height(headtwo));
getchar();
getchar();
break;
case 3:
system("cls");
printf("Enter a value: ");
scanf("%d", &value);
printf("found in a first tree with %d comparision.\n",find(headone, value));
printf("found in a second tree with %d comparision.",find(headtwo, value));
getchar();
getchar();
break;
case 4:
system("cls");
leftrotate(headtwo);
case 5:
system("cls");
rightrotate(headtwo);
case 6:
system("cls");
DSWbalance(headtwo);
default:
break;
}
}while(choice!=9);
}
int insert(one **p, int x)
{
static int d;
d=0;
if((*p)==NULL)
{
(*p)=(one*)malloc(sizeof(one));
(*p)->val=x;
(*p)->left=NULL;
(*p)->right=NULL;
}
else
{
if((*p)->val>x)
insert(&(*p)->left, x);
else if((*p)->val<x)
insert(&(*p)->right, x);
else
{
d=1;
}
}
return d;
}
int find(one *p, int x)
{
int c=0;
while(p!=NULL)
{
if(p->val==x)
{
printf("Element %d ",x);
c++;
return c;
getchar();
}
else if(p->val<x)
{
p=p->right;
c++;
}
else
{
p=p->left;
c++;
}
}
return c;
}
/*AVL*/
int height(two *p)
{
if(p==0)
return 0;
return 1 + max(height(p->left),height(p->right));
}
int leftrotate(two *p)
{
struct tree *nd;
int data;
if(p==0 || p->right==0)
return 0; //no right node
nd=p->right;
p->right=nd->right; //move node
nd->right=nd->left;
nd->left=p->left;
p->left=nd;
data=p->val;
p->val=nd->val;
nd->val=data;
return 1;
}
int rightrotate(two *p)
{
struct tree *nd;
int data;
if(p==0 || p->left==0)
return 0; //no left node
nd=p->left;
p->left=nd->left;
nd->left=nd->right;
nd->right=p->right;
p->right=nd;
data=p->val;
p->val=nd->val;
nd->val=data;
return 1;
}
void DSWbalance(two *p)
{
struct tree *q;
int i,k,nodecount;
for(q=p,nodecount=0;q!=0;q=q->right,++nodecount) //spine
while(rightrotate(p)==1)
for(i=nodecount/2;i>0;i/=2)
for(q=p,k=0;k<i;++k,q=q->right) //balance
leftrotate(p);
}
The funny thing is that neither left nor right rotation works now, they did before I added balance, what's more, when I hide balance in comment /* */ rotation works again.
Can someone point my mistakes?