主题:[讨论]二叉树中序遍历问题
书上没有源码,我写的这个不知哪里出了问题,帮忙看一下!不胜感激!
#include <stdio.h>
#include <stdlib.h>
struct tree
{
int node;
tree *left;
tree *right;
};
main()
{
int d;
tree *t1=NULL;
tree* addtree(int i);
void createtree(int i,tree **t);
void findtree(tree *t);
for (int i=0;i<10;i++)
{
createtree(i,&t1);
}
findtree(t1);
}
void findtree(tree *t)
{
if (t)
{
findtree(t->left);
printf("%d ",t->node);
findtree(t->right);
}
}
tree * addtree(int i)
{
tree *t;
t=(tree*)malloc(sizeof(tree));
t->left=NULL;
t->right=NULL;
t->node=i;
return t;
}
void createtree(int i,tree **t)
{
int q=0;
tree *t1;
t1=addtree(i);
if ((*t)==NULL)
(*t)=t1;
else
while(q!=1)
{
if (t1->node<(*t)->node)
{
if((*t)->left!=NULL)
{
(*t)=(*t)->left;
}
else {(*t)->left=t1;q=1;}
}
else
{
if ((*t)->right!=NULL)
{
(*t)=(*t)->right;
}
else {(*t)->right=t1;q=1;}
}
}
}
#include <stdio.h>
#include <stdlib.h>
struct tree
{
int node;
tree *left;
tree *right;
};
main()
{
int d;
tree *t1=NULL;
tree* addtree(int i);
void createtree(int i,tree **t);
void findtree(tree *t);
for (int i=0;i<10;i++)
{
createtree(i,&t1);
}
findtree(t1);
}
void findtree(tree *t)
{
if (t)
{
findtree(t->left);
printf("%d ",t->node);
findtree(t->right);
}
}
tree * addtree(int i)
{
tree *t;
t=(tree*)malloc(sizeof(tree));
t->left=NULL;
t->right=NULL;
t->node=i;
return t;
}
void createtree(int i,tree **t)
{
int q=0;
tree *t1;
t1=addtree(i);
if ((*t)==NULL)
(*t)=t1;
else
while(q!=1)
{
if (t1->node<(*t)->node)
{
if((*t)->left!=NULL)
{
(*t)=(*t)->left;
}
else {(*t)->left=t1;q=1;}
}
else
{
if ((*t)->right!=NULL)
{
(*t)=(*t)->right;
}
else {(*t)->right=t1;q=1;}
}
}
}