二叉樹遍歷算法

先序遍歷

思路:先根節點->左子樹->右子樹;
二叉樹如下圖:
二叉樹遍歷算法

/**
 * TreeSearch 簡要描述
 * <p> TODO:描述該類職責 </p>
 *
 * @author ckmike
 * @version 1.0
 * @date 18-12-6 下午10:13
 * @copyright ckmike
 **/
public class TreeSearch {

    // 節點數據結構
    class TreeNode{
        private String value = null;
        private TreeNode leftchildren = null;
        private TreeNode rightchildre = null;

        public TreeNode(String value, TreeNode leftchildren, TreeNode rightchildre) {
            this.value = value;
            this.leftchildren = leftchildren;
            this.rightchildre = rightchildre;
        }

        public TreeNode(String value) {
            this.value = value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public void setLeftchildren(TreeNode leftchildren) {
            this.leftchildren = leftchildren;
        }

        public void setRightchildre(TreeNode rightchildre) {
            this.rightchildre = rightchildre;
        }

        public String getValue() {
            return value;
        }

        public TreeNode getLeftchildren() {
            return leftchildren;
        }

        public TreeNode getRightchildre() {
            return rightchildre;
        }
    }

    public TreeNode getTargetTree() {
        // 葉子節點
        TreeNode G = new TreeNode("G");
        TreeNode D = new TreeNode("D");
        TreeNode E = new TreeNode("E",G,null);
        TreeNode B = new TreeNode("B",D,E);
        TreeNode H = new TreeNode("H");
        TreeNode I = new TreeNode("I");
        TreeNode F = new TreeNode("F",H,I);
        TreeNode C = new TreeNode("C",null,F);
        // 構造根節點
        TreeNode root = new TreeNode("A",B,C);
        return root;
    }

    // 先序遍歷二叉樹
    public void preorderVistTreeNode(TreeNode node){
        if(null != node){
            System.out.print(node.value);
            if(null != node.leftchildren){
                preorderVistTreeNode(node.leftchildren);
            }
            if(null != node.rightchildre){
                preorderVistTreeNode(node.rightchildre);
            }
        }
    }

    public static void main(String[] args) {
       TreeSearch treeSearch = new TreeSearch();
       TreeNode tree= treeSearch.getTargetTree();
       treeSearch.preorderVistTreeNode(tree);
    }
}

二叉樹遍歷算法
先序遍歷結果:ABDEGCFHI

中序遍歷

思路:先左子樹->根節點->右子樹;

    // 中序遍歷
    public void inorderVistTreeNode(TreeNode node){
        if(null != node){
            if(null != node.leftchildren){
                inorderVistTreeNode(node.leftchildren);
            }
            System.out.print(node.value);
            if(null != node.rightchildre){
                inorderVistTreeNode(node.rightchildre);
            }
        }
    }

    public static void main(String[] args) {
       TreeSearch treeSearch = new TreeSearch();
       TreeNode tree= treeSearch.getTargetTree();
        System.out.print("中序遍歷:");
       treeSearch.inorderVistTreeNode(tree);
    }

二叉樹遍歷算法
中序遍歷結果:DBGEACHFI

後序遍歷

思路:先左子樹->右子樹->根節點;

    // 後序遍歷
    public void postorderVistTreeNode(TreeNode node){
        if(null != node){
            if(null != node.leftchildren){
                postorderVistTreeNode(node.leftchildren);
            }
            if(null != node.rightchildre){
                postorderVistTreeNode(node.rightchildre);
            }
            System.out.print(node.value);
        }
    }

    public static void main(String[] args) {
       TreeSearch treeSearch = new TreeSearch();
       TreeNode tree= treeSearch.getTargetTree();
        System.out.print("後序遍歷:");
       treeSearch.postorderVistTreeNode(tree);
    }

二叉樹遍歷算法
後序遍歷結果:DGEBHIFCA

層序遍歷

思路:先根節點,然後第二層,第三層,依次往下走,(同層節點從左往右輸出);

// 層序遍歷
    public void levelorderVistTreeNode(TreeNode node){
        if(null != node){
            LinkedList<TreeNode> list = new LinkedList<TreeNode>();
            list.add(node);
            TreeNode currentNode;
            while (!list.isEmpty()){
                currentNode = list.poll();
                System.out.print(currentNode.value);
                if(null != currentNode.leftchildren){
                    list.add(currentNode.leftchildren);
                }
                if(null != currentNode.rightchildre){
                    list.add(currentNode.rightchildre);
                }
            }
        }
    }

    public static void main(String[] args) {
       TreeSearch treeSearch = new TreeSearch();
       TreeNode tree= treeSearch.getTargetTree();
        System.out.print("層序遍歷:");
       treeSearch.levelorderVistTreeNode(tree);
    }

二叉樹遍歷算法
層序遍歷:ABCDEFGHI

這裏要注意一點就是層序遍歷二叉樹,是非遞歸的隊列實現的,就是利用隊列的先進先出(FIFO)實現的。那麼寫到這裏就把先序遍歷、中序遍歷、後序遍歷、層序遍歷二叉樹的算法都寫完了,是不是很簡單。如果有興趣可以想想有沒有不用遞歸實現先、中、後序遍歷的算法,用你熟悉的語言編寫出來,寫出來了記得分享出來喲。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章