The realization of treemap--red-black tree

Source: Internet
Author: User

http://cmsblogs.com/?p=1013 original source.

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.

L-Right-hand rotation

   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 orderly, through the Comparator interface we can treemap the internal ordering of the precise control of the        private final comparator<? Super K> Comparator ;        TreeMap Red-Black node, for TreeMap inner class        private transient entry<k,v> root = null;        Container size        private transient int size = 0;        TreeMap modified the number of        private transient int modcount = 0;        Red black tree node color--Red        private static Final Boolean red = false;        Red black tree Node color--Black        private static Final Boolean black = true;

for leaf node entry is the inner class of TreeMap, which has several important properties:

Key        K key;        Value        V value;        Left child        entry<k,v> = null;        Right child        entry<k,v> = null;        Father        entry<k,v> parent;        Color        Boolean color = BLACK;

Note: The front is just appetizer, here is the top priority of this blog post, in the following two sections I will focus on the TreeMap put (), delete () method. By these two methods we will understand the core algorithm of adding and removing nodes of red-black tree.

       Third, TreeMap put () method

Before we understand the put () method of TreeMap, we first understand the algorithm of adding nodes to the red-black tree.

       Red and black trees add nodes

Red black tree in the new node process is more complex, complex and complex it must also be based on the five-point specification mentioned above, while the rules 1, 2, 3 are basically satisfied, the following we mainly discuss the rules 4, 5. Suppose we have a simple tree here, we specify that the new node is n, its parent node is P, p's sibling node is U, P's parent node is G.

There are three key areas for inserting new nodes:

1. Insert new node always red node.

2, if the parent node of the insertion node is black, can maintain the nature.

3. If the parent node of the insertion node is red, it destroys the nature. Therefore, the insertion algorithm is to maintain the properties by re-coloring or rotating.

in order to ensure that the following elaboration clearer and according to the reference, I here will be the red and black tree Five points to be affixed again:

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.

  • First, to follow the node
  • Jovin inserted node n does not have a parent node, it is directly inserted according to the node, and the color is set to black. (One (1))

    Second, the parent node is black

    In this case the new node n is also directly inserted, while the color is red, because according to rule four it will exist two black leaf nodes, the value is null. Also, because the new node n is red, the path through its child nodes will still hold the same number of black nodes, which also satisfies rule 5. (One (2))

    (Figure I)

    Third, if the parent node p and P Brothers node u are red

    In this case, if the direct insertion will definitely appear unbalanced. What's the deal? P, u nodes turn black, g nodes turn red. The number of black nodes above these paths is still the same because the path through node p and U must pass through G. But after the above processing, it is possible that the parent node of the G node is also red, this time we need to treat the G node as a new node recursion.

           Iv. if the parent node p is red, the Uncle node U is black or missing, and the new node n is the right child of P node

    For this case we have a left rotation for the new node n, p. The result here is not actually complete, it is not balanced (violates rule four), this is the operation we need to proceed to situation 5.

  • V, parent node P is red, Uncle node U is black or missing, new node n is parent node p left child

  • This situation may be due to situation four, or it may not be. For this case, the P node is centered on the right rotation, and in the tree produced after rotation, node p is the parent node of node N and G. But this tree is not standard, it violates rule 4, so we will exchange the color of P, g node, so that it satisfies the specification. All paths begin with the same number of their black nodes, but now all paths are changed to P, and P is the only black node of the whole tree, so the adjusted tree also satisfies spec 5.

      • There are five cases of new nodes in the red and black tree, and these five cases cover all the new possibilities, and no matter how complex the red and black tree is, it can be generated in five different situations. Here's an analysis of how TreeMap in Java implements red-black trees.

The realization of treemap--red-black tree

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.