TreeMap inheritance relations, and hashmap with inheritance in Abstractmap, belong to the brotherly relationship.
Public class Treemap<k,v> extends abstractmap<k,v> implements Navigablemap<k,v> Cloneable, java.io.Serializable
-1. red and black trees based on navigablemap implementation
-2. Sort by natrual ordering (comparator==null, default) or Comparator -defined order
-3. Basic operation containskey,get,put with log (n) time complexity
-4. non-thread safe
Sync:SortedMap m = collections.synchronizedsortedmap (new TreeMap (...));
Red-Black tree node type, default is black
Static Final class Implements Map.entry<k,v> { K key; V value; Entrynull; Entrynull; Entry<K,V> parent; boolean color = BLACK; ...}
ContainsKey Method:
Public Boolean ContainsKey (Object key) { returnnull;}
FinalEntry<k,v>getentry (Object key) {//Offload comparator-based version for sake of performance if(Comparator! =NULL) returnGetentryusingcomparator (key); if(Key = =NULL) Throw NewNullPointerException (); Comparable<?Superk> k = (comparable<?SuperK>) key; Entry<K,V> p =Root; //Looks like a two-fork find tree while(P! =NULL) { intCMP =K.compareto (P.key); if(CMP < 0) P=P.left; Else if(CMP > 0) P=P.right; Else returnp; } return NULL;}
Containsvalue Method:
Public Boolean Containsvalue (Object value) { fornull; e = successor (e)) if (Valequals (value, E.value)) return true ; return false ;}
Found the leftmost node in the tree, if the value in the left dial hand tree is less than the right subtree, this means that the first comparison node is the smallest node.
Final Entry<k,v> getfirstentry () { Entry<K,V> p = root; if NULL ) whilenull) = p.left; return p;}
Successor is actually a node of the next node, the so-called next, is sorted by order after the next node. As you can see from the code, if the right subtree is not empty, the smallest node in the right subtree is returned. If the right subtree is empty, it's going to go up backwards.
Static<K,V> treemap.entry<k,v> Successor (entry<k,v>t) {if(T = =NULL) return NULL; Else if(T.right! =NULL) {Entry<K,V> p =T.right; while(P.left! =NULL) P=P.left; returnp; } Else{Entry<K,V> p =t.parent; Entry<K,V> ch =T; while(P! =NULL&& ch = =p.right) {ch=p; P=p.parent; } returnp; }}
Final entry<k,v> getceilingentry(K key): Find an element of X.key >= key, and X is the smallest of the elements that satisfy the condition .
Public V put(K key, V value): If the tree is empty, create a root node. or insert a node in the tree if key is in the tree, SetValue sets a new value, otherwise, inserts a new node. After inserting, call fixafterinsertion to adjust the balance.
The TREEMAP delete node is maintained in the same way that the smallest node in the right subtree of the deleted node is exchanged with the truncated point.
Red black tree is a self-balancing binary search tree , a data structure used in computer science, and a typical use is to implement associative arrays .
Nature 1. The nodes are red or black.
Nature 2. The root node is black .
Nature 3. Each leaf node (nil node, empty node) is black .
Nature 4. the two child nodes of each red node are black . (no two consecutive red nodes can be found on all paths from each leaf to the root).
Nature 5. all paths from any node to each of its leaves contain the same number of black nodes.
The number of black nodes contained in the path from the root node to the leaf node is called the " Black height (black-height)" of the tree.
the longest path in a red-black tree is a red-black alternate path .
Red black tree with a black height of N, the shortest path length from root to leaf node is N-1, and the longest path length is 2 * (N-1).
Red and black trees are not really balanced binary trees , but in practical applications, the statistical performance of red and black trees is higher than that of balanced binary trees, but the extreme performance is slightly worse.
However, the red-black tree implemented in Java will use NULL to represent empty nodes , so the black leaf node will not be visible when traversing the red-black tree, but each leaf node is seen as red.
TreeMap Source Code Analysis