Red and black trees follow the rules: one (Root) a foot (leaf node) black (black), black and (from any node to the null node of the number of black nodes equal) red (red node can not connect)
The following is a simple implementation of the insert and rotate functions of the red-black tree:
#include <iostream> #include <assert.h> #include <string.h> using namespace std;
typedef int TYPE; typedef enum{red=0, Black}color; The color of the node is typedef struct node{color color; Node color Type key; The value of nodes struct node *left, *right, *parent; Left, right and parent node pointer}*pnode; Node pointer//tree structure definition typedef struct rb_tree{node *root; root node node *nil;
An implementation technique for determining whether a node is empty, etc.}rb_tree;
Node *buynode () {Node *p = new node;
ASSERT (P!= NULL);
memset (p, 0, sizeof (Node));
return p;
}//Construct an empty tree void Inittree (Rb_tree &t) {t.nil = Buynode ();
T.root = T.nil;
T.nil->color = black;
T.nil->key =-1; }//L-void Rotateleft (Rb_tree &t, node *p) {Node *s = p->right; S is the right tree p->right of unbalanced node P = s->left;
The left hung of S is connected to the right tree if (s->left!= t.nil) {//If s's left tree is not empty, the parent node of its left tree is changed s->left->parent = p; } s->parent = p->parent; Change the parent node of s if (p->parent = = T.nil) {//description is P as root node, after rotation s is rootNode T.root = s;
}else if (p = p->parent->left) {//p is positioned before the left tree to take s as the New Left tree P->parent->left = s; }else{p->parent->right = s; Otherwise, S is the right tree of the parent node of P s->left = p; P as the left tree p->parent = s;
Change P's parent node//Right Rotateright (Rb_tree &t, node *p) {Node *s = p->left;
P->left = s->right;
if (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 tree void Insert_fixup (Rb_tree &t, node *z) {node *y;
while (Z->parent->color = = red) {//Hung-hung unbalanced if (z->parent = = Z->parent->parent->left) {//left insert y = z->parent->parent->right;
Y is the Uncle node of the inserted 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) {//left side Insert Z = z->parent; Rotateleft (t, z);
L} z->parent->color = black;
Z->parent->parent->color = RED; Rotateright (t, z->parent->parent);
Right}else{//Right Insert y = z->parent->parent->left;
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->left) {//right side insert Z = 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 &t, Type x) {Node *p = T.nil;
Node *s = t.root;
Find the appropriate insertion position while (s!= t.nil) {p = s;
if (x = = S->key) {return false;
}else if (x < S->key) {s = s->left;
}else{s = s->right;
}//Constructs nodes node *q = Buynode ();
Q->key = x;
Q->parent = p;
Inserting a node into the appropriate place in the tree if (p = = T.nil) {//indicates that the tree has no nodes before, then this node is its root t.root = q;
}else if (x < P->key) {//Insert node in node P's left subtree p->left = q;
}else{//Insert node in right subtree p->right = q;
//Set Insert node information q->left = Q->right = T.nil;
Q->color = RED;
Adjust the balance of the tree insert_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; }