# Thread: binary tree problem - help needed

1. ## binary tree problem - help needed

respected sir,

i would be greatful if anyone could help me with my non recursive
binary tree problem.actually i had already got some help from u.thanks for that.

the problem is when u give data in either order it gets printed
meaning acending or descending ie either on left side or right side
but if u give in between values the value does not get printed.
like for example

10,15,20,13
here 13 is inbetween it does not get printed.
13 does not get printed, the remaining elements
get printed.

can anyone tell me what's wrong with the code,
one problem i found is
in if (temp->right) and the else at times
both get satisfied is that the problem.
is that the problem ...??? i am attaching my code

i would be greatful if anyone could point out the problem.

thanks

sanju

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');
}```

2. This will be of no help whatsoever, but what is a binary tree?

3. Well, if you insert those number in a regular binary tree, it will look something like this
Code:
```    10
\
15
/  \
13    20```
Follow your loops and see if you think you are inserting and printing correctly (you're definately not printing correctly, I haven't followed the insertion process.)

What is a binary tree... see here. Also see here.

4. What is a binary tree... see here. Also see here.
Thanks Hammer!

5. ## thanks any way

respected sir
thanks any way for the guidance,

i think i am able to figure out the problem.
bye

sanju