Research on TreeMap red-black tree algorithm by analyzing JDK source code

Source: Internet
Author: User
Tags addall constructor sort

Introduction: TreeMap and TreeSet are two important members of the Java Collection Framework, where TreeMap is a common implementation class for the Map interface, and TreeSet is a common implementation class for Set interfaces. Although the interface specification implemented by HASHMAP and HashSet is different, the TreeSet is implemented through TREEMAP, so they are implemented exactly the same way. and the implementation of TREEMAP is the red-black tree algorithm.

TREEMAP is the implementation of the red-black tree data structure, also said to be a self-balanced sort of binary tree, so as to ensure that when the need to quickly retrieve the specified node.

The relationship between TreeSet and TreeMap

To get a sense of the relationship between TreeMap and TreeSet, let's look at some of the source code for the TreeSet class:

public class Treeset<e> extends abstractset<e>
Implements Navigableset<e&gt, Cloneable, java.io.Serializable
{
Use the Navigablemap key to save the elements of the set collection
private transient navigablemap<e,object> m;
Use a PRESENT as all of the value of the Map collection.
private static final Object PRESENT = new Object ();
The constructor of the package access permission to create the set collection with the specified Navigablemap object
TreeSet (navigablemap<e,object> m)
{
THIS.M = m;
}
Public TreeSet ()//①
{
Create a new TreeMap in a natural sort way,
Create a TreeSet based on the TreeSet,
Use the TREEMAP key to save the elements of the set collection
This (new treemap<e,object> ());
}
Public TreeSet (COMPARATOR&LT; super e> Comparator)//②
{
Create a new TreeMap in a custom sort way.
Create a TreeSet based on the TreeSet,
Use the TREEMAP key to save the elements of the set collection
This (new treemap<e,object> (comparator));
}
Public TreeSet (COLLECTION&LT; extends e> c)
{
Call the ① constructor to create a TreeSet with TreeMap to save the collection elements
This ();
Add all the elements in Collection set C to TreeSet
AddAll (c);
}
Public TreeSet (sortedset<e> s)
{
Call the ② constructor to create a TreeSet with TreeMap to save the collection elements
This (S.comparator ());
Add all elements in SortedSet set S to TreeSet
AddAll (s);
}
TreeSet's other methods are simply calling the TreeMap method to provide the implementation
...
public boolean addall (collection<? extends e> c)
{
if (m.size () = = 0 && c.size () > 0 &&
C instanceof SortedSet &&
M instanceof TreeMap)
{
Cast the C set to the SortedSet collection
sortedset<? Extends e> set = (sortedset<? extends e>) C;
Cast m set to TreeMap set
treemap<e,object> map = (treemap<e, object>) m;
comparator<? Super e> cc = (comparator< super e>) Set.comparator ();
comparator<? Super E> MC = Map.comparator ();
If CC and MC two Comparator equal
if (cc = MC | | (CC!= NULL && cc.equals (MC))
{
To add all the elements in the Collection to the key of the TREEMAP set
Map.addallfortreeset (set, PRESENT);
return true;
}
}
Call the AddAll () method of the parent class directly to implement the
return Super.addall (c);
}
...
}

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.