Friday, February 12, 2016

【UVa】536 – Tree Recovery

題目內容

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;
}

No comments:

Post a Comment