the nature and definition of red-black tree
The Red-black tree (Red-black) is a two-fork lookup tree that satisfies the following properties:
1. Each node is either red or black.
2. The root node is black.
3. All leaf nodes are black (they are actually null pointers, as shown in nil). The leaf node does not contain any keyword information, and all query keywords are on a non endpoint.
4. The two subnodes of each red node must be black. In other words: there cannot be two consecutive red nodes from each leaf to the root of all paths
5. All paths from any node to each of its leaves contain the same number of black nodes
Black Depth-the number of black nodes from a node x (excluding the node x itself) to the path of the leaf node (including the leaf node), the black depth, called the node X, is recorded as BD (x), and the black depth of the root node is the black depth of the red-black tree. The black depth of the leaf node is 0. For example: The above figure BD (8) =2,BD (1) =1 =2,BD
Internal node-non-endpoint of red-black tree
External node--the leaf node of the red-black tree
The correlation theorem of red and black trees
1. The longest possible path from root to leaf is not more than twice times longer than the shortest possible path.
According to the nature of the above 5 we know that the red and black trees on each path are 3 black nodes. Therefore, the shortest path length is 2 (the path without the red node). Again according to Nature 4 (two red nodes cannot be connected) and properties 1,2 (leaves and roots must be black nodes). Then we can conclude that a path with 3 black nodes can have at most 2 red nodes (the red and black intervals exist). This means that the black depth is 2 (the root node is also black) the longest path of the red-black tree is 4, the shortest path is 2. From this we can see that the red and black trees are roughly balanced. (Of course, than the balance of the binary tree to some, AVL balance factor of up to 1)
2. Red-Black Tree height (h) is not greater than twice times the black depth of the red-black Tree (BD), that is, h<=2bd
According to Theorem 1, we are not difficult to illustrate this point. BD is the shortest path length of a red-black tree. The possible longest path length (the maximum tree height) is the red and black alternate path, equal to 2BD. So h<=2bd.
3. Tree height H<=2log (n+1) of a red-black tree having n internal nodes (excluding leaf nodes)
Below we first prove that a red-black tree with n internal nodes satisfies the n>=2^bd-1. This can be proved by mathematical induction, applied to the tree height H. When h=0, this is equivalent to a leaf node, black height BD is 0, and the number of internal nodes n is 0, at this time 0>=2^0-1 set up. If the tree is high h<=t, the n>=2^bd-1 is established, we remember that the number of internal nodes in the left subtree of the root node of a red-black tree with a tree high as t+1 is NL, the number of internal nodes of the right subtree is NR, and the black height of the two subtrees is BD ' (note that the black height of the two subtrees is necessarily the same), Obviously the tree of these two subtrees is high <=t, so there are nl>=2^bd '-1 and nr>=2^bd '-1, add the two inequalities nl+nr>=2^ (BD ' + 1)-2, add the inequality around 1, get n>=2^ (BD ' + 1) -1, obviously BD ' +1>=BD, so the preceding inequalities can become n>=2^bd-1, which proves that a red-black tree with n internal nodes satisfies the n>=2^bd-1.
2,H<=2BD according to the theorem. namely n>=2^ (H/2)-1, then H<=2log (n+1)
From here we can see that the red-black tree lookup length is no more than 2log (n+1), so its lookup time complexity is also O (log n) level.
operation of the red and black tree
Because each red-black tree is also a special two-fork lookup tree, the lookup operation on the red-black tree is the same as the lookup operation on the normal binary lookup tree. However, inserting and deleting operations on red and black trees can result in no longer conforming to the nature of the red-black tree. Restoring the red-black tree's properties requires a small amount (O (log n)) color change (actually very fast) and no more than three tree rotations (two times for insert operations). Although inserts and deletes are complex, the operation time can still remain O (log n) times.
Insert Operation
We first add the node to the two-fork lookup tree and mark it as red. (If set to black, it will cause the root to the path of the leaf there is a road, more than one additional black node, this is difficult to adjust.) However, when set to a red node, you may be able to cause two consecutive red node conflicts, which can be adjusted by color flips and tree rotation. What to do next depends on the color of the other neighboring nodes. As in the human family tree, we will use the term Uncle node to refer to the sibling node of a node's parent node.
Assuming that the new node is n, the Father node is P, and uncle's node is the UI (Uncle node is the sibling of some column p), and grandfather Node G (father of the Father node P). Each of these will be shown below, and we will use the C sample code to show it. The following functions allow you to find the uncle and grandparent nodes of a node: C code node grandparent (node N) {return n->parent->parent; Node uncle (node N) {if (n->parent = = Grandparent (n)->left) return grandparent (n)->righ T else return grandparent (n)->left; }
situation 1. The current red-black tree is empty, the new node n is located on the root of the tree, and there is no parent node.
It's very simple at this point, we'll just insert a black node n (satisfying Nature 2), and in other cases the inserted n is red (the reason is mentioned earlier). C code void Insert_case1 (node n) {if (n->parent = NULL) N->color = black; else Insert_case2 (n); Insert Case 2}
situation 2. The parent node p of the new node n is black.
In this case, we insert a red node n (satisfying nature 5). Java code void Insert_case2 (node n) {if (N->parent->color = black) return;//tree still valid else INSERT_CASE3 (n); Insert Case 3}
Note: Under the condition 3,4,5, we assume that the new node has a grandparent, because the parent node is red, and if it is the root, it should be black. So the new node always has an Uncle node, although it may be a leaf under conditions 4 and 5.
situation 3. If the parent node p and Uncle node are both red.
As shown in the following figure, because the newly added n node must be red, we can redraw the parent node P (guaranteed property 4) and N's Uncle node U (guaranteed Nature 5) to black. If at this point the grandfather node G is the root, then the change ends. If not the root, the grandparent node is redrawn to red (guaranteed property 5). However, the father of G may also be red, in order to guarantee the nature of 4. We take G recursion as a new addition to the node n in a variety of cases of the review.
C code void Insert_case3 (node n) {if (Uncle (n)!= NULL && uncle (n)->color = = RED) {N->paren T->color = black; Uncle (n)->color = black; Grandparent (n)->color = RED; Insert_case1 (grandparent (n)); else Insert_case4 (n); }
Note: Under Scenarios 4 and 5, we assume that the parent node p is the left child of the grandparent node G. If it is the right child node, the left and right in scenario 4 and case 5 should be reversed.
situation 4. The parent node p is red while Uncle node U is black or missing; In addition, the new node n is the right child node of its parent node p, and the parent node p is the left child node of the grandparent junction G.
In this case, we make a left rotation to replace the new node and its parent node (the same as the left rotation of the AVL tree). This causes some paths to pass through one of the new node n or the parent node p that they did not pass before, but the two nodes are red, so nature 5 does not fail. But the current situation will violate the nature of 4, so then we press the following case 5 to continue processing the previous parent node p.
C code void Insert_case4 (node N) {