1. 程式人生 > >二叉樹的非遞迴遍歷演算法

二叉樹的非遞迴遍歷演算法

看了一篇部落格上對二叉樹的非遞迴遍歷的總結,非常不錯,記錄一下;

 /** 非遞迴實現前序遍歷 */  
    protected static void iterativePreorder(Node p) {  
        Stack<Node> stack = new Stack<Node>();  
        if (p != null) {  
            stack.push(p);  
            while (!stack.empty()) {  
                p = stack.pop();  
                visit(p);  
                if
(p.getRight() != null) stack.push(p.getRight()); if (p.getLeft() != null) stack.push(p.getLeft()); } } } /** 非遞迴實現前序遍歷2 */ protected static void iterativePreorder2(Node p) { Stack<Node> stack = new
Stack<Node>(); Node node = p; while (node != null || stack.size() > 0) { while (node != null) {//壓入所有的左節點,壓入前訪問它 visit(node); stack.push(node); node = node.getLeft(); } if (stack.size() > 0
) {// node = stack.pop(); node = node.getRight(); } } } /** 非遞迴實現後序遍歷 */ protected static void iterativePostorder(Node p) { Node q = p; Stack<Node> stack = new Stack<Node>(); while (p != null) { // 左子樹入棧 for (; p.getLeft() != null; p = p.getLeft()) stack.push(p); // 當前節點無右子或右子已經輸出 while (p != null && (p.getRight() == null || p.getRight() == q)) { visit(p); q = p;// 記錄上一個已輸出節點 if (stack.empty()) return; p = stack.pop(); } // 處理右子 stack.push(p); p = p.getRight(); } } /** 非遞迴實現後序遍歷 雙棧法 */ protected static void iterativePostorder2(Node p) { Stack<Node> lstack = new Stack<Node>(); Stack<Node> rstack = new Stack<Node>(); Node node = p, right; do { while (node != null) { right = node.getRight(); lstack.push(node); rstack.push(right); node = node.getLeft(); } node = lstack.pop(); right = rstack.pop(); if (right == null) { visit(node); } else { lstack.push(node); rstack.push(null); } node = right; } while (lstack.size() > 0 || rstack.size() > 0); } /** 非遞迴實現後序遍歷 單棧法*/ protected static void iterativePostorder3(Node p) { Stack<Node> stack = new Stack<Node>(); Node node = p, prev = p; while (node != null || stack.size() > 0) { while (node != null) { stack.push(node); node = node.getLeft(); } if (stack.size() > 0) { Node temp = stack.peek().getRight(); if (temp == null || temp == prev) { node = stack.pop(); visit(node); prev = node; node = null; } else { node = temp; } } } } /** 非遞迴實現後序遍歷4 雙棧法*/ protected static void iterativePostorder4(Node p) { Stack<Node> stack = new Stack<Node>(); Stack<Node> temp = new Stack<Node>(); Node node = p; while (node != null || stack.size() > 0) { while (node != null) { temp.push(node); stack.push(node); node = node.getRight(); } if (stack.size() > 0) { node = stack.pop(); node = node.getLeft(); } } while (temp.size() > 0) { node = temp.pop(); visit(node); } } /** 非遞迴實現中序遍歷 */ protected static void iterativeInorder(Node p) { Stack<Node> stack = new Stack<Node>(); while (p != null) { while (p != null) { if (p.getRight() != null) stack.push(p.getRight());// 當前節點右子入棧 stack.push(p);// 當前節點入棧 p = p.getLeft(); } p = stack.pop(); while (!stack.empty() && p.getRight() == null) { visit(p); p = stack.pop(); } visit(p); if (!stack.empty()) p = stack.pop(); else p = null; } } /** 非遞迴實現中序遍歷2 */ protected static void iterativeInorder2(Node p) { Stack<Node> stack = new Stack<Node>(); Node node = p; while (node != null || stack.size() > 0) { while (node != null) { stack.push(node); node = node.getLeft(); } if (stack.size() > 0) { node = stack.pop(); visit(node); node = node.getRight(); } } }

轉載自robinsoncrusoe的部落格-JAVA 二叉樹的遞迴和非遞迴遍歷