public class E6 {
public static void main(String[] args) {
int preorder[] = {1,2,4,7,3,5,6,8};
int inorder[] = {4,7,2,1,5,3,8,6};
try {
BinaryTreeNode root = constructTree(preorder, inorder);
System.out.println("前序遍历:");
preorderTraversalRecursively(root);
System.out.println();
preorderTraversalNonRecursively(root);
System.out.println();
System.out.println("中序遍历:");
inorderTraversalRecursively(root);
System.out.println();
inorderTraversalNonRecursively(root);
System.out.println();
System.out.println("后序遍历:");
postorderTraversalRecursively(root);
System.out.println();
postorderTraversalNonRecursively(root);
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
private static BinaryTreeNode constructTree(int[] preorder, int[] inorder) throws Exception {
if (preorder == null || inorder == null
|| preorder.length == 0 || inorder.length!=preorder.length) {
throw new Exception("Invalid input");
}
BinaryTreeNode root = constructRecursively(preorder, 0, preorder.length-1,
inorder, 0, inorder.length-1);
return root;
}
private static BinaryTreeNode constructRecursively(int[] preorder, int preStart,
int preEnd, int[] inorder, int inStart, int inEnd) throws Exception {
if (preStart>preEnd || inStart>inEnd || preEnd-preStart != inEnd-inStart) {
return null;
}
BinaryTreeNode root = new BinaryTreeNode(preorder[preStart]);
int inorderRootIndex = inStart;
while (inorderRootIndex<=inEnd){
if (inorder[inorderRootIndex] == root.getValue()) {
break;
}
inorderRootIndex++;
}
if (inorderRootIndex>inEnd) {
throw new Exception("Invalid input");
}
int leftLength = inorderRootIndex-inStart;
root.setLeftNode(constructRecursively(preorder,preStart+1,preStart+leftLength,
inorder,inStart,inorderRootIndex-1));
root.setRightNode(constructRecursively(preorder, preStart+leftLength+1, preEnd,
inorder,inorderRootIndex+1,inEnd));
return root;
}
private static void postorderTraversalRecursively(BinaryTreeNode root) {
if (root == null) return;
postorderTraversalRecursively(root.getLeftNode());
postorderTraversalRecursively(root.getRightNode());
System.out.print(root.getValue()+" ");
}
private static void postorderTraversalNonRecursively(BinaryTreeNode root) {
if (root == null) return;
Stack<BinaryTreeNode> stack = new Stack<>();
BinaryTreeNode current = root;
do {
stack.push(current);
current = current.getLeftNode();
} while (current != null);
while (!stack.isEmpty()) {
if (stack.peek().getRightNode() != null) {
current = stack.peek().getRightNode();
do {
stack.push(current);
current = current.getLeftNode();
} while (current != null);
} else {
do {
current = stack.pop();
System.out.print(current.getValue()+" ");
}while (!stack.isEmpty() && current.equals(stack.peek().getRightNode()));
}
}
}
private static void inorderTraversalRecursively(BinaryTreeNode root) {
if (root == null) return;
inorderTraversalRecursively(root.getLeftNode());
System.out.print(root.getValue()+" ");
inorderTraversalRecursively(root.getRightNode());
}
private static void inorderTraversalNonRecursively(BinaryTreeNode root) {
if (root == null) return;
Stack<BinaryTreeNode> stack = new Stack<>();
BinaryTreeNode current = root;
do {
stack.push(current);
current = current.getLeftNode();
} while(current != null);
while (!stack.isEmpty()) {
BinaryTreeNode node = stack.pop();
System.out.print(node.getValue()+" ");
if (node.getRightNode() != null) {
current = node.getRightNode();
do {
stack.push(current);
current = current.getLeftNode();
}while (current!=null);
}
}
}
private static void preorderTraversalRecursively(BinaryTreeNode root) {
if (root == null) return;
System.out.print(root.getValue()+" ");
preorderTraversalRecursively(root.getLeftNode());
preorderTraversalRecursively(root.getRightNode());
}
private static void preorderTraversalNonRecursively(BinaryTreeNode root) {
if (root == null) return;
Stack<BinaryTreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
BinaryTreeNode node = stack.pop();
System.out.print(node.getValue()+" ");
if (node.getRightNode() != null) {
stack.push(node.getRightNode());
}
if (node.getLeftNode()!= null) {
stack.push(node.getLeftNode());
}
}
}
}
class BinaryTreeNode {
private int value;
private BinaryTreeNode leftNode;
private BinaryTreeNode rightNode;
public BinaryTreeNode(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinaryTreeNode getLeftNode() {
return leftNode;
}
public void setLeftNode(BinaryTreeNode leftNode) {
this.leftNode = leftNode;
}
public BinaryTreeNode getRightNode() {
return rightNode;
}
public void setRightNode(BinaryTreeNode rightNode) {
this.rightNode = rightNode;
}
}