public class StackTree {static Node root;class Node {private int val;private Node rchild;private Node lchild;public Node(int val) {this.val = val;}}boolean insert(int val) {//添加树节点Node node = new Node(val);if (root == null) {root = node;return true;} else {Node cur = root;while (true) {Node newNode = new Node(val);if (cur.val > val) {if (cur.lchild == null) {cur.lchild = newNode;return true;}cur = cur.lchild;} else if (cur.val < val) {if (cur.rchild == null) {cur.rchild = newNode;return true;}cur = cur.rchild;} else {return false;}}}}/*** 先序* @param node*/static void preorder(Node root) {if (root == null)return;Stack<Node> stack = new Stack<>();Node cur = root;while (cur != null || !stack.isEmpty()) {while (cur != null) {System.out.print(cur.val);//输出根stack.push(cur);//进栈cur = cur.lchild;//遍历左子树}cur = stack.pop();cur = cur.rchild;}}/*** 中序* @param node*/static void inorder(Node root) {if (root == null)return;Stack<Node> stack = new Stack<StackTree.Node>();Node cur = root;while (cur != null || !stack.isEmpty()) {while (cur != null) {stack.push(cur);cur = cur.lchild;}cur = stack.pop();System.out.println(cur.val);cur = cur.rchild;}}/*** 后序,双栈法* @param node*/static void postorder1(Node root) {if(root == null) return ;Stack<Node> stack = new Stack<StackTree.Node>();Stack<Node> stack1 = new Stack<StackTree.Node>();Node cur = root;while(cur != null || !stack.isEmpty()) {while(cur!=null) {stack1.push(cur);stack.push(cur);cur = cur.rchild;}cur = stack.pop();cur = cur.lchild;}int n = stack1.size();for (int i = 0; i < n; i++) {System.out.print(stack1.pop().val);}}/*** 后序,单栈标记法* @param node*/static void postorder(Node node) {if (root == null)return;Stack<Node> stack = new Stack<StackTree.Node>();Node cur = root;Node t = null;//标识是否已经出栈while (cur != null || !stack.isEmpty()) {while (cur != null) {stack.push(cur);cur = cur.lchild;}//cur.left == nullcur = stack.peek();//取栈顶元素if(cur.rchild == null || t == cur.rchild) {//右子树为空或者右子树已经处理过t = stack.pop();//出栈,标识是否已经处理过System.out.print(t.val);cur = null;}else {cur = cur.rchild;}}}static void preOrder(Node node) {if (node == null)return;System.out.print(node.val);preOrder(node.lchild);preOrder(node.rchild);}static void inOrder(Node node) {if (node == null)return;inOrder(node.lchild);System.out.print(node.val);inOrder(node.rchild);}static void postOrder(Node node) {if (node == null)return;postOrder(node.lchild);postOrder(node.rchild);System.out.print(node.val);}public static void main(String[] args) {StackTree tree = new StackTree();int[] a = { 2, 5, 1, 3, 9, 8, 7, 4, 6 };for (int i = 0; i < a.length; i++) {tree.insert(a[i]);}preOrder(root);System.out.println();preorder(root);System.out.println();inOrder(root);System.out.println();inorder(root);postOrder(root);System.out.println();postorder(root);System.out.println();postorder1(root);}}
用栈的方式(非递归)来实现二叉树的遍历,说白了只是拿我们的用户定义栈去替换系统栈而已,其它并没有本质的区别,来看个例子就知道了。
二叉树前序遍历的递归写法
public void preorder(Node root) {
if (root != null) {
System.out.println(root.val);
preorder(root.left);
preorder(root.right);
}
}
二叉树前序遍历的非递归写法
public void preorder(Node root) {
if (root == null)
return;
Stack<Node> stack = new Stack<>();
Node cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
System.out.print(cur.val);//输出根
stack.push(cur);//进栈
cur = cur.lchild;//遍历左子树
}
cur = stack.pop();
cur = cur.rchild;
}
}
可以看出来,递归和非递归都是先把当前 root 的值打印出来,然后不停地去遍历左子树,直到左子树为空。
// 递归
preorder(root.left);
// 非递归
stack.push(cur);//进栈
cur = cur.lchild;//遍历左子树
然后判断当前结点的左子树为空后,开始遍历它的右子树
// 递归
preorder(root.right);
// 非递归
while (...) {
...
}
cur = stack.pop();
cur = cur.rchild;
中序遍历也是如此:
二叉树中序遍历的递归写法
public void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.println(root.val);
inorder(root.right);
}
}
二叉树中序遍历的非递归写法
public void inorder(Node root) {
if (root == null)
return;
Stack<Node> stack = new Stack<>();
Node cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
System.out.println(cur.val);
cur = cur.right;
}
}
后序遍历有些特殊,先来看看后序遍历的递归写法:
后序遍历的递归写法:
public void postorder(Node root) {
if (root != null) {
postorder(root.left);
postorder(root.right);
System.out.println(root.val);
}
}
因为访问/输出 结点值是最后才执行的,所以无法边遍历边访问,只能把结点值放到一个栈中最后才访问。
而由于栈是先进后出的,所以我们得逆序保存结点
后序遍历的非递归写法:
public void postorder(Node root) {
if (root == null) return;
Stack<Node> stack1 = new Stack<>();
Stack<Node> stack2 = new Stack<>();
Node cur = root;
while (cur != null || !stack1.isEmpty()) {
while (cur != null) {
// 根节点是最后访问的,先入栈的后访问
stack1.push(cur);
stack2.push(cur);
cur = cur.right;
}
cur = stack.pop();
cur = cur.left;
}
// 访问结点
int n = stack2.size();
for (int i = 0; i < n; i++) {
System.out.print(stack2.pop().val);
}
}
但是上面这种写法需要完整遍历一遍树后,才能开始访问树的每一个节点,如果想一边遍历一边访问的话,就需要和递归写法靠近了。
public void postorder(Node root) {
if (root == null)
return;
Stack<Node> stack = new Stack<StackTree.Node>();
Node cur = root;
Node t = null; //标识是否已经出栈
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.lchild;
} //cur.left == null
cur = stack.peek(); //取栈顶元素
if(cur.rchild == null || t == cur.rchild) { //右子树为空或者右子树已经处理过
t = stack.pop(); //出栈,标识是否已经处理过
System.out.print(t.val);
cur = null;
}else {
cur = cur.rchild;
}
}
}
其中 t == cur.rchild 很重要,不能在 if 里面只判断 if(cur.rchild == null) ,因为当前结点的右孩子遍历完后,就开始遍历当前结点了,如果只有if(cur.rchild == null),那么又会进入到 else 分支去执行 cur = cur.rchild 造成无限循环,因此需要做标记才行(手动添加 visited Set 也可以,不过会浪费一定空间)
