#include
using namespace std;
typedef struct BinaryTree
{
char data;
struct BinaryTree *lchild,*rchild;
}BinaryTree,*BiTree;
void CreateBiTree(BiTree &T)
{
char z;
if((z=getchar())==' ')
T=NULL;
else
{
T=(BinaryTree*)malloc(sizeof(BinaryTree));
T->data=z;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
//先序遍历
void preBiTree(BiTree T)
{
if(T!=NULL)
{
cout<
preBiTree(T->lchild);
preBiTree(T->rchild);
}
}
//中序遍历
void InBiTree(BiTree T)
{
if(T!=NULL)
{
InBiTree(T->lchild);
cout<
InBiTree(T->rchild);
}
}
//后序遍历
void PostBiTree(BiTree T)
{
if(T!=NULL)
{
PostBiTree(T->lchild);
PostBiTree(T->rchild);
cout<
}
}
int Depth(BiTree T)
{
if(T==NULL)
{
return 0;
}
else
return 1+(Depth(T->lchild)>Depth(T->rchild)? Depth(T->lchild):Depth(T->rchild));
}
void main()
{
BiTree T;
cout<<"请输入相应二叉树:"<
cout<悄滚腔<"二叉树的先序遍历为:"<
cout<
cout<
cout<
#include
#include
#include
int count=0;
typedef struct BiTNode {
int data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
void CreateBiTree(BiTree &T){
int ch;
scanf("%d",&ch);
if(ch==0)T=NULL;
else{
if(!(T=(BiTNode * )malloc(sizeof(BiTNode)))) exit(-1);
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int PreOrder(BiTree T)
{
if (!T) return 0;
printf("%d ",T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
return 1;
}
int InOrder(BiTree T)
{
if (!T) return 0;
InOrder(T->lchild);
printf("%d ",T->data);
InOrder(T->rchild);
return 1;
}
int PostOrder(BiTree T)
{
if (!T) return 0;
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%d ",T->data);
return 1;
}
int CountLeaf (BiTree T){
if (!T ) return 0;
if (!T->lchild && !T->rchild) return 1;
int m;
int n;
m = CountLeaf( T->lchild);
n = CountLeaf( T->rchild);
return (m+n);
}
void main(){
int a;
BiTree T;
CreateBiTree(T);
printf("先岁启序乎祥如遍历:");
PreOrder(T);
printf("中序遍历:");
InOrder(T);
printf("后序遍历:");
PostOrder(T);
a=CountLeaf(T);
printf("叶子节宴雀点个数:");
printf("%d",a);
}