# Comparision of binary trees, balance.

Show 80 post(s) from this thread on one page
Page 3 of 3 First 123
• 02-18-2012
anduril462
No, the return type should be struct tree *:
Code:

```struct tree *DSWbalance(struct tree *root) {     /* algorithm */     return root;  // this is the "new" root, since rotations may alter the root of the tree }```
• 02-19-2012
blunt
Ohhh god, I'm so stupid. I've never considered that function can be something else than void or int :o

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;     }```
Show 80 post(s) from this thread on one page
Page 3 of 3 First 123