樹的深度優先遍曆和廣度優先遍曆的原理和java實現代碼

來源:互聯網
上載者:User

標籤:

import java.util.ArrayDeque;public class BinaryTree {    static class TreeNode{        int value;        TreeNode left;        TreeNode right;        public TreeNode(int value){            this.value=value;        }    }    TreeNode root;    public BinaryTree(int[] array){        root=makeBinaryTreeByArray(array,1);    }    /**     * 採用遞迴的方式建立一顆二叉樹     * 傳入的是二叉樹的數組標記法     * 構造後是二叉樹的二叉鏈表標記法     */    public static TreeNode makeBinaryTreeByArray(int[] array,int index){        if(index<array.length){            int value=array[index];            if(value!=0){                TreeNode t=new TreeNode(value);                array[index]=0;                t.left=makeBinaryTreeByArray(array,index*2);                t.right=makeBinaryTreeByArray(array,index*2+1);                return t;            }        }        return null;    }    /**     * 深度優先遍曆,相當於先根遍曆     * 採用非遞迴實現     * 需要輔助資料結構:棧     */    public void depthOrderTraversal(){        if(root==null){            System.out.println("empty tree");            return;        }               ArrayDeque<TreeNode> stack=new ArrayDeque<TreeNode>();        stack.push(root);               while(stack.isEmpty()==false){            TreeNode node=stack.pop();            System.out.print(node.value+"    ");            if(node.right!=null){                stack.push(node.right);            }            if(node.left!=null){                stack.push(node.left);            }                   }        System.out.print("\n");    }    /**     * 廣度優先遍曆     * 採用非遞迴實現     * 需要輔助資料結構:隊列     */    public void levelOrderTraversal(){        if(root==null){            System.out.println("empty tree");            return;        }        ArrayDeque<TreeNode> queue=new ArrayDeque<TreeNode>();        queue.add(root);        while(queue.isEmpty()==false){            TreeNode node=queue.remove();            System.out.print(node.value+"    ");            if(node.left!=null){                queue.add(node.left);            }            if(node.right!=null){                queue.add(node.right);            }        }        System.out.print("\n");    }    /**      *                  13     *                 /       *               65    5     *              /  \         *             97  25   37     *            /    /\   /     *           22   4 28 32     */    public static void main(String[] args) {        int[] arr={0,13,65,5,97,25,0,37,22,0,4,28,0,0,32,0};        BinaryTree tree=new BinaryTree(arr);        tree.depthOrderTraversal();        tree.levelOrderTraversal();    }}

樹的橫向遍曆和縱向遍曆

樹的深度優先遍曆和廣度優先遍曆的原理和java實現代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.