Traversals of binary tree

_Git發表於2018-04-17

網上關於二叉樹的遍歷很多,不一定都全,希望我這個可以全一些。

Node

 private class BinaryNode<Any extends Comparable<Any>> {

        public Any data;
        public BinaryNode<Any> left;
        public BinaryNode<Any> right;
        public BinaryNode<Any> parent;

        public BinaryNode(Any data, BinaryNode<Any> left, BinaryNode<Any> right, BinaryNode<Any> parent) {
            this.data = data;
            this.left = left;
            this.right = right;
            this.parent = parent;
        }
}
複製程式碼

前序遍歷遞迴法

順序是根左右,沒什麼好記的

private void preOrder(BinaryNode<Any> root) {
        if (root == null)
            return;
        System.out.println(root.data);
        preOrder(root.left);
        preOrder(root.right);
}
複製程式碼

前序遍歷迭代法

用一個棧來實現,壓入根節點,再彈出,判斷彈出之節點的右孩子以及左孩子(順序不能倒)是否為空,再壓棧

private void preOrder(BinaryNode<Any> root) {
        if (root == null)
            return;
        Stack<BinaryNode<Any>> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            BinaryNode<Any> cur = stack.pop();
            System.out.println(cur.data);
            if (cur.right != null)
                stack.push(cur.right);
            if (cur.left != null)
                stack.push(cur.left);
        }
    }
複製程式碼

中序遍歷遞迴法

順序左根右,僅此而已

private void inOrder(BinaryNode<Any> root) {
        if (root == null)
            return;
        inOrder(root.left);
        System.out.println(root.data);
        inOrder(root.right);
    }
複製程式碼

中序遍歷迭代法

關鍵記住中序是一路向左的,還是用一個棧來實現

private void inOrder(BinaryNode<Any> root) {
        if (root == null)
            return;
        Stack<BinaryNode<Any>> stack = new Stack<>();
        BinaryNode<Any> cur = root;
        while (!stack.isEmpty() || cur != null) {
            while (cur != null) {
                stack.push(root);
                root = root.left;
            }
            if (!stack.isEmpty()) {
                cur = stack.pop();
                System.out.println(cur.data);
                cur = cur.right;
            }
        }
    }
複製程式碼

後序遍歷遞迴法

順序左右根

private void postOrder(BinaryNode<Any> root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.data);
    }
複製程式碼

後序遍歷迭代法

關鍵點,兩個棧實現一個佇列

private void postOrder2(BinaryNode<Any> root) {
        if (root == null)
            return;
        Stack<BinaryNode<Any>> s = new Stack<>();
        Stack<BinaryNode<Any>> output = new Stack<>();
        s.push(root);
        while (!s.isEmpty()) {
            BinaryNode cur = s.pop();
            output.push(cur);
            if (cur.left != null)
                s.push(cur.left);
            if (cur.right != null)
                s.push(cur.right);
        }
        while (!output.isEmpty()) {
            System.out.println(output.pop().data);
        }
    }
複製程式碼

深度遍歷迭代法

private void DFS(BinaryNode<Any> root) {
        if (root == null)
            return;
        Stack<BinaryNode<Any>> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            BinaryNode<Any> cur = stack.pop();
            System.out.println(cur.data);
            if (cur.right != null)
                stack.push(cur.right);
            if (cur.left != null)
                stack.push(cur.left);
        }
    }
複製程式碼

廣度遍歷迭代法

private void BFS(BinaryNode<Any> root) {
        if (root == null)
            return;
        Queue<BinaryNode<Any>> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            BinaryNode<Any> cur = queue.poll();
            System.out.print(cur.data + " ");
            if (cur.left != null)
                queue.offer(cur.left);
            if (cur.right != null)
                queue.offer(cur.right);
        }
    }
複製程式碼

相關文章