From this picture: treelist implementation structure: The first is to build the function treelist (Collection coll), call the Add function: public void Add (int index, Object obj) { Modcount+ +;checkinterval (index, 0, size ()); if (root = = null) { Root = new avlnode (index, obj, null, null);} Else { Root = root. Insert (index, obj); } size+ +; }It can be seen that treelist adopts the method of realizing the balanced binary tree, and takes the root node as the member variable, first of all the data structure of AVL:
static class Avlnode { /** the left child node or the predecessor if {@link #leftIsPrevious}. * / Private avlnode left; /** Flag indicating that left reference was 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 was not a subtree but the successor. * * Private boolean rightisnext; /** How many levels of left/right is below this one. * * private int height; /** The relative position, root holds absolute position. * / private int relativeposition; /** the stored element. * / Private Object value;}New for node: Private avlnode (int relativeposition, Object obj, avlnode rightfollower , Avlnode leftfollower) {This . relativeposition = relativeposition;//relative position, index to label Value = obj; Rightisnext = true; leftisprevious = true;Right = rightfollower;Left = leftfollower; }
New complete, the root node is established, the next step is to continue to increase, using the way of inserting: avlnode Insert (int index, Object obj) { int indexrelativetome = index - relativeposition; if (indexrelativetome <= 0) { return insertonleft (indexrelativetome, obj); } Else { return insertonright (indexrelativetome, obj); }}First we look at the insertion of the left node: Private avlnode insertonleft (int indexrelativetome, Object obj) {avlnode ret = this; If (getleftsubtree () = = null) {Setleft (new Avlnode ( -1, obj, This, left), null);} Else {setleft (left. Insert (indexrelativetome, obj), null); } if (relativeposition >= 0) { relativeposition+ +; } ret = balance ();recalcheight (); return ret; }
From for notes (Wiz)
Data structure of List