根据二叉树前序和中序遍历,生成二叉树

import java.util.Scanner;

/**
 *
 * 项目名称:jobdu
 * 类名称:Main2
 * 类描述:构建二叉树
 * 创建人:黄传聪
 * 创建时间:2013-10-21 上午10:24:10
 * 修改人:
 * 修改时间:
 * 修改备注:
 * @version
 */
public class Main2 {

	private char node;
	private Main2 lTree;
	private Main2 rTree;
	
	
	public Main2(char node) {
		this.node = node;
		this.lTree = null;
		this.rTree = null;
	}


	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()){
			String preOrder = scanner.next();
			String inOrder = scanner.next();
			//生成二叉树
			Main2 binTree = new Main2(preOrder.charAt(0));
			generator(binTree,preOrder,inOrder);
			post(binTree);
			System.out.println();
		}
		
		
	}


	private static void post(Main2 binTree) {
		// TODO Auto-generated method stub
		if(binTree != null){
			post(binTree.lTree);
		post(binTree.rTree);
		System.out.print(binTree.node);
		}
	}


	private static Main2 generator(Main2 root ,String preOrder, String inOrder) {
		// TODO Auto-generated method stub
		
//		Main2 root = new Main2(preOrder.charAt(0));
		//根节点在中序遍历中的位置
		int index = inOrder.indexOf(preOrder.charAt(0));
		//拆分中序遍历的左右子树
		String lInorder = inOrder.substring(0,index);
		String rInorder = inOrder.substring(index+1,inOrder.length());
		//拆分前序遍历节点
		String lPreOrder = preOrder.substring(1,preOrder.length()-rInorder.length());
		String rPreOrder = preOrder.substring(preOrder.length()-rInorder.length(),preOrder.length());
		
		if(lPreOrder.length()>0){
			root.lTree = new Main2(lPreOrder.charAt(0));
			generator(root.lTree,lPreOrder,lInorder);
		}
		if(rPreOrder.length()>0){
			root.rTree = new Main2(rPreOrder.charAt(0));
			generator(root.rTree,rPreOrder,rInorder);
		}
		return null;
	}

}
c++ 实现: 1.直接输出方式实现;2. 先创建二叉树,再遍历

#include <iostream>
#include <string>
using namespace std;

struct Node{
	char data;
	Node* lChild;
	Node* rChild;

	Node(char data){
		this->data = data;
		lChild = NULL;
		rChild = NULL;
	}
};

//生成二叉树
void generator(Node* root, const string &pre, const string &mid){

	if(pre.size() >0){
		int index = mid.find(pre[0]);

		string pre_pre = pre.substr(1,index);
		string pre_mid = mid.substr(0,index);


		string post_pre = pre.substr(index+1,pre.size() - pre_pre.size() );
		string post_mid = mid.substr(index+1,mid.size() - pre_mid.size());
		if(pre_pre.size() > 0){
			root->lChild = new Node(pre_pre[0]);
			generator(root->lChild, pre_pre, pre_mid);
		}

		if(post_pre.size() > 0){
			root->rChild = new Node(post_pre[0]);
			generator(root->rChild, post_pre, post_mid);
		}
	}
}


//直接输出
void pre_mid_post(const string &pre, const string &mid){
	if(pre.size() <= 0) return;
	if(pre.size() == 1){
		cout<<pre;
		return;
	}

	//拆分字符串
	//前序遍历,第一个字符为根节点
	int k = mid.find(pre[0]);

	string pre_pre = pre.substr(1,k);
	string pre_mid = mid.substr(0,k);
	pre_mid_post(pre_pre, pre_mid);
	string post_pre = pre.substr(k+1,pre.size() - k - 1);
	string post_mid = mid.substr(k+1,mid.size() - k - 1);
	pre_mid_post(post_pre,post_mid);
	cout<<mid[k];

}
int main() {
	void post(Node* );
	string pre, mid;
	while(cin>>pre>>mid){
		pre_mid_post(pre, mid);
		cout<<endl;

		Node* root = new Node(pre[0]);
		generator(root , pre , mid);
//		cout<<root->data;
		post(root);
	}
	return 0;
}


//后序遍历二叉树
void post(Node* root){
	if(root){
		post(root->lChild);
		post(root->rChild);
		cout<<root->data;
	}
}





你可能感兴趣的:(根据二叉树前序和中序遍历,生成二叉树)