#include<iostream>
#include<stdio.h>
using namespace std;
class BitNode
{
public:
char data;
BitNode * lchild;
BitNode * rchild;
};
class BitTree
{
private:
BitNode * pBase;
public:
BitTree()
{
CreateByPreOrder(pBase);
}
void show()
{
cout<<"前序遍历:"<<endl;
PreOrderTraverse(pBase);
cout<<endl<<"中序遍历:"<<endl;
InOrderTraverse(pBase);
cout<<endl<<"后序遍历:"<<endl;
BackOrderTraverse(pBase);
cout<<endl<<"深度为:"<<getTreeDeep(pBase)<<endl;
}
private:
void CreateByPreOrder(BitNode * &pB)
{
char ch;
if((ch=getchar())=='#')
{
pB=NULL;
}
else
{
pB=new BitNode;
pB->data=ch;
CreateByPreOrder(pB->lchild);
CreateByPreOrder(pB->rchild);
}
}
void PreOrderTraverse(BitNode * &pB)
{
if(pB)
{
cout<<pB->data;
PreOrderTraverse(pB->lchild);
PreOrderTraverse(pB->rchild);
}
}
void InOrderTraverse(BitNode * &pB)
{
if(pB)
{
InOrderTraverse(pB->lchild);
cout<<pB->data;
InOrderTraverse(pB->rchild);
}
}
void BackOrderTraverse(BitNode * &pB)
{
if(pB)
{
BackOrderTraverse(pB->lchild);
BackOrderTraverse(pB->rchild);
cout<<pB->data;
}
}
int getTreeDeep(BitNode * &pB)
{
int deep=0;
if(pB)
{
int lchildDeep=getTreeDeep(pB->lchild);
int rchildDeep=getTreeDeep(pB->rchild);
deep=lchildDeep>=rchildDeep?lchildDeep+1:rchildDeep+1;
}
return deep;
}
};
int main()
{
BitTree bt;
bt.show();
return 0;
}