JAVA二叉数的深度遍历和广度遍历

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();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现二叉树的深度优先遍历可以使用递归或者栈来实现。递归方法比较简单,可以按照先序遍历、中序遍历和后序遍历的方式进行深度优先遍历。下面是一段示例代码,演示了Java中使用递归实现深度优先遍历的方法: ``` public class BinaryTree { // 定义二叉树节点类 public class TreeNode { public TreeNode leftNode; public TreeNode rightNode; public Integer val; public TreeNode(Integer val) { this.val = val; } } // 深度优先遍历-先序遍历 public void startErgodic(TreeNode node) { if (node == null) { return; } System.out.print(node.val + " "); startErgodic(node.leftNode); startErgodic(node.rightNode); } // 深度优先遍历-中序遍历 public void midErgodic(TreeNode node) { if (node == null) { return; } midErgodic(node.leftNode); System.out.print(node.val + " "); midErgodic(node.rightNode); } // 深度优先遍历-后序遍历 public void endErgodic(TreeNode node) { if (node == null) { return; } endErgodic(node.leftNode); endErgodic(node.rightNode); System.out.print(node.val + " "); } // 二叉树的插入操作 public void insert(Integer val) { // 插入操作的具体实现代码 } // 二叉树的递归插入操作 public void insertDigui(Integer val, TreeNode node) { // 递归插入操作的具体实现代码 } // 广度优先遍历 public void Order() { // 广度优先遍历的具体实现代码 } } public class Test { public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(); // 创建二叉树并进行插入操作 // 深度优先遍历-先序遍历 binaryTree.startErgodic(binaryTree.root); System.out.println(); // 深度优先遍历-中序遍历 binaryTree.midErgodic(binaryTree.root); System.out.println(); // 深度优先遍历-后序遍历 binaryTree.endErgodic(binaryTree.root); } } ``` 上述代码中的`startErgodic()`方法实现了二叉树的深度优先遍历先序遍历,`midErgodic()`方法实现了中序遍历,`endErgodic()`方法实现了后序遍历。可以根据需要选择相应的方法进行遍历。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java实现二叉树的深度优先遍历广度优先遍历算法示例](https://download.csdn.net/download/weixin_38518885/12761000)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [java有序二叉树的深度优先遍历广度优先遍历](https://blog.csdn.net/m566666/article/details/122280365)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值