问题补充:
@热风 2017/05/03 07:50:29
#include <iostream>
using namespace std;
struct BiTree
{
char data;
BiTree *lchild, *rchild;
};
void CreateBiTree(BiTree*&T)//创建二叉树
{
char ch;
cin >> ch;
if (ch == '#')
T = NULL;
else
{
T = new BiTree;
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void firstordertravel(BiTree *T)//先序遍历二叉树
{
if (T)
{
cout << T->data;
firstordertravel(T->lchild);
firstordertravel(T->rchild);
}
}
void Inordertravel(BiTree *T)//中序遍历二叉树
{
if (T)
{
Inordertravel(T->lchild);
cout << T->data;
Inordertravel(T->rchild);
}
}
void lastordertravel(BiTree *T)//后序遍历二叉树
{
if (T)
{
lastordertravel(T->lchild);
lastordertravel(T->rchild);
cout << T->data;
}
}
void Copy(BiTree *T1, BiTree*&T2)//复制二叉树
{
if (T1)
{
T2 = new BiTree;
T2->data = T1->data;
Copy(T1->lchild, T2->lchild);
Copy(T1->rchild, T2->rchild);
}
else
T2 = NULL;
}
int Depth(BiTree *T)//计算二叉树的深度
{
int m, n;
if (T == NULL) return 0;
else
{
m = Depth(T->lchild);
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;
}
void main()
{
BiTree *T1, *T2;
cout << "请先序输入T1二叉树:";
CreateBiTree(T1);
Copy(T1, T2);
cout << "先序遍历T1二叉树:";
firstordertravel(T1);
cout << endl;
cout << "中序遍历T1二叉树:";
Inordertravel(T1);
cout << endl;
cout << "后序遍历T1二叉树:";
lastordertravel(T1);
cout << endl;
cout << "先序遍历T2二叉树:";
firstordertravel(T2);
cout << endl;
cout << "T1的深度:" << Depth(T1) << endl;
cout << "T1的结点个数:" << NodeCount(T1) << endl;
}