您现在的位置是:首页 > 后台技术 > 数据结构与算法数据结构与算法

顺序存储二叉树(图文)

第十三双眼睛2023-10-19【数据结构与算法】人已围观

简介顺序存储二叉树

顺序存储二叉树
package com.xingchen.day009;
public class ArrayBinTreeDemo {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        ArrayBinTree tree = new ArrayBinTree(arr);
        tree.preOrder(0);
    }
}
class ArrayBinTree {
    public int[] arr;
    public ArrayBinTree(int[] arr) {
        this.arr = arr;
    }
    public void preOrder(int index) {
        if (arr == null || arr.length == 0 ) {
            System.out.println("数组为空");
        }
        // 输出当前元素
        System.out.println(arr[index]);
        //递归左边
        if (index * 2 + 1 < arr.length) {
            preOrder(index * 2 + 1);
        }
        //向右递归
        if (index * 2 + 2 < arr.length) {
            preOrder(index * 2 + 2);
        }
    }
}

Tags:

很赞哦! ()

文章评论

    共有条评论来说两句吧...

    用户名:

    验证码:

本站推荐

站点信息

  • 网站名称:JavaStudy
  • 建站时间:2019-1-14
  • 网站程序:帝国CMS7.5
  • 文章统计242篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们