#include "stdafx.h"
#include
using namespace std;
typedef char DataType;
typedef struct Node
{
DataType data; //数据域
struct Node * lchild;
struct Node * rchild; //结点的左右子数指针
}BTNode; //二叉树结点类型
//初始化空二叉树
void TreeInit(BTNode * &root);
//按照前序遍历序列建立二叉树
void CreateBTree_Pre(BTNode * &root, DataType Array[]);
//前序遍历二叉树
void PreOrder(BTNode * root);
//中序遍历二叉树
void InOrder(BTNode * root);
//后序遍历二叉树
void PostOrder(BTNode * root);
//计算二叉树的深度
int BTreeDepth(BTNode * root);
//释放二叉树中所有结点
void ClearBTree(BTNode * &root);
#include"stdafx.h"
#include"BiTree.h"
//初始化空二叉树
void TreeInit(BTNode * &root)
{
root = NULL;
}
//按照前序遍历序列建立二叉树
void CreateBTree_Pre(BTNode * &root, DataType Array[])
{
static int count = 0; //静态变量count
char item = Array[count]; //读取Array[]数组中的第count 元素
count++;
if (item == '#')
{
root = NULL;
}
else
{
root = new BTNode;
root->data = item;
CreateBTree_Pre(root->lchild, Array); //建立左子树
CreateBTree_Pre(root->rchild, Array); //建立右子树
}
}
//前序遍历二叉树
void PreOrder(BTNode * root)
{
if (root != NULL)
{
cout << root->data; //访问根结点
PreOrder(root->lchild); //前序遍历左子树
PreOrder(root->rchild); //前序遍历右子树
}
}
//中序遍历二叉树
void InOrder(BTNode * root)
{
if (root != NULL)
{
InOrder(root->lchild); //中序遍历左结点
cout << root->data; //访问根结点
InOrder(root->rchild); //中序遍历右子树
}
}
//后序遍历二叉树
void PostOrder(BTNode * root)
{
if (root != NULL)
{
PostOrder(root->lchild); //后序遍历左结点
PostOrder(root->rchild); //后序遍历右子树
cout << root->data; //访问根结点
}
}
//释放二叉树中所有结点
void ClearBTree(BTNode * &root)
{
if (root != NULL)
{
ClearBTree(root->lchild);
ClearBTree(root->rchild);
delete root;
root = NULL;
}
}
//计算二叉树的深度
int BTreeDepth(BTNode * root)
{
if (root == NULL)
{
return 0;
}
else
{
int depl = BTreeDepth(root->lchild);
int depr = BTreeDepth(root->rchild);
if (depl > depr)
{
return depl + 1;
}
else
{
return depr + 1;
}
}
#include "stdafx.h"
#include"BiTree.h"
int main()
{
BTNode *root;
DataType A[] = "ABD##E##CF#G###"; //以“#”补充空分支后的某个遍历序列
TreeInit(root); //初始化空二叉树
CreateBTree_Pre(root, A); //以前序遍历序列建立二叉树
cout << "中序遍历序列:";
InOrder(root);
cout << endl; //输出中序遍历树
cout << "后序遍历序列:";
PostOrder(root);
cout << endl; //输出后序遍历树
cout << "深度:" << BTreeDepth(root) << endl; //计算二叉树深度
system("pause");
return 0;
}
ok.清楚了我们要做什么以后,开始动手!
//按照后序遍历序列建立二叉树 --error
void CreateBTree_Post(BTNode * &root, DataType Array[])
{
static int count = 0; //静态变量count
char item = Array[count]; //读取Array[]数组中的第count 元素
count++;
if (item == '#')
{
root = NULL;
}
else
{
CreateBTree_Post(root->lchild, Array); //建立左子树
CreateBTree_Post(root->rchild, Array); //建立右子树
root = new BTNode;
root->data = item;
}
}
//按照后序遍历序列建立二叉树
void CreateBTree_Post2(BTNode * &root, DataType Array[])
{
static int count = strlen(Array); //静态变量count
char item = Array[count - 1]; //读取Array[]数组中的第count 元素
count--;
if (item == '#')
{
root = NULL;
}
else
{
root = new BTNode;
root->data = item;
CreateBTree_Post2(root->rchild, Array); //建立右子树
CreateBTree_Post2(root->lchild, Array); //建立左子树
}
}
int main()
{
BTNode *root;
TreeInit(root); //初始化空二叉树
DataType A[] = "##C##DB##EA"; //以“#”补充空分支后的某个遍历序列
CreateBTree_Post2(root, A); //以前序遍历序列建立二叉树
cout << "前序遍历序列:";
PreOrder(root);
cout << endl; //输出前序遍历树
cout << "中序遍历序列:";
InOrder(root);
cout << endl; //输出中序遍历树
cout << "深度:" << BTreeDepth(root) << endl; //计算二叉树深度
system("pause");
return 0;
}