主题:关于二叉树输出不正确的问题
请大家帮我看看为什么二叉树输出度为1的结点和度为2的结点不正确,先谢谢了.
#include<stdio.h>
#include<malloc.h>
typedef int DataType;
typedef struct node
{
DataType data;
struct node *lchild,*rchild;
}bitree;
int count_0=0;
int count_1=0;
int count_2=0;
bitree *Creat_preorder();
void preorder_0( bitree *tree);
void preorder_1( bitree *tree);
void preorder_2( bitree *tree);
void preorder(bitree *tree);
void main()
{
bitree *tree;
tree=Creat_preorder();
printf("此二叉树的前序遍历顺序为:\n");
preorder(tree);
printf("\n其中0度结点为:");
preorder_0(tree);
printf("\n其中0度结点数目为:%d",count_0);
printf("\n其中1度结点为:");
preorder_1(tree);
printf("\n其中1度结点数目为:%d",count_1);
printf("\n其中2度结点为:");
preorder_2(tree);
printf("\n其中2度结点数目为:%d",count_2);
printf("\n");
}
bitree *Creat_preorder()
{
bitree *t;
DataType x;
printf("请输入一个数以0结尾");
scanf("%d",&x);
if(x==0) t=NULL;
else
{
t=(bitree *)malloc(sizeof(bitree));
t->data=x;
t->lchild=Creat_preorder();
t->rchild=Creat_preorder();
}
return (t);
}
void preorder(bitree *tree)
{
if(tree!=NULL)
{
printf("%3d",tree->data);
preorder(tree->lchild);
preorder(tree->rchild);
}
}
void preorder_0( bitree *tree)
{
if(tree!=NULL)
{
if(tree->lchild==NULL&&tree->rchild==NULL)
{
count_0++;
printf("%3d",tree->data);
}
preorder_0(tree->lchild);
preorder_0(tree->rchild);
}
}
void preorder_1(bitree *tree)
{
if(tree!=NULL)
{
if((tree->lchild==NULL&&tree->rchild!=NULL)||(tree->lchild!=NULL&&tree->rchild==NULL))
{
count_1++;
printf("%3d",tree->data);
}
preorder_1(tree->lchild);
preorder_1(tree->rchild);
}
}
void preorder_2( bitree *tree)
{
if(tree!=NULL)
{
if(tree->lchild!=NULL&&tree->rchild!=NULL)
{
count_2++;
printf("%3d",tree->data);
}
preorder_2(tree->lchild);
preorder_2(tree->rchild);
}
}
#include<stdio.h>
#include<malloc.h>
typedef int DataType;
typedef struct node
{
DataType data;
struct node *lchild,*rchild;
}bitree;
int count_0=0;
int count_1=0;
int count_2=0;
bitree *Creat_preorder();
void preorder_0( bitree *tree);
void preorder_1( bitree *tree);
void preorder_2( bitree *tree);
void preorder(bitree *tree);
void main()
{
bitree *tree;
tree=Creat_preorder();
printf("此二叉树的前序遍历顺序为:\n");
preorder(tree);
printf("\n其中0度结点为:");
preorder_0(tree);
printf("\n其中0度结点数目为:%d",count_0);
printf("\n其中1度结点为:");
preorder_1(tree);
printf("\n其中1度结点数目为:%d",count_1);
printf("\n其中2度结点为:");
preorder_2(tree);
printf("\n其中2度结点数目为:%d",count_2);
printf("\n");
}
bitree *Creat_preorder()
{
bitree *t;
DataType x;
printf("请输入一个数以0结尾");
scanf("%d",&x);
if(x==0) t=NULL;
else
{
t=(bitree *)malloc(sizeof(bitree));
t->data=x;
t->lchild=Creat_preorder();
t->rchild=Creat_preorder();
}
return (t);
}
void preorder(bitree *tree)
{
if(tree!=NULL)
{
printf("%3d",tree->data);
preorder(tree->lchild);
preorder(tree->rchild);
}
}
void preorder_0( bitree *tree)
{
if(tree!=NULL)
{
if(tree->lchild==NULL&&tree->rchild==NULL)
{
count_0++;
printf("%3d",tree->data);
}
preorder_0(tree->lchild);
preorder_0(tree->rchild);
}
}
void preorder_1(bitree *tree)
{
if(tree!=NULL)
{
if((tree->lchild==NULL&&tree->rchild!=NULL)||(tree->lchild!=NULL&&tree->rchild==NULL))
{
count_1++;
printf("%3d",tree->data);
}
preorder_1(tree->lchild);
preorder_1(tree->rchild);
}
}
void preorder_2( bitree *tree)
{
if(tree!=NULL)
{
if(tree->lchild!=NULL&&tree->rchild!=NULL)
{
count_2++;
printf("%3d",tree->data);
}
preorder_2(tree->lchild);
preorder_2(tree->rchild);
}
}