二叉树 前面先中后续输出正常 后面计算孩子就出错
#include<stdio.h>#include<stdlib.h>
typedef char DateType;
typedef struct Node
{
DateType date;
struct Node *lchild;
struct Node *rchild;
}BiTreeNode;
void PrintDate(DateType x)
{
printf("%c",x);
}
void CreatBiTreeNode(BiTreeNode **T)
{
char ch;
scanf("%c",&ch);
if(ch==' ')
{
*T=NULL;
}
else
{
if(!(*T=(BiTreeNode*)malloc(sizeof(BiTreeNode))))
{
printf("Overflow.\n");
exit(0);
}
(*T)->date=ch;
CreatBiTreeNode(&(*T)->lchild);
CreatBiTreeNode(&(*T)->rchild);
}
}
void PreOrder(BiTreeNode *T,void(*Visit)(DateType item))
{
if(T!=NULL)
{
Visit(T->date);
PreOrder(T->lchild,Visit);
PreOrder(T->rchild,Visit);
}
}
void InOrder(BiTreeNode *T,void(*Visit)(DateType item))
{
if(T!=NULL)
{
InOrder(T->lchild,Visit);
Visit(T->date);
InOrder(T->rchild,Visit);
}
}
void PostOrder(BiTreeNode *T,void(*Visit)(DateType item))
{
if(T!=NULL)
{
PostOrder(T->lchild,Visit);
PostOrder(T->rchild,Visit);
Visit(T->date);
}
}
int BTreeDepth(BiTreeNode *T)
{
int leftdep,rightdep;
if (T==NULL)
return 0;
else
{
leftdep=BTreeDepth(T->lchild);
rightdep=BTreeDepth(T->rchild);
if(leftdep>rightdep)
return (leftdep+1);
else
return (rightdep+1);
}
}
int NodeCount(BiTreeNode *T)
{
if(T=NULL)
return 0;
else
return(NodeCount(T->lchild)+NodeCount(T->rchild)+1);
}
int LeafCount(BiTreeNode *T)
{
if(T=NULL)
return 0;
else if ((T->lchild==NULL)&&(T->rchild==NULL))
return 1;
else
return(LeafCount(T->lchild)+LeafCount(T->rchild)+1);
}
int OneChildCount(BiTreeNode *T)
{
if(T=NULL)
return 0;
else
if((T->lchild==NULL)&&(T->rchild!=NULL)||(T->lchild!=NULL)&&(T->rchild==NULL))
return(OneChildCount(T->lchild)+OneChildCount(T->rchild)+1);
else
return(OneChildCount(T->lchild)+OneChildCount(T->rchild));
}
int TwoChildCount(BiTreeNode *T)
{
if(T=NULL)
return 0;
else
if((T->lchild==NULL)&&(T->rchild==NULL))
return(TwoChildCount(T->lchild)+TwoChildCount(T->rchild));
else
return(TwoChildCount(T->lchild)+TwoChildCount(T->rchild)+1);
}
void main()
{
int choice;
BiTreeNode *root;
printf("下面建立一颗二叉树,结点数据输入按先次序。\n");
printf("输入下面数据请注意,每一个非空结点的所有孩子数据都要给出。\n");
printf("若孩子为空,请用空格表示。\n");
printf("请输入二叉树结点的数据,这里设定为字符。\n");
CreatBiTreeNode(&root);
printf("******************************\n");
printf("1:先序序列:\n");
printf("2:中序序列:\n");
printf("3:后序序列:\n");
printf("4:二叉树的深(高)度:\n");
printf("5:二叉树的结点数:\n");
printf("6:二叉树的叶子结点数:\n");
printf("7:二叉树度为2 的结点数:\n");
printf("8:二叉树度为1 的结点数:\n");
printf("******************************\n");
do
{
printf("请输入对相应的操作符:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n先序序列为:");
PreOrder(root,PrintDate);
printf("\n");
break;
case 2:printf("\n中序序列为:");
InOrder(root,PrintDate);
printf("\n");
break;
case 3:printf("\n后序序列为:");
PostOrder(root,PrintDate);
printf("\n");
break;
case 4:printf("二叉树的深度是:%d\n",BTreeDepth(root));
break;
case 5:printf("二叉树的节点数是:%d\n",NodeCount(root));
break;
case 6:printf("二叉树的叶子树是:%d\n",LeafCount(root));
break;
case 7:printf("二叉树度为二的节点数是:%d\n",OneChildCount(root));
break;
case 8:printf("二叉树度为一的节点数是:%d\n",TwoChildCount(root));
break;
}
}while(choice>0&&choice<9);
}