I really want to deepen my understanding of exactly how a TreeSet, particularly the no-argument version that does not use a comparator, maintains the elements that it contains in order. I cannot find a satisfactory explanation anywhere; they are either too basic or way too advanced for me. From my research, it seems that TreeSets actually store their elements in a TreeMap and TreeMaps are actually Red-Black trees. I feel pretty confident of my understanding about how elements are added to red-black trees.
I assume that somewhere in the java API there must be an algorithm or method that performs the insertion of elements into a Red-Black tree. My first question is where in the Java API is that algorithm located?
Also, how exactly is this algorithm invoked? I guess it is invoked by the add(E e) method in the TreeSet class, right? Can someone provide more details on the exact chain of events that occur when adding an element to a treeset.
Lastly, just as an experiment, I gave an object a compareTo() method but DID NOT implement the Comparable interface. When trying to add these objects to a TreeSet, an exception is thrown. I want to understand why an exception is thrown even though the object has a compareTo() method. I guess that there is a method somewhere in the insertion algorithm that requires all objects implement Comparable, is this correct? The stackTrace of the thrown exception points to TreeMap.compare() method. I guess this is the method that requires all objects that are added to a TreeSet implement the Comparable interface but I can not see this TreeMap.compare() method in the API. How can I find more information about this TreeMap.compare() method in the API?
Thank you very much in advance for your help.