Red and Black Tree rules: one (root) foot (leaf node) black (black), black (the number of black nodes starting from any node to a null node is equal) red not connected (red node cannot be connected)
The following is a simple implementation of the insertion and rotation functions of the red and black trees:
#include <iostream>#include <Assert.H>#include <string.H>using namespace Std;typedef intType; typedef enum{red=0, Black}color;//Node colortypedef struct node{color color;//Node color TypeKeythe value of the//nodestruct Node*Left*Right*Parent;//Left, right and parent node pointers}*Pnode;//Node pointer//tree structure definitiontypedef struct rb_tree{Node*Root//root nodeNode*Nil;//An implementation technique for determining whether a node is empty, etc.}rb_tree; Node*Buynode () {Node*P= NewNode; ASSERT (P!= NULL); Memset (P,0, sizeof (Node));returnP;}//Construct an empty treevoidInittree (Rb_tree&T) {T.Nil=Buynode (); T.Root=T.Nil; T.Nil -Color=BLACK; T.Nil -Key= -1;}//LvoidRotateleft (Rb_tree&T, Node*P) {Node*S=P -Right//s is the right tree of the unbalanced node PP -Right=S -Left//The Left rime of S is connected to the right tree of P if(s -Left!=T.Nil) {//If the left tree of S is not empty, it changes the parent node of its left treeS -Left -Parent =P } s -Parent =P -Parent;//Change the parent node of S if(p -Parent ==T.Nil) {//Description p is the root node, and S is the root node after rotation .T.Root=S }Else if(p=P -Parent -left) {//p before the left tree will take s as the New Left treeP -Parent -Left=S }Else{p -Parent -Right=S//Otherwise S is P's parent node's right tree} s -Left=P//p as the left tree of SP -Parent =S//Change the parent node of P}//Right-handedvoidRotateright (Rb_tree&T, Node*P) {Node*S=P -Left P -Left=S -Rightif(s -Right!=T.Nil) {s -Right -Parent =P } s -Parent =P -Parent;if(p -Parent ==T.Nil) {T.Root=S }Else if(p=P -Parent -left) {p -Parent -Left=S }Else{p -Parent -Right=S } s -Right=P P -Parent =s;}//Adjust the balance of the treevoidInsert_fixup (Rb_tree&T, Node*Z) {Node*Y while(Z -Parent -Color==RED) {//red-red connection unbalanced if(Z -Parent ==Z -Parent -Parent -left) {//Left insertY=Z -Parent -Parent -Right//y for the Uncle node of the insertion node if(Y -Color==RED) {Z -Parent -Color=BLACK; Z -Parent -Parent -Color=RED; Y -Color=BLACK; Z=Z -Parent -Parent; Continue }Else if(Z==Z -Parent -right) {//inner insertion on leftZ=Z -Parent; Rotateleft (t, z);//L} Z -Parent -Color=BLACK; Z -Parent -Parent -Color=RED; Rotateright (T, Z -Parent -Parent);//Right-handed}Else{//Right InsertY=Z -Parent -Parent -Leftif(Y -Color==RED) {Z -Parent -Color=BLACK; Z -Parent -Parent -Color=RED; Y -Color=BLACK; Z=Z -Parent -Parent; Continue }Else if(Z==Z -Parent -left) {//medial insert on rightZ=Z -Parent; Rotateright (t, z); } Z -Parent -Color=BLACK; Z -Parent -Parent -Color=RED; Rotateleft (T, Z -Parent -Parent); }} t.Root -Color=BLACK;} BOOL Insert (Rb_tree&TTypex) {Node*P=T.Nil; Node*S=T.Root//Find the right insertion position while(s!=T.Nil) {P=Sif(x==S -Key) {return false; }Else if(x<S -Key) {s=S -Left }Else{s=S -Right } }//Construction nodeNode*Q=Buynode (); Q -Key=X Q -Parent =P//Insert a node in the appropriate location in the tree if(p==T.Nil) {//Indicates that the tree does not have a node before it is the root node.T.Root=Q }Else if(x<P -Key) {//Insert node in the left subtree of node PP -Left=Q }Else{//Insert node in right subtreeP -Right=Q }//Set the information for the insertion nodeQ -Left=Q -Right=T.Nil; Q -Color=RED;//Adjust the balance of the treeInsert_fixup (t, q);return true;} int main () {int AR[]= {100, 40, 6}; Rb_tree RB; Inittree (RB); int n = sizeof (AR)/sizeof (int); for (int i = 0; i < n; ++i) {Insert (RB, AR[I]); } return 0;}
C + + implements a simple red-black tree (Rb_tree)