#include <iostream>
using namespace std;
#include "Status.h"
typedef char TElemType;
#include "BiTree.h"
int main()
{
BiTree T;
CreateBiTree(T);
cout<<"二叉树的深度为:"<<Depth(T)<<endl;
cout<<"二叉树中结点个数为:"<<NodeCount(T)<<endl;
cout<<"二叉树中叶子结点个数为:"<<LeavesNodeCount(T)<<endl;
cout<<"先序遍历:"; PreOrderTraverse(T); cout<<"\n中序遍历:";
InOrderTraverse(T);
cout<<"\n后序遍历:";
PostOrderTraverse(T);
cout<<endl;
return 0;
}
---------*****Status.h******-------------
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
---------*****BiTree.h******--------------
typedef struct BiTNode{
TElemType data; //结点数据域
struct BiTNode *lchild,*rchild; //左右孩子指针
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T)
{
char ch;
cin>>ch;
if(ch==‘#‘) T=NULL;
else{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int Depth(BiTree T)
{
if(T==NULL) return 0;
else {
int m=Depth(T->lchild);
int n=Depth(T->rchild);
if(m>n) return m+1;
else return n+1;
}
}
int NodeCount(BiTree T)
{
if(T==NULL) return 0;
else return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
}
int LeavesNodeCount(BiTree T)
{
if(!T) return 0;
else if(!T->lchild&&!T->rchild) return 1;
else return LeavesNodeCount(T->lchild)+LeavesNodeCount(T->rchild);
}
void PreOrderTraverse(BiTree T)
{
if(T) { cout<<T->data; PreOrderTraverse(T->lchild); PreOrderTraverse(T->rchild); }
}
void InOrderTraverse(BiTree T)
{
if(T) { InOrderTraverse(T->lchild); cout<<T->data; InOrderTraverse(T->rchild); }
}
void PostOrderTraverse(BiTree T)
{
if(T) { PostOrderTraverse(T->lchild); PostOrderTraverse(T->rchild); cout<<T->data; }
}