Shallow solution of TreeSet and TreeMap

Source: Internet
Author: User
Tags comparable

The relationship between TreeSet and TreeMap:

1. TreeSet actually uses treemap to organize the data, because a navigablemap<e,object> interface instance variable is saved in TreeSet, and the implementation class of the interface is TreeMap

2. Both TreeSet and treemap use binary tree data structures to store

The data saved in 3.TreeSet and TreeMap requires the implementation of the comparable interface and overrides the CompareTo () method In addition to custom types that have default order types such as Integer and string.

TreeSet and TreeMap Add data:

The Add method of TreeSet calls the put method of TreeMap

The implementation of the put () method of the TreeMap,

 Publicv put (K key, V value) {Entry<K,V> T =Root; //determine if there is a root node in a binary tree if there is a bed-building root node        if(T = =NULL) {root=NewEntry<k,v> (key, value,NULL);//Creating the root nodesize = 1;//set the number of elements in the collection to 1modcount++; return NULL; }        intCMP; Entry<K,V> parent;//Declaring parent nodescomparator<?Superk> CPR = Comparator;//Create a comparer        if(CPR! =NULL) {//The collection has a custom comparer             Do{Parent= t;//set the parent node to T (the first t is the root node)CMP = Cpr.compare (key, T.key);//Compare the key of the root node with the key in the parameter                if(CMP < 0)//Key<t.keyt =T.left; Else if(CMP > 0)//Key>t.keyt =T.right; Else                    returnT.setvalue (value);//Key==t.key} while(t! =NULL); }        Else{//The collection does not have a custom comparer            if(Key = =NULL)                Throw NewNullPointerException (); Comparable<?Superk> k = (comparable<?Superk>) key;//Comparator for parameter usage type             Do{Parent= t;//set the parent node to T (the first t is the root node)CMP = K.compareto (T.key);//Compare the key of the root node with the key in the parameter                if(CMP < 0)//Key<t.keyt =T.left; Else if(CMP > 0)//Key>t.keyt =T.right; Else                    returnT.setvalue (value);;//Key==t.key} while(t! =NULL); } Entry<K,V> e =NewEntry<k,v> (key, value, parent);//encapsulates an incoming parameter as a collection element        if(CMP < 0)//e<parentParent.left =e; Else    //e>parentParent.right =e;        Fixafterinsertion (e); Size++; Modcount++; return NULL;}

The following is a graphical way to make a more intuitive display

First time Assignment:

Store this value as the root node

Second assignment:

Value is less than the root node

Value is greater than the root node

Third time assignment

Assuming that the assignment is 3 its first and the root node "2" is compared with a value greater than "2" attempting to assign it to the right child node of the root node "2" but found that it already has a value, the right child node "2" of the lookup 4 is compared to the value of "4" so that the result is assigned:

And so the final data structure is similar to the tree structure shown

TreeSet and TreeMap Get values

The obtained value is similar to the assignment, and is compared to its child nodes starting from the root node until it finds the element to be sequenced.

Source:

Getentry (key) method source code:
FinalEntry<k,v>getentry (Object key) {if(Comparator! =NULL)    //A collection exists with a custom comparer            returnGetentryusingcomparator (key); if(Key = =NULL)            Throw NewNullPointerException (); Comparable<?Superk> k = (comparable<?SuperK>) key; Entry<K,V> p = root;//Get root node         while(P! =NULL) {//The Joghen node is not empty and the node is traversed            intCMP =K.compareto (P.key); if(CMP < 0) P=P.left; Else if(CMP > 0) P=P.right; Else                returnp; If k compares the return value to the key of the node =0(returns 0 for its equality) returns the node}return NULL; }
Getentryusingcomparator (key) source code:
//using a custom comparer for comparison traversalFinalEntry<k,v>getentryusingcomparator (Object key) {k K=(K) key; Comparator<?Superk> CPR =Comparator; if(CPR! =NULL) {Entry<K,V> p =Root;  while(P! =NULL) {                intCMP =Cpr.compare (k, P.key); if(CMP < 0) P=P.left; Else if(CMP > 0) P=P.right; Else                    returnp; }        }        return NULL; }

Sort of TreeSet and TreeMap

Its sort will be based on the hierarchical relationship of the second fork tree from the left-most leaf node to find its parent node when looking for the right child node of its parent node if there are child nodes under the right child node of its parent node the right node is traversed until it finds the left-most leaf-level node under the right node. After the parent node traversal is complete, the parent node of the parent node is traversed in the same way, and so on until the two fork tree Traverse complete

Shallow solution of TreeSet and TreeMap

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.