/**
* 树结构,包括节点值,左子树节点指针,右子树节点指针
*/
public class TreeNode<T extends Comparable> {
private T value;
private TreeNode left;
private TreeNode right;
public TreeNode() {
}
public TreeNode(T value, TreeNode left, TreeNode right) {
this.value = value;
this.left = left;
this.right = right;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
}
import java.util.Arrays;
import java.util.Stack;
public class TreeTest {
public static void main(String[] args) throws Exception {
int[] input = {4, 2, 6, 1, 3, 5, 7, 8, 10};
System.out.println("======构建树:" + Arrays.toString(input) + "======");
TreeNode<Integer> rootNode = null;
for (int i = 0; i < input.length; i++) {
rootNode = insert(input[i], rootNode);
}
preOrder(rootNode);
System.out.println();
preOrder1(rootNode);
System.out.println();
System.out.println("==============================================");
inOrder(rootNode);
System.out.println();
inOrder1(rootNode);
System.out.println();
System.out.println("==============================================");
postOrder(rootNode);
System.out.println();
postOrder1(rootNode);
System.out.println();
}
//前序(先根):根、左、右
public static void preOrder(TreeNode node) {
if (null != node) {
System.out.print(node.getValue() + ", ");
preOrder(node.getLeft());
preOrder(node.getRight());
}
}
//中序(根中间):根、左、右
public static void inOrder(TreeNode node) {
if (null != node) {
inOrder(node.getLeft());
System.out.print(node.getValue() + ", ");
inOrder(node.getRight());
}
}
//后序(根最后):左、右、根
public static void postOrder(TreeNode node) {
if (null != node) {
inOrder(node.getLeft());
inOrder(node.getRight());
System.out.print(node.getValue() + ", ");
}
}
/**
* 前序遍历
* 非递归
*/
public static void preOrder1(TreeNode node) {
Stack<TreeNode> stack = new Stack<>();
stack.push(node);
while (!stack.empty()) {
node = stack.pop();
System.out.print(node.getValue() + ", ");
if (null != node.getRight()) {
stack.push(node.getRight());
}
if (null != node.getLeft()) {
stack.push(node.getLeft());
}
}
}
/**
* 中序遍历
* 非递归
*/
public static void inOrder1(TreeNode node) {
Stack<TreeNode> stack = new Stack<>();
while (node != null || !stack.empty()) {
while (null != node) {
stack.push(node);
node = node.getLeft();
}
if (!stack.empty()) {
node = stack.pop();
System.out.print(node.getValue() + ", ");
node = node.getRight();
}
}
}
/**
* 后序遍历
* 非递归
*/
public static void postOrder1(TreeNode node) {
Stack<TreeNode> stack = new Stack<>();
TreeNode visiteNode = node;
TreeNode currentNode = node;
while (currentNode != null || !stack.empty()) {
while (null != currentNode) {
stack.push(currentNode);
currentNode = currentNode.getLeft();
}
currentNode = stack.peek();
if (null != currentNode.getRight() && visiteNode != currentNode.getRight()) {
currentNode = currentNode.getRight();
} else {
currentNode = stack.pop();
System.out.print(currentNode.getValue() + ", ");
visiteNode = currentNode;
currentNode = null;
}
}
}
private static <E extends Comparable> TreeNode<E> insert(E value, TreeNode<E> t) {
if (t == null) {
return new TreeNode<>(value, null, null);
}
int compareResult = value.compareTo(t.getValue());
if (compareResult < 0) {
t.setLeft(insert(value, t.getLeft()));
} else if (compareResult > 0) {
t.setRight(insert(value, t.getRight()));
} else {
;
}
return t;
}
}