POJ 2255 Tree Recovery

Tree Recovery

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8155 Accepted: 5175

Description

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!

Input

    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.

Output

    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
 
package poj;

import java.util.Scanner;
/**
 * @description 								北京大学 OJ 2255 
 * 												跟据树的前序和中序遍历, 算出树的后序遍历。
 * @technique									递归。
 * @version 									1.0
 * @date										20120806
 * @time										10:54
 * @author Alex
 *
 */
public class Poj2255_20120806_0 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String preOrder, inOrder;
		Node root;
		while(in.hasNext()){
			preOrder = in.next();		//输入前序
			inOrder = in.next();		//输入后序。
//			root = getRoot(preOrder, 0, inOrder, 0, inOrder.length());  //递归获得树根。
			root = getRoot(preOrder, inOrder);
			lastOrder(root);			//后序遍历树。
			System.out.println();       //换行。
		}
	}
	/**
	 * 递归函数,获得一棵树,返回根结点。
	 * @param preStr  前序序列。
	 * @param ps	//前序序列开始位置。可不用。因为每次都从 0 位开始。(程序修改遗留);
	 * @param midStr //中序序列。
	 * @param ms  //后序序列开始位置。 可不用。因为每次都从 0 位开始遍历(程序修改遗留);。
	 * @param me //后序序列结束位置。 可不用。因为最多到结束位置。(程序修改遗留);
	 * @return 树根结点。
	 */
	@SuppressWarnings("unused")
	private static Node getRoot(String preStr,int ps,String midStr, int ms, int me){
		int i;
		for(i = ms; i < me; ++i){
			if(midStr.charAt(i) == preStr.charAt(ps)){
				Node node = new Poj2255_20120806_0().new Node(midStr.charAt(i));
				String pre_0 = preStr.substring(1,i+1);   //获得左子树的前序序列。
				String pre_1 = preStr.substring(i+1,preStr.length());//获得右子树的前序序列。
				String mid_0 = midStr.substring(0,i); //获得右子树的中序序列。
				String mid_1 = midStr.substring(i+1,midStr.length()); // 获得右子树的中序序列。
				Node left = getRoot(pre_0, 0 , mid_0, 0, mid_0.length()); //递归生成左子树。
				Node right = getRoot(pre_1,0, mid_1, 0, mid_1.length()); //递归生成右子树。
				node.setLeft(left); //设置根结点左子树。
				node.setRight(right);//设置根结点右子树。
				return node; //返回根结点。
			}
		}
		return null;
	}
	
	/**
	 * 先看完上一重载函数后,再看本函数。这是上一函数的重写,省去了很多东西。
	 * @param preStr
	 * @param midStr
	 * @return
	 */
	private static Node getRoot(String preStr, String midStr){
		int i;
		for(i = 0; i < midStr.length(); ++i){
			if(midStr.charAt(i) == preStr.charAt(0)){
				Node root = new Poj2255_20120806_0().new Node(midStr.charAt(i));
				Node left = getRoot(preStr.substring(1,i+1), midStr.substring(0,i));
				Node right = getRoot(preStr.substring(i+1,preStr.length()), midStr.substring(i+1,midStr.length()));
				root.setLeft(left);
				root.setRight(right);
				return root;
			}
		}
		return null;
	}
	private static void lastOrder(Node root){
		Node p = root;
		last(p);
	}
	private static void last(Node p){
		if(p!= null){
			last(p.getLeft());
			last(p.getRight());
			System.out.print(p.getValue());
		}
	}
	private class Node{
		private char value;
		private Node left;
		private Node right;
		private Node parent;
		
		public Node(){
			
		}
		public Node(char value){
			this.value = value;
			this.left = null;
			this.right = null;
			this.parent = null;
		}
		public char getValue() {
			return value;
		}
		public void setValue(char value) {
			this.value = value;
		}
		public Node getLeft() {
			return left;
		}
		public void setLeft(Node left) {
			this.left = left;
		}
		public Node getRight() {
			return right;
		}
		public void setRight(Node right) {
			this.right = right;
		}
		public Node getParent() {
			return parent;
		}
		public void setParent(Node parent) {
			this.parent = parent;
		}	
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值