Java Improvement (27)-----TreeMap
TreeMap implementation is the implementation of red-black tree algorithm, so to understand the treemap must have a certain understanding of red and black trees, in fact, the name of this blog is called: According to the red-black tree algorithm to analyze the implementation of TREEMAP, But it's better to be consistent with the Java Improvement blog or to be called TreeMap. With this blog post you can get the following points of knowledge:
1. The basic concept of red and black trees.
2, red black tree to increase the node, delete node implementation process.
3, red black tree left rotation, right rotation of the complex process.
4, Java TreeMap is how to implement the red-black tree by put, deleteentry two to add, delete nodes.
I think you must have a deeper understanding of treemap through this blog post. OK, the following first simple popularization of red and black tree knowledge.
I. Introduction to red and black trees
Red black tree is also called red-black binary tree, it is first a binary tree, it concrete binary tree all the characteristics. At the same time the red and black tree is a self-balanced sort of binary tree.
We know a basic two-fork tree. They all need to satisfy a basic nature-that is, any node in the tree has a value greater than its left child node and less than its right child node. According to this basic nature, the efficiency of tree retrieval is greatly improved. We know that in the process of generating a two-fork tree is very easy to unbalance, the worst case is one-sided (only right/Zuozi), which will inevitably lead to the retrieval efficiency of the two fork tree is greatly reduced (O (n)), so in order to maintain the binary tree balance, Daniel proposed a variety of algorithms, such as: AVL,SBT, stretching tree, Treap, red and black trees and so on.
A balanced binary tree must have the following characteristics: It is an empty tree or its left and right two sub-tree height difference of the absolute value of not more than 1, and the left and right two sub-trees are a balanced binary tree. That is, any one of the two fork tree and so on, the height of the left and right sub-tree is similar.
The red and black tree, as its name implies, is a red or black balanced binary tree, which maintains the balance of the two-fork tree by its color constraints. For a valid red and black tree Two fork tree We must add the following rules:
1, each node can only be red or black
2, the root node is black
3, each leaf node (nil node, empty node) is black.
4, if a node is red, then its two sub-nodes are black. This means that no adjacent two red nodes can appear on a path.
5. All paths from any node to each of its leaves contain the same number of black nodes.
These constraints force the key properties of the red-black tree: The longest possible path from the root to the leaf is no more than twice times longer than the shortest possible path. The result is that the tree is generally balanced. Because the worst-case time for operations such as inserting, deleting, and finding a value is proportional to the height of the tree, the theoretical upper limit on the height allows the red and black trees to be efficient in the worst case, unlike the normal two-fork lookup tree. So the red-black tree It is complex and efficient, its retrieval efficiency O (log n). As a typical red and black binary tree.
For the red and black binary tree, it mainly includes three basic operations: left-hand, right-handed, coloring.
(Image from: http://www.cnblogs.com/yangecnu/p/Introduce-Red-Black-Tree.html)
This section references: Http://baike.baidu.com/view/133754.htm?fr=aladdin-----Baidu Encyclopedia
Note: as this article is mainly about Java TreeMap, so there is no very deep understanding of the red and black trees and research, if you want to do more in-depth study LZ provides several better blog:
1. Red and Black Tree Collection
2. analysis of red and black tree data structure
3. red and black trees
Second, TREEMAP data structure
>>>>>> return to protagonist:treemap<<<<<<
TreeMap is defined as follows:
Public class Treemap<k,v> extends abstractmap<k,v> implements Navigablemap<k,v> Cloneable, java.io.Serializable
TreeMap inherits Abstractmap, realizes Navigablemap, Cloneable, serializable three interfaces. Where Abstractmap indicates that TreeMap is a map that supports Key-value collections, navigablemap (more) means that it supports a range of navigation methods, with a navigation method that returns the closest match for a given search target.
The TreeMap also contains several important properties as follows:
//comparator, because the TreeMap is ordered, and through the comparator interface we can control the internal ordering of the treemap with precisionPrivate Finalcomparator<?SuperK>Comparator; //TreeMap Red-black node, for TreeMap's inner classPrivate transiententry<k,v> root =NULL; //Container sizePrivate transient intSize = 0; //treemap number of changesPrivate transient intModcount = 0; //Red and Black tree node color--RedPrivate Static Final BooleanRED =false; //Red and Black tree node color--BlackPrivate Static Final BooleanBLACK =true;
Java-hashtree source parsing + red and black tree