您的位置:首页 > 其它

习题课第一次作业:2、重建二叉树

2016-09-19 19:20 435 查看
描述:

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.

This is an example of one of her creations:

D
/ \
/   \
B     E
/ \     \
/   \     \
A     C     G
/
/
F


To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.

She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.

However, doing the reconstruction by hand, soon turned out to be tedious.

So now she asks you to write a program that does the job for her!

输入

The input will contain one or more test cases.

Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)

Input is terminated by end of file.

输出

For each test case, recover Valentine’s binary tree and print one line containing the tree’s postorder traversal (left subtree, right subtree, root).

样例输入

DBACEGF ABCDEFG

BCAD CBAD

样例输出

ACBFGED

CDAB

来源

Ulm Local 1997

解决思路:

#include <iostream>
#include <string>
#define LOCAL
using namespace std;
string s_pre , s_in , s_post;

struct Dnode
{
char ch;
struct Dnode * left;
struct Dnode * right;
};

void Post_travle(Dnode * &T)
{
if ( T != nullptr )
{
Post_travle(T->left);
Post_travle(T->right);
cout << T->ch ;
delete(T);
}
}

int Find_root(string in, char ch)
{
for (size_t i = 0 ;i < in.length(); i++)
if (in[i] == ch)
return i;
return -1;
}

Dnode* Construct_Binary_tree(const string pre, int pre_low, int pre_high,
const string in,  int in_low,  int in_high)
{
int len = in_high - in_low + 1;
if (len > 0)
{
Dnode * cur = new Dnode;
cur->ch = pre[pre_low];
cur->left = nullptr;
cur->right = nullptr;
if (len == 1)
return cur;
int postion = Find_root(in, cur->ch);
cur->left = Construct_Binary_tree(pre, pre_low + 1, pre_low + postion - in_low, in, in_low, postion - 1);
cur->right = Construct_Binary_tree(pre, pre_low + postion - in_low + 1, pre_high, in, postion + 1, in_high);
return cur;
}
return nullptr;
}

int main()
{
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
while ( std::cin >> s_pre >> s_in )
{
Dnode * root = Construct_Binary_tree(s_pre, 0, s_pre.length() - 1, s_in, 0, s_in.length() - 1);
Post_travle(root);
cout << endl;
}
return 0;
}


//考虑中序与后序、中序与层次重构二叉树的算法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: