my following inorder traversal program is:
#include<stdio.h>
struct node
{
int info;
struct node *left;
struct node *right;
};
typedef struct node *nodeptr;
nodeptr getnode()
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node *));
return p;
}
nodeptr maketree(int x)
{
nodeptr p;
p=getnode();
p->info=x;
p->left=NULL;
p->right=NULL;
return p;
}
void setleft(nodeptr p,int x)
{
if(p==NULL)
printf("void insertion");
else if(p->left!=NULL)
printf("invalid insertion");
else
p->left=maketree(x);
}
void setright(nodeptr p,int x)
{
if(p==NULL)
printf("void insertion");
else if(p->right!=NULL)
printf("invalid insertion");
else
p->right=maketree(x);
}
void intrav(nodeptr ptree)
{
if(ptree!=NULL)
{
intrav(ptree->left);
printf("%d\n",ptree->info);
intrav(ptree->right);
}
}
int main()
{
nodeptr tree;
nodeptr p,q;
int n;
scanf("%d",&n);
tree=maketree(n);
while(scanf("%d",&n)!=EOF)
{
p=q=tree;
while(n!=p->info&&q!=NULL)
{
p=q;
if(n<p->info)
q=p->left;
else
q=p->right;
}
if(n<p->info)
setleft(p,n);
else
setright(p,n);
}
intrav(tree);
return 0;
}
the output is not coming correct
can yu suggest me the errors
Use the CODE tag and make sure indentation looks properly. Noone will bother to try to read in when on it when it looks the way it does right now. D;
Last edited on