Ohhh god, I'm so stupid. I've never considered that function can be something else than void or int
Although not everything is okey.
Code:
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");
getchar();
getchar();
}
else
{
insert(&headtwo, value);
headtwo=DSWbalance(headtwo); //when this part is 'on' program doesn't give my errors, but it looks like algorithm is wrong, when I print a result of it I have only one element in a tree
}
break;
If red part is off, and I put into my tree 5 values, ie 5, 4, 3, 2, 1, then I perform balancing I have error of access violation. I checked step-by-step what's going on inside of it. First part (creating a spine) looks okey. Not so sure of second part. Problem occurs when 'k' reaches 'i'.
Code:
struct tree *DSWbalance(struct tree *root){
struct tree *p;
int nodecount;
int i;
for(p=root,nodecount=0;p!=0;p=p->right,++nodecount) //spine
{
while(rightrotate(&p)==1) { }
}
for(i=nodecount/2; i>0; i/=2 ) //balance
{
int k;
for(p=root,k=0;k<i;++k,p=p->right)
leftrotate(&p);
}
return root;
}