First, the concept
The red and black tree is a binary search tree, which adds a storage bit to each node to represent the node's color, which can be red or black. By constraining the color of each node on a simple path from root to leaf, the red-black tree ensures that no path is twice times longer than the other path, and thus is approximately balanced.
Second, the definition
A red and black tree is a two-fork search tree that satisfies the following red-black nature:
1. Each knot is either red or black;
2, the root node is black;
3, each leaf node (NIL) is black;
4, if a node is red, then its two sub-nodes are black;
5, on each node, from the node to all of its descendants of the leaf nodes of the simple path, the same number of black nodes (the number of this black node is called Black height ).
Three, and the difference of the balanced binary tree
Balanced binary tree is completely balanced, and the red black tree is a local balance, can ensure that no path will be twice times longer than the other path, and less adjustment, high performance, so it is widely used, in the STL map and set are implemented by the red black tree.
Iv. Definition of a node
Enum node_color{red,black};typedef struct rbnode{ int key;//data information struct Rbnode *left;//left child struct Rbnode *right;//Right child struct Rbnode *p;//parent node enum node_color color;//node color}
V. Examples (all null pointers in the figure point to the leaf node nil)
Six, rotation
Objective: To insert and delete red and black trees, which may cause the tree not to satisfy the nature of the red-black tree, in order to maintain the nature of the red-black tree, it is necessary to change the color of some nodes in the tree and the pointer structure. Changing the pointer structure is done by rotation, which is a local operation of the search tree which can keep the nature of the binary search tree.
1, L-(left_rotate)
Illustration: This operation is done by three departments.
Code:
The node x L-Rbnode *left_rotate (Rbnode *t, Rbnode *x) { rbnode *y; Y=x->right;//set y //step1:tunrn y ' s left subtree into X's right subtree x->right=y->left; if (y->left!=nil) y->left->p=x; Step2:link X ' s parent to y y->p=x->p; if (x->p==nil) t=y; else if (x==x->p->left) x->p->left=y; else x->p->right=y; Step3:put x on Y ' s left y->left=x; x->p=y; return T;}
2, right-handed (left_rotate)
Illustration: This operation is done by three departments.
Code:
The node x right-Rbnode *right_rotate (Rbnode *t, Rbnode *x) { rbnode *y; Y=x->p;//set y //step1:tunrn x ' s right subtree to Y ' s left subtree y->left=x->right; if (x->right!=nil) x->right->p=y; Step2:link y ' s parent to x x->p=y->p; if (y->p==nil) t=x; else if (y==y->p->left) y->p->left=x; else y->p->right=x; Step3:put y on X ' s right x->right=y; y->p=x; return T;}
Seven, insert
Insert operation The binary sort tree insert operation is basically the same as in the previous post, except that the details change slightly.
Code:
Insert Node Zrbnode *rb_insert (Rbnode *t, Rbnode *z) on Red black tree T {rbnode *x ; Rbnode *y; Y=nil; x=t; while (X!=nil) { y=x; if (Z->key < X->key) x=x->left; else x=x->right; }
z.p=y; if (y==nil) t=z; else if (Z->key < Y->key) y->left=z; else y->right=z;
z->left=nil; z->right=nil; z->color=red; Rb_insert_fixup (t,z);}
Eight, dyeing and adjustment
Because the insert operation may break the red-black tree nature, the rb_insert_fixup (t,z) function adjusts the node color and the structure of the tree so that it remains red-black. -- continue to improve tomorrow night
Red and Black tree C language implementation--An introduction to the algorithm