二叉樹的深度和廣度優先遍歷 - Java實現


轉載https://blog.csdn.net/cafucwxy/article/details/78447166

樹的深度優先遍歷需要用到額外的數據結構—>棧;而廣度優先遍歷需要隊列來輔助;這里以二叉樹為例來實現。

package com.web;

import java.util.ArrayDeque;
import java.util.Stack;

public class TreeTest {
       static class TreeNode{
            int value;
            TreeNode left;
            TreeNode right;
            public TreeNode(int value){
                this.value=value;
            }
        }
        TreeNode root;
        public TreeTest(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<TreeNode> stack = new Stack();   //也可以用棧實現
            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};
            TreeTest tree=new TreeTest(arr);
            tree.depthOrderTraversal();
            tree.levelOrderTraversal();
        }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM