1。由先序和中序建立二叉树。
2。由中序和后序建立二叉树。
3。二叉树的非递归先序,中序和后序遍历
import java.util.Scanner;
import java.util.Stack;
class Node {
Node left;
Node right;
char value;
Node() {}
Node(char value) {
left = null;
right = null;
this.value = value;
}
}
public class BuidTree {
/**
* 由先序和中序建立二叉树
* @param A 先序遍历
* @param B 中序遍历
* @param L1 先序遍历下标起点
* @param H1 先序遍历下标终点
* @param L2 中序遍历下标起点
* @param R2 中序遍历下标终点
*/
public static Node PreInCreate(char A[], char B[], int L1, int H1, int L2, int H2) {
Node root = new Node();
root.value = A[L1];
int i;
for(i = L2; B[i] != root.value; i++);
int llen = i - L2;
int rlen = H2 - i;
if(llen > 0) {
root.left = PreInCreate(A, B, L1+1, L1+llen, L2, L2+llen-1);
} else {
root.left = null;
}
if(rlen > 0) {
root.right = PreInCreate(A, B, H1-rlen+1, H1, H2-rlen+1, H2);
} else {
root.right = null;
}
return root;
}
/**
* 由中序和后序建立二叉树
* @param A 中序遍历
* @param B 后序遍历
* @param L1 中序遍历起点下标
* @param H1 中序遍历终点下标
* @param L2 后序遍历起点下标
* @param H2 后序遍历终点下标
* @return
*/
public static Node InPostCreate(char A[], char B[], int L1, int H1, int L2, int H2) {
Node root = new Node(B[H2]);
int i;
for(i = L1; A[i] != root.value && i <= H1; i++);
int llen = i - L1;
int rlen = H1 - i;
if(llen > 0) {
root.left = InPostCreate(A, B, L1, L1+llen-1, L2, L2+llen-1);
} else {
root.left = null;
}
if(rlen > 0) {
root.right = InPostCreate(A, B, i+1, H1, H2-rlen, H2-1);
} else {
root.right = null;
}
return root;
}
//先序遍历
public static void preOrder(Node head) {
if(head == null) return;
System.out.print(head.value + " ");
preOrder(head.left);
preOrder(head.right);
}
//非递归先序遍历
public static void preOrderUnRecurion(Node head) {
System.out.println("非递归先序遍历:");
if(head != null) {
Stack<Node> stack = new Stack<>();
stack.add(head);
while(!stack.isEmpty()) {
head = stack.pop();
System.out.print(head.value + " ");
if(head.right != null) {
stack.add(head.right);
}
if(head.left != null) {
stack.add(head.left);
}
}
}
}
//中序遍历
public static void inOrder(Node head) {
if(head == null) return;
inOrder(head.left);
System.out.print(head.value + " ");
inOrder(head.right);
}
public static void inOrderUnRecurion(Node head) {
System.out.println("非递归中序遍历:");
if(head != null) {
Stack<Node> stack = new Stack<>();
while(!stack.isEmpty() || head != null) {
if(head != null) {
stack.add(head);
head = head.left;
} else {
head = stack.pop();
System.out.print(head.value + " ");
head = head.right;
}
}
}
System.out.println();
}
//后序遍历
public static void posOrder(Node head) {
if(head==null) {
return;
}
posOrder(head.left);
posOrder(head.right);
System.out.print(head.value + " ");
}
public static void posOrderUnRecurion(Node head) {
System.out.println("非递归后序遍历: ");
if(head != null) {
Stack<Node> s1 = new Stack<>();
Stack<Node> s2 = new Stack<>();
s1.push(head);
while(!s1.isEmpty()) {
head = s1.pop();
s2.push(head);
if(head.left != null) {
s1.push(head.left);
}
if(head.right != null) {
s1.push(head.right);
}
}
while(!s2.isEmpty()) {
System.out.print(s2.pop().value + " ");
}
}
System.out.println();
}
public static void testPreInCreate() {
String A, B;
Scanner scanner = new Scanner(System.in);
A = scanner.nextLine();
B = scanner.nextLine();
Node root = PreInCreate(A.toCharArray(), B.toCharArray(), 0, A.length()-1, 0, B.length()-1);
System.out.println("先序遍历:");
preOrder(root);
System.out.println();
System.out.println("中序遍历:");
inOrder(root);
System.out.println();
System.out.println("后序遍历:");
posOrder(root);
}
public static void testInPostCreate() {
String A, B;
Scanner scanner = new Scanner(System.in);
A = scanner.nextLine();
B = scanner.nextLine();
Node root = InPostCreate(A.toCharArray(), B.toCharArray(), 0, A.length()-1, 0, B.length()-1);
System.out.println(A + " " + B);
System.out.println("先序遍历:");
//preOrder(root);
preOrderUnRecurion(root);
System.out.println();
System.out.println("中序遍历:");
//inOrder(root);
inOrderUnRecurion(root);
System.out.println();
System.out.println("后序遍历:");
//posOrder(root);
posOrderUnRecurion(root);
}
public static void main(String[] args) {
//由先序和中序建立二叉树
//testPreInCreate();
System.out.println();
//由中序和后序建立二叉树
testInPostCreate();
}
}