唯一的确定一棵二叉树
如果给出了遍历二叉树的前(后)序序列和中序序列,则可以构造出唯一的一棵二叉树。试编写实现上述功能的程序。
- 构造一棵二叉树
- 证明构造正确(即分别以前序和中序遍历该树,将得到的结果与给出的序列进行比较)
- 对该二叉树进行后序遍历,输出后序遍历序列
- 用凹入法输出该二叉树
BinTree.h
#include<iostream>
using namespace std;
struct BinTreeNode
{
char data;
BinTreeNode *leftChild, *rightChild;
BinTreeNode() :leftChild(NULL), rightChild(NULL){}
BinTreeNode(char x, BinTreeNode*l = NULL, BinTreeNode*r = NULL)
:data(x),leftChild(l),rightChild(r){}
};
class BinTree
{
public:
BinTreeNode * root;
BinTree() :root(NULL) {};
void InOrder(BinTreeNode*subtree);
void PreOrder(BinTreeNode*subtree);
void PosOrder(BinTreeNode*subtree);
BinTreeNode*pre_mid_createBinTree(char* pre, char *ord, int n);
BinTreeNode*pos_mid_createBinTree(char* pos, char *ord, int n);
void PrintBinTree(BinTreeNode*&t, int level);
};
void BinTree::PrintBinTree(BinTreeNode*&t, int level)
{
if (t == NULL)return;
PrintBinTree(t->rightChild, level + 1);
for (int i = 0; i < 4* (level - 1); i++)cout << " ";
cout << t->data << endl;
PrintBinTree(t->leftChild, level + 1);
}
void BinTree::PreOrder(BinTreeNode*subtree)
{
if (subtree != NULL) {
cout << subtree->data;
PreOrder(subtree->leftChild);
PreOrder(subtree->rightChild);
}
}
void BinTree::InOrder(BinTreeNode*subtree)
{
if (subtree != NULL) {
InOrder(subtree->leftChild);
cout << subtree->data;
InOrder(subtree->rightChild);
}
}
void BinTree::PosOrder(BinTreeNode*subtree)
{
if (subtree != NULL) {
PosOrder(subtree->leftChild);
PosOrder(subtree->rightChild);
cout << subtree->data;
}
}
BinTreeNode* BinTree::pre_mid_createBinTree(char* pre, char *ord, int n)
{
if (n<=0)return NULL;
int k = 0;
char ch = pre[0];
while(ord[k]!=ch)k++;//寻找中序遍历的根结点
BinTreeNode*t = new BinTreeNode(ch);
//分隔左右孩子
t->leftChild = pre_mid_createBinTree(pre + 1, ord, k);
t->rightChild = pre_mid_createBinTree(pre + k + 1, ord + k + 1, n - k - 1);
return t;
}
BinTreeNode* BinTree::pos_mid_createBinTree(char* pos, char *ord, int n)
{
if (n <= 0)return NULL;
char ch = pos[n - 1];
int k = 0;
while (ord[k]!=ch)
{
k++;
}
BinTreeNode*t = new BinTreeNode(ch);
//分隔左右孩子
t->leftChild = pos_mid_createBinTree(pos, ord, k);
t->rightChild = pos_mid_createBinTree(pos+k, ord + k + 1, n - k - 1);
return t;
}
cpp调用文件
#include<iostream>
#include<cstring>
#include"BinTree.h"
using namespace std;
char pre[100], ord[100], pos[100];
int main()
{
cout << "依次输入前序遍历和中序遍历的序列:" << endl;
cin >> pre >> ord;
BinTree tree,tree1;
tree.root=tree.pre_mid_createBinTree(pre, ord, strlen(pre));
cout << "****************************************************" << endl;
cout << "建立之后的树的遍历如下:" << endl;
cout << " 前序遍历:" << endl;
tree.PreOrder(tree.root);
cout << endl;
cout << " 中序遍历:" << endl;
tree.InOrder(tree.root);
cout << endl;
cout << " 后序遍历:" << endl;
tree.PosOrder(tree.root);
cout << endl;
cout << "打印二叉树:" << endl;
tree.PrintBinTree(tree.root, 1);
cout << "****************************************************" << endl;
cout << "依次输入中序遍历和后序遍历的序列:" << endl;
cin >> ord >> pos;
tree1.root = tree1.pos_mid_createBinTree(pos, ord, strlen(pos));
cout << "****************************************************" << endl;
cout << "建立之后的树的遍历如下:" << endl;
cout << " 前序遍历:" << endl;
tree1.PreOrder(tree1.root);
cout << endl;
cout << " 中序遍历:" << endl;
tree1.InOrder(tree1.root);
cout << endl;
cout << " 后序遍历:" << endl;
tree1.PosOrder(tree1.root);
cout << endl;
cout << "打印二叉树:" << endl;
tree1.PrintBinTree(tree1.root, 1);
cout << "****************************************************" << endl;
system("pause");
return 0;
}