zoukankan      html  css  js  c++  java
  • List的数据结构

    从这张图片说起:TreeList的实现结构:
    首先是构建函数 TreeList(Collection coll),调用增加函数:
    public void add(int index, Object obj) {
            modCount++;
            checkInterval(index, 0, size());
            if (root == null) {
                root = new AVLNode(indexobjnullnull);
            } else {
                root = root.insert(indexobj);
            }
            size++;
        } 
    //由此可以看出,Treelist采用的是平衡二叉树的实现的方式 ,并且以根节点作为成员变量,首先是AVL的数据结构:
    static class AVLNode {
            /** The left child node or the predecessor if {@link #leftIsPrevious}.*/
            private AVLNode left;
            /** Flag indicating that left reference is not a subtree but the predecessor. */
            private boolean leftIsPrevious;
            /** The right child node or the successor if {@link #rightIsNext}. */
            private AVLNode right;
            /** Flag indicating that right reference is not a subtree but the successor. */
            private boolean rightIsNext;
            /** How many levels of left/right are below this one. */
            private int height;
            /** The relative position, root holds absolute position. */
            private int relativePosition;
            /** The stored element. */
            private Object value;
    }
    节点的新建:
     private AVLNode(int relativePosition, Object obj, AVLNode rightFollower, AVLNode leftFollower) {
                this.relativePosition = relativePosition;//相对的位置,index来进行标注
                value = obj;
                rightIsNext = true;
                leftIsPrevious = true;
                right = rightFollower;
                left = leftFollower;
            }  
    新建完成,根节点的建立,下一步就是继续的增加,采用的是插入的方式:
    AVLNode insert(int index, Object obj) {
                int indexRelativeToMe = index - relativePosition;
                if (indexRelativeToMe <= 0) {
                    return insertOnLeft(indexRelativeToMeobj);
                } else {
                    return insertOnRight(indexRelativeToMeobj);
                }
    }
    首先我们看 插入左节点:
     private AVLNode insertOnLeft(int indexRelativeToMe, Object obj) {
                AVLNode ret = this;
                if (getLeftSubTree() == null) {
                    setLeft(new AVLNode(-1, objthisleft), null);
                } else {
                    setLeft(left.insert(indexRelativeToMeobj), null);
                }
                if (relativePosition >= 0) {
                    relativePosition++;
                }
                ret = balance();
                recalcHeight();
                return ret;
            }  

      





  • 相关阅读:
    IBM Lotus网站荟萃
    Lotus 深入浅出系列——前言(二)IBM Lotus开发人员的几个境界
    在IIS上配置和测试Perl脚本
    网站推广
    iTOP开发板MiniLinuxC程序调用shell命令
    iTOP4412开发板_驱动_adc驱动升级和测试例程
    最近想入手树莓派3来学习编程同学没有选择4412开发板吗?
    iTOP4412开发板串口转接小板的使用文档
    学习嵌入式4412开发板手把手配套视频_2000人群组在线交流
    电子医疗设备创新研发应该用i.MX6Q开发板吗?为医疗设备提供解决方案
  • 原文地址:https://www.cnblogs.com/zhailzh/p/4825910.html
Copyright © 2011-2022 走看看