#include
#include
using namespace std;
#define ElemType char
struct MyStruct
{
ElemType data;
MyStruct* left;
MyStruct* right;
};
void creat(MyStruct* &tree)//建立二叉樹
{
MyStruct* k;
char c;
cin >> c;
if (c != '#')//如果不為'#'則放入
{
tree = (MyStruct*)malloc(sizeof(MyStruct));
tree->data = c;
creat(tree->left);//左遞歸
creat(tree->right);//右遞歸
}
else//為’#’說明結束,置空
{
tree = NULL;
}
}
void pre(MyStruct* tree)//先序輸出
{
if (tree)
{
cout << tree->data << " ";
pre(tree->left);
pre(tree->right);
}
}
void mid(MyStruct* tree)//中序輸出
{
if (tree)
{
mid(tree->left);
cout << tree->data << " ";
mid(tree->right);
}
}
void last(MyStruct* tree)//後序輸出
{
if (tree)
{
last(tree->left);
last(tree->right);
cout << tree->data << " ";
}
}
int treecnt(MyStruct* tree)
{
int cnt;
if(tree==NULL)
return 0;
if(tree->left==NULL&&tree->right==NULL)
return 1;
cnt=treecnt(tree->left)+treecnt(tree->right);
}
int treedeep(MyStruct* tree)
{
int m,n;
if(tree==NULL)
return 0;
else
{
m=treedeep(tree->left);
n=treedeep(tree->right);
if(m>n)
return (m+1);
else
return (n+1);
}
}
int main()
{
MyStruct *tree;
cout<
creat(tree);
cout << "建立完成" << endl;
cout << "前序周遊" << endl;
pre(tree);
cout << endl;
cout << "中序周遊" << endl;
mid(tree);
cout << endl;
cout << "後序周遊" << endl;
last(tree);
cout << endl;
int cnt=treecnt(tree);
cout<
int deep=treedeep(tree);
cout<
cout << "結束" << endl;
system("pause");
return 0;
}