# Thread: binary tree solution-- help needed

1. ## binary tree solution-- help needed

respected sir,

this is a non recursive solution for a binary tree.but the code
seems to have some problem,
even after debugging i am not able to find out the problem.
the problem is

while printing the elements only the root is getting printed,the other elements are not getting printed.
i would be greatful if anyone could suggest a solution.

the code
Code:
```#include<stdio.h>
struct btree
{
int data;
struct btree *right;
struct btree *left;
};
void main()
{
char ch;
struct btree *temp,*start,*p;
struct btree *disp;
start = temp = p = NULL;

do
{
p=(struct btree *)malloc(sizeof(struct btree));/* new node */

printf("ENTER THE NODE VALUE  ");
scanf("%d",&(p->data));
p->left = NULL;
p->right = NULL;

if (start == NULL)		/* for the very first time */
{
start = p;
printf("ROOT NODE IS  %d",start->data);
}
else
{
temp = start;
while(1 && temp)   /* traversing till the end of left and right pointers */
{

if ((p->data) < ((temp->data) && (temp->left != NULL)))
{
temp = temp->left;
}
else if ((p->data) >= ((temp->data) && ( temp->right != NULL)))
{
temp= temp->right;

}
else
{
break;
}
}
}

/* inserting the node in the below statements */
temp=(struct btree *)malloc(sizeof(struct btree));
if (p->data < temp->data)
{
temp->left = p;
}
else
{
temp->right = p;

}

printf("The elements of the tree \n");
for(disp=start;disp != NULL;disp=disp->right)
{
printf("%d\n",disp->data); /* printing elements */
}

printf("type y if  u want to continue ");
scanf("%s",&ch);
}while(ch=='y');

}```

2. Well as I suspected your problem was with the way you were inserting new leaves into the tree. Although the way you were printing leaves is non-recursive it also doesn't fully print out the tree. But that sounds like a different problem that I think you can fix yourself. Here is some working code:

Code:
```#include<stdio.h>
struct btree
{
int data;
struct btree *right;
struct btree *left;
};

int main(void)
{
char ch, done;
struct btree *temp,*start,*p;
struct btree *disp;
start = temp = p = NULL;

do {
p=(struct btree *)malloc(sizeof(struct btree));/* new node */
done = 0;

printf("ENTER THE NODE VALUE  ");
scanf("%d",&(p->data));
p->left = NULL;
p->right = NULL;

if (start == NULL) {	/* for the very first time */
start = p;
printf("ROOT NODE IS  %d",start->data);
} else {
temp = start;
while(!done) {   /* traversing till the end of left and right pointers */
if ((p->data) < ((temp->data))) {
if(temp->left)
temp = temp->left;
else {
temp->left = p;
done = 1;
}
} else if ((p->data) >= ((temp->data))) {
if(temp->right)
temp = temp->right;
else {
temp->right = p;
done = 1;
}
} else {
break;
}
}
}

printf("The elements of the tree \n");
for(disp=start->left;disp != NULL;disp=disp->left) {
printf("%d\n",disp->data); /* printing elements */
}
printf("%d\n", start->data);
for(disp=start->right;disp != NULL;disp=disp->right) {
printf("%d\n",disp->data); /* printing elements */
}

printf("type y if  u want to continue ");
scanf("%s",&ch);
} while(ch=='y');
}```

3. Are you sure about those contitionals below 'while (1 && temp)' (hmm.)? What about

if ((p->data < temp->data) && (temp->left != NULL))

...

if ((p->data) < ((temp->data) && (temp->left != NULL)))

...

?

I'm not sure, but it might be the thing, otherwise i'd recomend you debugging it again )

4. Actually those didn't even contribute to the problem although lobo is certainly correct that the syntax was a little suspect. Real quick, (assuming you are a newbie, sanju) statements like

Code:
`while(1 && temp)`
do not technically interfere with any loop. However what you are saying is:

"while one is one and while temp is not equal to zero"

Since one is always going to be one it is safe to puts your loops like this

Code:
```while(temp)
//or alternatively
while(temp != NULL)```