, there's a process of adjusting the color./ * * If the parent node is black, it is not violated without adjustment. * instead .... * When inserting a node. The nature of the possible destruction is (4) If a node is red, then its child node is black (2) The root node is black * Insert Repair
node's parent node is red and must violate rule 3 (because the red node is inserted by default), you need to fix the red-black tree at this point. This situation is subdivided into several cases. 4. Parent red, tertiary red paren
Deleting a node can also change the balance of the tree, and the imbalance caused by the deletion is more complex than the balance correction caused by the insertion.
Simplification is a common method in algorithm analysis. Below we will want to delete the node into three main categories: to delete the node as a leaf node, to delete a node only one child node and to delete has two child nodes.
There are two possible colors for deleting nodes, and they need to be treated separately.
To simp
the same, as follows.node n is red:If the node n is red, then its child node must be black, and two children node is a leaf node (because node n can only have a non-leaf child node, so only one side is a black non-leaf child node, the other side is a black leaf knot point, So the number of
A red/black tree is a special binary search tree. It has the following five properties:
1. The root node is black.
2. Each node is either black or red.
3. the two sons of each Red node must be
the triangle, before the color transformation, through this triangle will add a black node, after the color transformation, p becomes red, X1, X2 becomes black, whether through X1 or through X2, or will add a black node.
If the parent node of P is black, there will be no
The fact is always caused by the desire of man,Want to have, and when most people think the same timeIt will only become the life of posterity.ReciprocatingCited:The red and black trees were bornAh (sigh), anyway, I think so.It is slightly more mature than the original, but ears, but it shows its unique styleDescribedDemand, is the cause of the red and
The red/black tree is a binary search tree. to delete the red/black tree, follow the rules of the Binary Search Tree. To delete node Z (5), delete the minimum value of Y in the right subtree of Z and then assign the value of Y to the Z node. :
Then we will consider the red
case, we simply redraw s to red. The result is all paths through s, which are those paths that previously did not pass N, with a black node missing.Figure 6:n's father, S and S's sons are all black processing4) s and S's sons are all Black, but N's father is
The red-black tree is a self-balancing two-fork search tree, a data structure used in computer science, typically for associative arrays (Map/set in C + + STL). It was invented in 1972 by Rudolf Bayer, which he called "symmetric binary B-tree", and its modern name was obtained by Leo J. Guibas and Robert Sedgewick in a paper written in 1978. The red-
some color changes are required.To insert 50,25,75,12,6 as an example, note that node 6 is an outer descendant node, and its parent node is red.In this example, X is an outer descendant node and is a left dial hand node, and X is the outer descendant node and is the right child node, which is a symmetric case. By creating a tree with 50,25,75,87,93, and then drawing a drawing again, this is omitted.The likelihood 3:p is
One, red and black tree introductionWhat is a red-black tree? Why do you need a red-black tree?finding, inserting, deleting, finding the maximum node, finding the minimum node, finding the precursor/successor node is a common requ
2)------------TemplateclassT>voidRedblacktree//--------------The judging of the rotation condition---------------TemplateclassT>nodeif(Itemreturntheparent->lchild; }Else{Itemreturntheparent->rchild; }}//-----------Adjust the operation--------------TemplateclassT>voidRedblacktree//Discolorationcurrent->color=red; current->lchild->color=black; current->rchild->color=b
the tree will be damaged. To maintain these properties, we need to change the color of some knots in the tree and the pointer structure. The pointer structure is modified by rotation. Rotation is a local operation that maintains the infix order of nodes in the tree. There are two rotation methods: left and right.
2. Insert
Case 1: As shown in, X is the newly inserted node and red. P is the parent node of
As a data structure, red and black trees are not simple, because all kinds of propaganda let it too mysterious, online collected a lot of articles about the red and black trees, nothing more than the same, introduce concepts, analysis performance, paste code, and then give a sentence of sin, it is the worst
is a weakly balanced binary tree (due to the fact that if the balance can be rolled out, the same node case, the AVL tree height is lower than the red Black tree ), Compared to the strict AVL tree, it has fewer rotations, so for search, insert, and delete operations, we use red-bl
in order to make the property 5 not damaged, we will rotate the previous node w red, so that the entire tree meets all the properties
In a word, inserting a vertex may change the black height of several paths. We need to re-satisfy the properties of them by rotating and changing the color, this is also the reason why the first two of the three cases have switched to the third
root node;3 The left and right subtrees of any node are also two-fork lookup trees, respectively. 4 No nodes with key values equal (no duplicate nodes). because the height of a two-fork search tree that is randomly constructed from N nodes is LGN, it is logical that the execution time of the generic operation of the binary lookup tree is O (LGN). But if a binary lookup tree is degraded into a linear chain with n nodes, the worst-case run time for the
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.