如果给出了遍历二叉树的前序序列和中序序列,则可以构造出唯一的一棵二叉
树。试编写实现上述功能的程序。
已知一棵二叉树的前序和中序序列,试设计完成下列任务的一个算法:
(1)构造一棵二叉树;
(2)证明构造正确(即分别以前序和中序遍历该树,将得到的结果与给出的序列
进行比较)。
(3)对该二叉树进行后序遍历,输出后序遍历序列。
(4)用凹入法输出该二叉树。
测试数据:
10
ABDEGCFHIJ
DBGEAHFIJC
9
-*+abc/de
a+b*c-d/e
#include <string>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int MaxSize = 40;
struct node
{
char data;
node* lchild;
node* rchild;
};
char pre[100], in[100];
string ans;
node* create(int preL, int preR, int inL, int inR)//输入先序序列和中序序列的区间
{
if (preL > preR){//递归边界
return NULL;
}
node* root = new node;//创建根节点
root->data = pre[preL];//赋值
int k;
for (k = inL; k <= inR; k++)//查找中序序列中根结点位置
if (in[k] == pre[preL])
break;
int numLeft = k - inL;//左子树结点数
root->lchild = create(preL + 1, preL + numLeft, inL, k - 1); //返回左子树根节点地址
root->rchild = create(preL + numLeft + 1, preR, k + 1, inR);//返回右子树根节点地址
return root;
}
void preorder(node* root)//先序遍历
{
if (root == NULL)
return;
// printf("%c", root->data);
ans = ans + (root->data);
preorder(root->lchild);
preorder(root->rchild);
}
void inorder(node* root)//中序遍历
{
if (root == NULL)
return;
inorder(root->lchild);
// printf("%c", root->data);
ans = ans + (root->data);
inorder(root->rchild);
}
void postorder(node* root)//后序遍历
{
if (root == NULL)
return;
postorder(root->lchild);
postorder(root->rchild);
// printf("%c", root->data);
ans = ans + (root->data);
}
string char2str(int st, int ed,char cs[])
{
string tmp;
for (int i = st; i <=ed; i++)
tmp = tmp + cs[i];
return tmp;
}
void DispTree(node* root)
{
node* p;
node*stack[MaxSize]; //stack[top]为指针数组
int level[MaxSize][2], top, i, n, width = 4;
//top为stack栈指针、levle[MaxSize][2]为二维数组,0维度-width(输出空格数)、1维度-不同节点
string type;
if (root != NULL)
{
top = 1; //左节点之前输出(0)
stack[top] = root; //根节点入栈
level[top][0] = width;
level[top][1] = 2; //2代表根节点
while (top > 0) //栈不为空时,循环继续
{
p = stack[top]; //退栈并凹入显示该节点值
n = level[top][0];
switch (level[top][1])
{
case 0:
type = "左"; //左节点之前输出(0)
break;
case 1:
type = "右"; //右节点之前输出(1)
break;
case 2:
type = "根"; //根节点之前输入(r)
break;
}
for (i = 1; i <= n; i++) //n为输出的空格数,字符以右对齐显示
cout << " ";
cout << p->data << " (" << type << ")";
for (i = n + 1; i <= MaxSize; i += 2)
cout << "-";
cout << endl;
top--; //根节点出栈
if (p->rchild != NULL)
{
top++;
stack[top] = p->rchild; //将右子树根节点入栈
level[top][0] = n + width; //输出空格数增加width
level[top][1] = 1; //1表示是右子树
}
if (p->lchild != NULL)
{
top++;
stack[top] = p->lchild; //将左子树根节点入栈
level[top][0] = n + width; //输出空格数增加width
level[top][1] = 0; //0代表左子树
}
}
}
}
int main()
{
string org;
int n;
printf("输入序列长度:");
scanf("%d", &n);
n = n+1 ;
printf("输入前序序列:");
for (int i = 0; i < n; i++)
pre[i] = getchar();
printf("输入中序序列:");
for (int i = 0; i < n; i++)
in[i] = getchar();
//for (int i = 0; i <= n; i++)
// printf("%c", pre[i]);
node* root = create(1, n-1 , 1, n-1 );
preorder(root);
org = char2str(1, n - 1, pre);
if (org == ans) cout << endl << ans << " 前序序列正确" << endl << endl;
org.clear();
ans.clear();
inorder(root);
org = char2str(1, n - 1, in);
if (org == ans) cout << ans << " 中序序列正确" << endl << endl;
org.clear();
ans.clear();
postorder(root);
cout << "后序序列为 " << ans << endl << endl;
cout << "凹入法输出 " << endl;
DispTree(root);
return 0;
}
/*
10
ABDEGCFHIJ
DBGEAHFIJC
9
-*+abc/de
a+b*c-d/e
*/