顺序二叉树的遍历

顺序二叉树的遍历 :

顺序二叉树首先要满足的条件是:二叉树应当是一个完全二叉树

几个关于完全二叉树的结论 :

第n个元素的左节点为 :2n+1 右节点是 2n+2;

任意一个节点的父节点是 (n-1)/2

代码实现:

public class ArrayBinaryTree {
    //数组顺序存储形成的树
    int []  data;

    public ArrayBinaryTree(int []data){
        this.data=data;
    }
    public void frontShow(int index){     //index 为传入的需要遍历的点
        if(data==null||data.length==0){
            //data=null   数组没有被创建
            return;  //直接结束
        }
        System.out.println(data[index]);

        //开始遍历子结点的数据  完全二叉树中寻找接下来的左右节点
          if (2*index+1<data.length){
              frontShow(2*index+1);
          }
          if (2*index+2< data.length){
              frontShow(2*index+2);
          }
    }
}

测试代码:

public class Test {
    public static void main(String[] args) {
        int []data=new int[]{1,2,3,4,5,6,7};
        ArrayBinaryTree tree =new ArrayBinaryTree(data);
        tree.frontShow(0);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值