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