【UVa】536 – Tree Recovery

来源:互联网 发布:软件压力测试报告 编辑:程序博客网 时间:2024/06/05 16:50

Problem

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!

Input Specification

The input file 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.

Output Specification

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

Sample Input

DBACEGF ABCDEFG
BCAD CBAD

Sample Output

ACBFGED
CDAB

Solution

在preorder中可以找到root,然后在inorder中找root的位置,在root左边为左子树,右边则为右子树。
然后就递归重建整棵树,最后输出postorder

Class Version

#include <iostream>#include <vector>#include <string>using namespace std;class Node{    public:        char data;        Node *lchild, *rchild;        Node(char _data){            data = _data;            lchild = NULL;            rchild = NULL;        }        ~Node(){            if(lchild != NULL){                delete lchild;            }            if(rchild != NULL){                delete rchild;            }        }        void postorder(){            if(lchild != NULL){                lchild->postorder();            }            if(rchild != NULL){                rchild->postorder();             }            cout << data; } Node* rebuild(const string &pre, const string &in, int length){ Node *current_node = new Node(pre[0]); int pos = in.find(pre[0]); if(pos > 0){                current_node->lchild = rebuild(pre.substr(1, pos), in.substr(0, pos), pos);            }            if(length-1-pos > 0){                current_node->rchild = rebuild(pre.substr(pos+1), in.substr(pos+1), length-1-pos);            }            return current_node;        }};class Tree{    public:           Node *root;           Tree(){               root = NULL;           }            ~Tree(){               if(root != NULL){                   delete root;               }           }           Tree(const string &pre, const string &in, int lenghth){               root = root->rebuild(pre, in, lenghth);           }           void postorder(){               root->postorder();           }};int main(){    string preorder, inorder;        while(cin >> preorder >> inorder){        Tree tree(preorder, inorder, inorder.length());        tree.postorder();          cout << endl;      }    return 0;}

Struct Version

#include <iostream>#include <string>using namespace std;struct Node{    char data;    Node *lchild;    Node *rchild;    Node(){        lchild = NULL;        rchild = NULL;    }  };Node* buildTree(string preorder, string inorder, int length){    Node *current_node = new Node();    current_node->data = preorder[0];    int pos = inorder.find(preorder[0]);    if(pos > 0){        current_node->lchild = buildTree(preorder.substr(1, pos), inorder.substr(0, pos), pos);    }    if(length-1-pos){        current_node->rchild = buildTree(preorder.substr(pos+1), inorder.substr(pos+1), length-1-pos);    }    return current_node;}void postorder(Node* root){    if(root->lchild != NULL){        postorder(root->lchild);    }    if(root->rchild != NULL){        postorder(root->rchild);    }    cout << root->data;}int main(){    string preorder, inorder;    while(cin >> preorder >> inorder){        postorder(buildTree(preorder, inorder, inorder.length()));        cout << endl;    }    return 0;}
0 0
原创粉丝点击