/** * 非递归前序遍历 * * @param p */ public static void interativePreOrder(Node<Integer> p) { Stack<Node<Integer>> stack = new Stack<Node<Integer>>(); if (p != null) { stack.push(p); while (!stack.empty()) { p = stack.pop(); visit(p); if (p.getLchild() != null) stack.push(p.getRchild()); if (p.getRchild() != null) stack.push(p.getLchild()); } } }
/** * 递归后序遍历 * * @param p */ public static void postOrder(Node<Integer> p) { if (p != null) { postOrder(p.getLchild()); postOrder(p.getRchild()); visit(p); } }
/** * 非递归实现后序遍历2:双栈法 * * @param node */ public static void interativePostOrder2(Node<Integer> node) { Stack<Node<Integer>> lstack = new Stack<Node<Integer>>(); Stack<Node<Integer>> rstack = new Stack<Node<Integer>>(); Node<Integer> p = node, right; do { while (p != null) { right = p.getRchild(); lstack.push(p); rstack.push(right); p = p.getLchild(); } p = lstack.pop(); right = rstack.pop(); // 所有右子节点已经全部访问完 if (right == null) { visit(p); } else { lstack.push(p); rstack.push(null); } p = right; } while (lstack.size() > 0 && rstack.size() > 0); }
/** * 非递归后序遍历1:单栈法 * * @param node */ public static void interativePostOrder(Node<Integer> node) { Node<Integer> p = node; Stack<Node<Integer>> stack = new Stack<Node<Integer>>(); while (node != null) { // 左子树入栈 while (node.getLchild() != null) { stack.push(node); node = node.getLchild(); } // 当前节点无右子节点或右子节点已经输出 while (node != null && (node.getRchild() == null || node.getRchild() == p)) { visit(node); p = node; if (stack.empty()) return; node = stack.pop(); } // 处理右子节点 stack.push(node); node = node.getRchild(); } }
/** * 非递归实现中序遍历1 * * @param node */ public static void interativeInOrder(Node<Integer> node) { Stack<Node<Integer>> stack = new Stack<Node<Integer>>(); while (node != null) { while (node != null) { // 当前节点右子节点入栈 if (node.getRchild() != null) stack.push(node.getRchild()); // 当前节点入栈 stack.push(node); node = node.getLchild(); } node = stack.pop(); while (!stack.empty() && node.getRchild() == null) { visit(node); node = stack.pop(); } visit(node); if (!stack.empty()) node = stack.pop(); else node = null; } }
/** * 非递归实现中序遍历2 ****************** 简单易懂: * * @param node */ public static void interativeInOrder2(Node<Integer> node) { Stack<Node> stack = new Stack<Node>(); Node p = node; while (p != null || stack.size() > 0) { while (p != null) { stack.push(p); p = p.getLchild(); } if (stack.size() > 0) { p = stack.pop(); visit(node); node = node.getRchild(); } } }
/** * 非递归前序遍历2 ***************************** 简单易懂, * * @param p */ public static void interativePreOrder2(Node<Integer> p) { Stack<Node<Integer>> stack = new Stack<Node<Integer>>(); Node<Integer> node = p; while (node != null || stack.size() > 0) { // 压入所有的左节点,压入前,先访问 while (node != null) { visit(node); stack.push(node); node = node.getLchild(); } // 左子树访问完后访问又子树 if (stack.size() > 0) { node = stack.pop(); node = node.getRchild(); } } }
/** * 非递归实现后序遍历4:双栈法 * * @param node */ public static void interativePostOrder4(Node<Integer> node) { Stack<Node<Integer>> stack = new Stack<Node<Integer>>(); Stack<Node<Integer>> temp = new Stack<Node<Integer>>(); Node<Integer> p = node; while (p != null && stack.size() > 0) { while (p != null) { temp.push(p); stack.push(p); p = p.getRchild(); } if (stack.size() > 0) { p = stack.pop(); p = p.getLchild(); } } while (temp.size() > 0) { p = temp.pop(); visit(p); } }