日撸 Java 三百行day22学习笔记

第 22 天: 二叉树的存储

今天的代码与18天的循环队列还有21的二叉树代码有很大关联。二叉树的存储同样有两种方法,今天对应博客里也已提到:

1:空使用 0 来表示, 可以用一个向量来存储:
[a, b, c, 0, d, e, 0, 0, 0, f, g]
优点: 仅需要一个向量, 简单直接.
缺点: 对于实际的二叉树, 很多子树为空, 导致大量的 0 值.
2: 应使用压缩存储方式, 即将节点的位置和值均存储. 可表示为两个向量:
[0, 1, 2, 4, 5, 9, 10]
[a, b, c, d, e, f, g]

显然我们今天的代码使用的第二种方式。上核心code:

char[] valuesArray;

    /**
     * The indices in the complete binary tree.
     */
    int[] indicesArray;

    /**
     ********************
     * Convert the tree to data arrays, including a char array and an int array.
     * The results are stored in two member variables.
     *
     * @see #valuesArray
     * @see #indicesArray
     *********************
     */
    public void toDataArrays() {
        //Initialize arrays.
        int tempLength = getNumNodes();

        valuesArray = new char[tempLength];
        indicesArray = new int[tempLength];
        int i = 0;

        //Traverse and convert at the same time.
        CircleObjectQueue tempQueue = new CircleObjectQueue();
        tempQueue.enqueue(this);
        CircleIntQueue tempIntQueue = new CircleIntQueue();
        tempIntQueue.enqueue(0);

        BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
        int tempIndex = tempIntQueue.dequeue();
        while (tempTree != null) {
            valuesArray[i] = tempTree.value;
            indicesArray[i] = tempIndex;
            i++;

            if (tempTree.leftChild != null) {
                tempQueue.enqueue(tempTree.leftChild);
                tempIntQueue.enqueue(tempIndex * 2 + 1);
            } // Of if

            if (tempTree.rightChild != null) {
                tempQueue.enqueue(tempTree.rightChild);
                tempIntQueue.enqueue(tempIndex * 2 + 2);
            } // Of if

            tempTree = (BinaryCharTree) tempQueue.dequeue();
            tempIndex = tempIntQueue.dequeue();
        } // Of while
    }// Of toDataArrays


这一部分理解有难度,而且几个类开始的时候都没有找到。主要就是使用两个队列,然后一个是处理数值一个是处理下标的,然后分别入队出队,存储在数组中。因为下标从0开始,所以左孩子是*2+1,右孩子则是*2+2。包括里面的一些用法“this",还有强制转换的,接触的较少,应该也需要二刷。

最后附上运行结果

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值