Data structure of List

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.