ツリーの深さ、ノードの数を計算します.
1009 ワード
ツリーの深さ、ノードの数を計算します.
// 5.5 , 。
#include
using namespace std;
//
typedef struct BiNode
{
char data; //
struct BiNode *lchild,*rchild; //
}BiTNode,*BiTree;
// 5.3
void CreateBiTree(BiTree &T)
{
// ( ), T
char ch;
cin >> ch;
if(ch=='#') T=NULL; // ,
else{
T=new BiTNode;
T->data=ch; //
CreateBiTree(T->lchild); //
CreateBiTree(T->rchild); //
} //else
} //CreateBiTree
int Depth(BiTree T)
{
int m,n;
if(T == NULL ) return 0; // , 0,
else
{
m=Depth(T->lchild); // m
n=Depth(T->rchild); // n
if(m>n) return(m+1); // m n 1
else return (n+1);
}
}
int NodeCount(BiTree T)
{
if(T==NULL)return 0;
else return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
}
void main()
{
BiTree tree;
cout<