I was going through the source code of TreeMap in JAVA. As per JAVA doc:
A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.
In the source code I found that an Inner Class Entry was used as a node.
static final class Entry<K,V> implements Map.Entry<K,V> {
K key;
V value;
Entry<K,V> left = null;
Entry<K,V> right = null;
Entry<K,V> parent;
boolean color = BLACK;
....
As for the defination of Red-Black tree. From Wikipedia I found:
A red–black tree is a type of self-balancing binary search tree, a data structure used in computer science.
The self-balancing is provided by painting each node with one of two colors (these are typically called 'red' and 'black', hence the name of the trees) in such a way that the resulting painted tree satisfies certain properties that don't allow it to become significantly unbalanced. When the tree is modified, the new tree is subsequently rearranged and repainted to restore the coloring properties. The properties are designed in such a way that this rearranging and recoloring can be performed efficiently.
I tried to analyse the souce code but couldn't understand the following:
Suppose I am already having two keys "C" and "E" in the Tree and then I am adding "D". How the nodes will be arranged (Using natural ordering).
How is self-balancing of Tree is implemented in the java source code.
I tried searching for detail implementationof TreeMap but was unable to find any article such as the following article I have found for HashMap
Since yesterday I am hanging on this Tree :( Can someone please help me get down...