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