, according to the nature of the red and black trees, the grand must not be empty, and must be black. Suppose in this case cur's uncle uncle is red. Scenario 2:Suppose Cur's uncle node does not exist or exists and is black. 1, ass
conditions, it must adjust the color and rotate the tree, for example:
Suppose we insert nodes 3, 8, 35, and 75 respectively. According to the Binary Search Tree rules, after these four nodes are inserted, we will find that they all break the rules of the red and black trees, therefore, we must adjust the tree, that is, rotate the tree and change the color of the node.
2. Insert nodes on the
Next to the previous article.
Insert the red/black tree!
The deletion of the red/black tree is further considered in various situations.
First, consider the single support of the red/black tree, that is, only the parent node has
About red and black treesRed-black tree is a self-balancing binary search tree, also has the characteristics of a two-fork tree, keeping the right side is always greater than the left node key features. The AVL tree mentioned earlier is also a variant of the binary search tree, which does not reach the height of the AVL tree, in other words, its height is not as
function to restore the balance. The following is a complete algorithm:
Template Void rb_tree Node
While (x! = Root) (x-> cr = BLACK )){If (x = x-> parent-> left ){W = x-> parent-> right;If (w-> cr = RED ){// Case 1: w is RED. We change the color// X's parent and w, then perform a left rotate on// Parent of x, then
The red and black trees belong to the balanced binary tree. It is not strictly because it does not strictly control the difference between the left and right subtree height or the number of nodes is less than or equal to 1, but the red black tree height remains the average log (n), and the worst
node is red, delete it directly.(2) If the leaf node is black, the sibling node has no sub-nodes.FIX: brother Node B is painted red, parent p is painted black. Note: After the operation of the node is all left child, right child for symmetrical operation can(3) The leaf knot is bl
A red-black tree is a binary lookup tree, but adds a storage bit to the node's color at each node, either red or black. The red-black tree ensures that no path will be twice times longer than the other paths, and thus is nearly ba
sortable key/numeric data. It is different from the cardinality tree (used to efficiently store sparse arrays, so using long integers strands insert/access/delete nodes and hash tables (it can easily be sequentially traversed without sorting, but you have to set the specific size and hash function, while the red-black tree gracefully extends the storage of any key) Red
destroyed, red black tree adjustment, mainly meet these two conditions can be1 insertingIn order to satisfy the 4 and 52 conditions, the key point is: the inserted node must be a red node, because the number of black nodes from a node to each leaf node is the same as the original, will not change, so that the nature o
; Info;}
// If the deleted y node is a black node, the red and black properties of the tree will be damaged, that is, the black height will change, and you need to adjust it.If (Y-> color = black){Rb_delete_fixup (t, x );}
// In this cas
Recently, because of the support for CCache to join the red-black tree, find the code that has been implemented as a reference, this only to find that the original implementation is problematic, but also blame my test case writing is not good, just the insertion of the test, I have read this code and caused confusion of friends to apologize.
This time, all the c
1. The nature of the red-black tree.1) The root node is black.2) All nodes are either red or black.3) The child of the red node must be black.4) all with the leaf node end, and the leaf
maintain the balance after insertion. In a red-black tree, we use two tools to maintain balance:(1) Re-coloring(2) RotationFirst, you'll try to recolor it first, and if it doesn't work, use rotation. Also, the color of the empty node defaults to black.Based on the color of the tertiary node, there are two main scenarios for this algorithm. If the tertiary node is red
node has a black node, and that the left subtree of the parent node is 1 smaller than the right subtree, requiring adjustments to it, which satisfies the red-black tree limit. Because this problem stems from black node n has black sub-node, we call it "double
right child, they are all the same operation (in fact, there are 4 kinds of cases, here I omitted not to introduce, these scenes are just to adjust the color, No need to rotate the tree).
2.2 Uncle BlackWhen the Uncle node is black, it needs to be rotated, and the following diagram shows all the rotations possible: (CASE1 and CAEs 2 are the larger left nodes to the top)
Case 1:"first rotation, then discol
Two forks The basic operation of the search tree can be done within O (h), however the height of the number is higher, and may not be faster than executing on the linked list. red and black trees are a guarantee in the search tree of the worst case time complexity O (LG (n))The nature of red and
node into the binary treeX->left =New_node; Elsex->right =New_node; New_node->parent = x;//set the father of the new node if(X->color = = red)//if the new node's father is red, adjustinsert_fixup (New_node, tree); }}The new node must be red, so it will not violate rule 4, but it may violate rule 3. This means that if the father of the inserted node is
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 eas
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.