public class BinaryTree {
static class Node {
String value;
Node leftTree, rightTree;
public Node(String value, Node leftTree, Node rightTree) {
this.value = value;
this.leftTree = leftTree;
this.rightTree = rightTree;
}
void printValue() {
System.out.print(value);
}
}
public static Node createNode(String value) {
return new Node(value, null, null);
}
public static Node createNode(char value) {
return createNode(String.valueOf(value));
}
/**
* 添加节点到左子树
*/
public static void addNodeToLeft(Node root, Node child) {
if (root.leftTree == null) {
root.leftTree = child;
} else {
throw new RuntimeException("the left child has already created");
}
}
/**
* 添加节点到右子树
*/
public static void addNodeToRight(Node root, Node child) {
if (root.rightTree == null) {
root.rightTree = child;
} else {
throw new RuntimeException("the right child has already created");
}
}
/**
* 先序遍历
*/
public static void preorder_visit(Node root) {
root.printValue();
if (root.leftTree != null) {
preorder_visit(root.leftTree);
}
if (root.rightTree != null) {
preorder_visit(root.rightTree);
}
}
/**
* 中序遍历
*/
public static void inorder_visit(Node root) {
if (root.leftTree != null) {
inorder_visit(root.leftTree);
}
root.printValue();
if (root.rightTree != null) {
inorder_visit(root.rightTree);
}
}
/**
* 后序遍历
*/
public static void posorder_visit(Node root) {
if (root.leftTree != null) {
posorder_visit(root.leftTree);
}
if (root.rightTree != null) {
posorder_visit(root.rightTree);
}
root.printValue();
}
/**
* 根据前序和中序来创建这棵树
*/
public static Node createTreeByPreAndIn(Node root, String pre_values,
String in_values) {
if (!"".equals(in_values)) {
String new_pre_values = synchronizedPreAndIn(pre_values,in_values);//同步先序和中序字符串
char rootValue = new_pre_values.charAt(0);//这样可以保证,先序的第一个肯定是这棵树的根节点
root = createNode(rootValue);
int rootIndexOfIn = in_values.indexOf(rootValue);//得到根节点在中序中的位置
String leftInValues = in_values.substring(0,rootIndexOfIn);//节点左边的一大串
String rightInvalues = in_values.substring(rootIndexOfIn+1,in_values.length());//节点右边的一大串
/**
* 如果右边一大串就剩下一个了,说明这肯定就是这课树的右节点,如果还剩下多个就继续递归调用
*/
if(leftInValues.length() == 1){
addNodeToLeft(root, createNode(leftInValues));
}else if(leftInValues.length() != 0){
addNodeToLeft(root, createTreeByPreAndIn(root, new_pre_values, leftInValues));
}
/**
* 如果左边一大串就剩下一个了,说明这肯定就是这课树的左节点,如果还剩下多个就继续递归调用
*/
if(rightInvalues.length() == 1){
addNodeToRight(root, createNode(rightInvalues));
}else if(rightInvalues.length() != 0){
addNodeToRight(root, createTreeByPreAndIn(root, new_pre_values, rightInvalues));
}
}
return root;
}
/**
* 每次都把在pre_values但是不在in_values里面的元素删掉,但是顺序不要变
*/
private static String synchronizedPreAndIn(String preValues, String inValues) {
String retVal = "";
for (int i = 0; i < preValues.length(); i++) {
for (int j = 0; j < inValues.length(); j++) {
if(preValues.charAt(i) == inValues.charAt(j)){
retVal+=preValues.charAt(i);
}
}
}
return retVal;
}
public static void main(String[] args) {
Node a = createTreeByPreAndIn(null, "ADCEFGHB", "CDFEGHAB");
/*Node a = createNode("A");
Node b = createNode("B");
Node c = createNode("C");
Node d = createNode("D");
Node e = createNode("E");
Node f = createNode("F");
Node g = createNode("G");
Node h = createNode("H");
addNodeToLeft(a, d);
addNodeToRight(a, b);
addNodeToLeft(d, c);
addNodeToRight(d, e);
addNodeToLeft(e, f);
addNodeToRight(e, g);
addNodeToRight(g, h);*/
System.out.print("先序遍历:");
preorder_visit(a);
System.out.println();
System.out.print("中序遍历:");
inorder_visit(a);
System.out.println();
System.out.print("后序遍历:");
posorder_visit(a);
System.out.println();
}
}
转载于:https://blog.51cto.com/2925665/1328362