二叉树的顺序存储(数组转二叉树和二叉树转数组)Java实现

数组的储存方式树的储存方式可以相互转化

特点:

顺序存储二叉树通常只考虑完全二叉树;

非完全二叉树也可以顺序存储不过不能使用基本数据类型数组,要用引用类型(对象)数组,例如:不能使用 int[], 而要使用 Integer[]。

因为 Integer[] 的数组元素可以是null,表示当前节点为空,而 int[] 的数组元素只能是整数,无法表示当前节点为空的情况。所以 int[] 适用于完全二叉树。而 Integer[] 适用于所有情况。

规则:

第n个元素的左子节点的下标为2 * n + 1(数组下标从0开始)或2 * n(数组下标从1开始);

第n个元素的右子节点的下标为2 * n + 2(数组下标从0开始)或2 * n + 1(数组下标从1开始);

第n个元素的父节点为(n - 1) / 2(数组下标从0开始)或 n / 2(数组下标从1开始),都是向下取整;

二叉数转数组:

  • 层序遍历形成数组

  • 层序遍历是广度优先遍历,使用queue进行操作,不需要递归

        //二叉树的顺序存储,二叉树转数组
        public int[] binaryTreeToArray(Node root){
            //层序遍历形成数组
            //层序遍历是广度优先遍历,使用queue进行操作,不需要递归
            if(root == null){
                System.out.println("数为空");
                return null;
            }
            int[] arr = new int[10000];
            int k = 0;
            Queue<Node> queue = new LinkedList<>();
            queue.offer(root);
            while(!queue.isEmpty()){
                Node ret = queue.poll();
                arr[k++] = ret.getVal();
                if(ret.getLeft() != null){
                    queue.offer(ret.getLeft());
                }
                if(ret.getRight() != null){
                    queue.offer(ret.getRight());
                }
            }
            int[] ans = new int[k];
            for(int i = 0;i < k;i++){
                ans[i] = arr[i];
            }
            return ans;
        }

    数组转二叉树:

    同样数组转二叉树也是通过层序遍历(广度优先遍历)来形成二叉树的。

    完全二叉树:

传入的数组都是整数,不能有null来表示节点为空。

    
	public Node toTree(int[] arr) {
        Node root = new Node(arr[0]);
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        int k = 0;
        while (k < arr.length) {
            Node ret = queue.poll();
            if (2 * k + 1 < arr.length) {
                Node tem = new Node(arr[2 * k + 1]);
                ret.setLeft(tem);
                queue.offer(tem);

            }
            if (2 * k + 2 < arr.length) {

                Node tem = new Node(arr[2 * k + 2]);
                ret.setRight(tem);
                queue.offer(tem);

            }
            k++;
        }
        return root;
    }

输入样例:

{1, 2, 3, 4, 5, 6, 7}

这里我都用层序遍历打印以便还原数组元素的顺序。

运行结果:

所有情况:

 

可以用null来表示节点为空,可以完成任何形式的二叉树。


    public Node toTree(Integer[] arr) {
        Node root = new Node(arr[0]);
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        int k = 0;
        while (k < arr.length) {
            Node ret = queue.poll();
            if (2 * k + 1 < arr.length) {
                if (arr[2 * k + 1] != null) {
                    Node tem = new Node(arr[2 * k + 1]);
                    ret.setLeft(tem);
                    queue.offer(tem);
                } else {
                    ret.setLeft(null);
                }
            }
            if (2 * k + 2 < arr.length) {
                if (arr[2 * k + 2] != null) {
                    Node tem = new Node(arr[2 * k + 2]);
                    ret.setRight(tem);
                    queue.offer(tem);
                } else {
                    ret.setRight(null);
                }
            }
            k++;
        }
        return root;
    }

样例:

{1, 2, 3, null, null, null, 7}

运行结果:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值