#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
typedef struct node
{ DataType data;
struct node *lchild, *rchild;
} BinTNode;
typedef BinTNode *BinTree;
void main()
{
void CreateBinTree(BinTree *T);
void Preorder(BinTree T);
void Inorder(BinTree T);
void Postorder(BinTree T);
int nodes(BinTree T,int n);
int leafs(BinTree T,int n);
BinTree swap(BinTree T);
BinTree T;
int n =0;
printf("qing shu ru xian xu xu lie (xu jie dian yong kong ge biao shi)\n");
CreateBinTree(&T);
printf("\n");
printf("qian xu bian li\n");
Preorder(T);
printf("\n");
printf("zhong xu bian li\n");
Inorder(T);
printf("\n");
printf("hou xu bian li\n");
Postorder(T);
printf("\n");
printf("er cha shu de zong jie dian wei %d\n",nodes(T,n));
printf("er cha shu de ye zi zong shu wei %d\n",leafs(T,n));
T=swap(T);
printf("\n");
printf("jiao huan hou de zhong xu bian li wei\n");
Inorder(T);
getch();
}
void CreateBinTree(BinTree *T)
{
char ch;
ch=getchar();
if(ch==' ')
*T=NULL;
else
{
*T=(BinTNode *)malloc(sizeof(BinTNode));
(*T)->data=ch;
CreateBinTree(&(*T)->lchild);
CreateBinTree(&(*T)->rchild);
}
}
void Preorder(BinTree T)
{
if(T)
{
printf("%c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}
void Inorder(BinTree T)
{
if(T)
{
Inorder(T->lchild);
printf("%c ",T->data);
Inorder(T->rchild);
}
}
void Postorder(BinTree T)
{
if(T)
{
Postorder(T->lchild);
Postorder(T->rchild);
printf("%c ",T->data);
}
}
int nodes(BinTree T,int n)
{n=0;
if(T)
n++;
n=nodes(T->lchild,n) ;
n=nodes(T->rchild,n) ;
return n;
}
int leafs(BinTree T,int n)
{ n=0;
if(T)
{ if(T->lchild && T->data==NULL)
{
n++;
n=leafs(T->lchild,n);
n=leafs(T->rchild,n);
}
}
}
BinTree swap(BinTree T)
{
BinTNode *p;
if(T)
{
p=T->lchild;
T->lchild=T->rchild;
T->rchild=p;
swap( T->lchild);
swap(T->rchild) ;
}
return T;
}
运行后 打上一串数回车就没反应了 高手指教下啊