二叉树的遍历:前中后序(递归、非递归、不借助辅助空间)、按层遍历
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class T1 {
public static class Node {
public int value;
Node left;
Node right;
public Node(int data) {
this.value = data;
}
}
public static void main(String[] args) {
Node head = new Node(4);
head.left = new Node(2);
head.right = new Node(6);
head.left.left = new Node(1);
head.left.right = new Node(3);
head.right.left = new Node(5);
head.right.right = new Node(7);
printTree(head);
System.out.println("中序遍历(morris遍历):");
morrisIn(head);
System.out.println("前序遍历(morris遍历):");
morrisPre(head);
System.out.println("前序遍历(递归):");
printTreePreOrderRecursively(head);
System.out.println("\n中序遍历(递归):");
printTreeInOrderRecursively(head);
System.out.println("\n后序遍历(递归):");
printTreePostOrderRecursively(head);
System.out.println("\n前序遍历(非递归):");
printTreePreOrderNotRecur(head);
System.out.println("\n中序遍历(非递归):");
printTreeInOrderNotRecur(head);
System.out.println("\n后序遍历(非递归):");
printTreePostOrderNotRecur(head);
System.out.println("\n按层遍历:");
printTreeQueue(head);
printTree(head);
}
public static void printTree(Node head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "H", 17);
System.out.println();
}
public static void printInOrder(Node head, int height, String to, int len) {
if (head == null) {
return;
}
printInOrder(head.right, height + 1, "v", len);
String val = to + head.value + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
System.out.println(getSpace(height * len) + val);
printInOrder(head.left, height + 1, "^", len);
}
public static String getSpace(int num) {
String space = " ";
StringBuffer buf = new StringBuffer("");
for (int i = 0; i < num; i++) {
buf.append(space);
}
return buf.toString();
}
private static void morrisPre(Node head) {
if(head == null) {
return;
}
while(head != null) {
if(head.left != null) {
Node morisRight = getMorrisRight(head);
if(morisRight.right != null) {
morisRight.right = null;
head = head.right;
} else {
morisRight.right = head;
System.out.print(head.value + ",");
head = head.left;
}
} else {
System.out.print(head.value + ",");
head = head.right;
}
}
System.out.println();
}
public static void morrisIn(Node head) {
if(head == null) {
return;
}
while(head != null) {
if(head.left != null) {
Node morisRight = getMorrisRight(head);
if(morisRight.right != null) {
System.out.print(head.value + ",");
morisRight.right = null;
head = head.right;
} else {
morisRight.right = head;
head = head.left;
}
} else {
System.out.print(head.value + ",");
head = head.right;
}
}
System.out.println();
}
public static Node getMorrisRight(Node head) {
Node left = head.left;
while(left.right != null && left.right != head) {
left = left.right;
}
return left;
}
private static void printTreeQueue(Node head) {
if(head == null) {
return;
}
Queue<Node> queue = new LinkedList<>();
queue.add(head);
while(!queue.isEmpty()) {
head = queue.poll();
System.out.print(head.value + ",");
if(head.left != null) {
queue.add(head.left);
}
if(head.right != null) {
queue.add(head.right);
}
}
System.out.println();
}
private static void printTreePostOrderNotRecur(Node head) {
if(head == null) {
return;
}
Stack<Node> stack = new Stack<>();
Stack<Node> help = new Stack<>();
while(head != null || !stack.isEmpty()) {
if(head != null) {
stack.push(head);
help.push(head);
head = head.right;
} else {
head = stack.pop();
head = head.left;
}
}
while(!help.isEmpty()) {
System.out.print(help.pop().value + ",");
}
}
private static void printTreeInOrderNotRecur(Node head) {
if(head == null) {
return;
}
Stack<Node> stack = new Stack<>();
while(head != null || !stack.isEmpty()) {
if(head != null) {
stack.push(head);
head = head.left;
} else {
head = stack.pop();
System.out.print(head.value + ",");
head = head.right;
}
}
}
private static void printTreePreOrderNotRecur(Node head) {
if(head == null) {
return;
}
Stack<Node> stack = new Stack<>();
while(head != null || !stack.isEmpty()) {
if(head != null) {
System.out.print(head.value + ",");
stack.push(head);
head = head.left;
} else {
head = stack.pop();
head = head.right;
}
}
}
private static void printTreePostOrderRecursively(Node head) {
if(head == null) {
return;
} else {
printTreePostOrderRecursively(head.left);
printTreePostOrderRecursively(head.right);
System.out.print(head.value + ",");
}
}
private static void printTreeInOrderRecursively(Node head) {
if(head == null) {
return;
} else {
printTreeInOrderRecursively(head.left);
System.out.print(head.value + ",");
printTreeInOrderRecursively(head.right);
}
}
private static void printTreePreOrderRecursively(Node head) {
if(head == null) {
return;
} else {
System.out.print(head.value + ",");
printTreePreOrderRecursively(head.left);
printTreePreOrderRecursively(head.right);
}
}
}