"The nature of the red-black tree"
The red-black tree is a special two-fork search tree, with all the null pointers of the red and black trees replaced with the external node Nullnode (nullnode->rchild=nullnode->rchild=nullnode). The red-black tree has the following properties:
- The root node is black
- All nodes are either black or red.
- The external nodes are all black.
- The child node of the red node must be black.
- All paths from the root node to the external node contain the same number of black nodes.
"Code Implementation"
//Redblacktree.h code#ifndef Redblacktree_h#define REDBLACKTREE_HTemplate<classT>classRedblacktree;Template<classT>classnode{friend classredblacktree<t>;//private: Public: T data; Node<t> *rchild; Node<t> *lchild;intcolor;};Template<classT>classredblacktree{ Public: Redblacktree (ConstT Neginf);//using constructors to initialize pseudo-roots~redblacktree ();//Use destructors to free up space voidReclaimmemory (node<t> *p);enum{black,red};voidInsert (T item);//insert Operation voidHandlereorient (T item);//Adjustment OperationNode<t> *rotate (T item,node<t> *parent);//Rotate voidRotationwithleft (node<t> *&k2);voidRotationwithright (node<t> *&k1);voidPreorder ();//Pre-order traversal voidPreorder (node<t> *currentnode);voidFindmin ();//Find minimum value voidFindmax ();//Find maximum valuePrivate: Node<t> *header;//pseudo-rootNode<t> *nullnode;//NULL nodeNode<t> *current; Node<t> *parent;//Parent nodeNode<t> *grand;//Grandfather nodeNode<t> *great;//Grandfather node};Template<classT>redblacktree<t>::redblacktree (ConstT neginf) {nullnode=NewNode<t> (); nullnode->rchild=nullnode; nullnode->lchild=nullnode; Header=NewNode<t> (); header->data=neginf; header->color=black; header->rchild=nullnode; Header->lchild=nullnode;}Template<classT>redblacktree<t>::~redblacktree () {reclaimmemory (header->rchild);DeleteHeaderDeleteNullnode;}Template<classT>voidRedblacktree<t>::insert (T item) {Current=parent=grand=header; nullnode->data=item;//----insert data into the two-fork find tree----- while(Current->data!=item) {Great=grand; Grand=parent; Parent=current;//parent to insert position if(Item<current->data) {current=current->lchild; }Else{current=current->rchild; }if(current->lchild->color==red&¤t->rchild->color==red) {handlereorient (item);//Handle nodes with two red children} }if(Current!=nullnode)Throw "Cannot insert duplicate data"; Current=NewNode<t> (); current->data=item; current->lchild=nullnode; current->rchild=nullnode;if(Item<parent->data) parent->lchild=current;Elseparent->rchild=current; Handlereorient (item);//After inserting the node, processing the Father node is also a red case}//------------single rotation (case 1)------------Template<classT>voidRedblacktree<t>::rotationwithleft (node<t> *&k2) {node<t> *k1=k2->lchild; k2->lchild=k1->rchild; k1->rchild=k2; K2=K1;}//------------single rotation (case 2)------------Template<classT>voidRedblacktree<t>::rotationwithright (node<t> *&k1) {node<t> *k2=k1->rchild; k1->rchild=k2->lchild; k2->lchild=k1; K1=K2;}//--------------The judging of the rotation condition---------------Template<classT>node<t> *redblacktree<t>::rotate (T item,node<t> *theparent) {if(Item<theparent->data) {Item<theparent->lchild->data? Rotationwithleft (Theparent->lchild): Rotationwithright (Theparent->lchild);returntheparent->lchild; }Else{Item<theparent->rchild->data? Rotationwithleft (Theparent->rchild): Rotationwithright (Theparent->rchild);returntheparent->rchild; }}//-----------Adjust the operation--------------Template<classT>voidRedblacktree<t>::handlereorient (T Item) {//Discolorationcurrent->color=red; current->lchild->color=black; current->rchild->color=black;//Rotate if(parent->color==red)//Two consecutive red nodes appear (both the Father node and the insertion node are red){grand->color=red;if(Item<grand->data==item<parent->data)//This case is a single rotation{ }Else //This is the case of double rotation (two rotation operation){parent=rotate (Item,grand); } current=rotate (Item,great); current->color=black; } header->rchild->color=black;//The root node must be black.}//--------Recursive implementation of pre-order traversal---------Template<classT>voidRedblacktree<t>::p reorder () {cout<<"Pre-order traversal is:"; Preorder (Header->rchild);cout<<endl;}Template<classT>voidRedblacktree<t>::p Reorder (node<t> *currentnode) {if(Currentnode!=nullnode) {cout<<currentnode->data<<" "; Preorder (Currentnode->lchild); Preorder (Currentnode->rchild); }}//---------------free space----------Template<classT>voidRedblacktree<t>::reclaimmemory (node<t> *p) {if(P!=nullnode) {reclaimmemory (p->lchild); Reclaimmemory (P->rchild);DeleteP } }//-----------Find the minimum value---------Template<classT>voidRedblacktree<t>::findmin () {node<t> *p=header->rchild;if(P==nullnode) {cout<<"This is an empty tree."<<endl; }Else{ while(P->lchild!=nullnode) {p=p->lchild; }cout<<"Minimum value is:"<<p->data<<endl; }}//-----------Find the maximum value---------Template<classT>voidRedblacktree<t>::findmax () {node<t> *p=header->rchild;if(P==nullnode) {cout<<"This is an empty tree."<<endl; }Else{ while(P->rchild!=nullnode) {p=p->rchild; }cout<<"Maximum value is:"<<p->data<<endl; }}#endif
Main. cppCode#include "redblacktree.h"#include <iostream>Using namespaceSTD;int main () {const int a=-188999;//pseudo-root dataRedblacktree<int> RB1 (a);RB1. Insert( -);RB1. Insert(Ten);RB1. Insert( the);RB1. Insert( -);RB1. Insert( -);RB1. Insert( -);RB1. Insert( $);RB1. Insert( +);RB1. Preorder();RB1. Findmin();RB1. Findmax();cout<<"ok!"<<endl;System"Pause");Return0;}
Results
After inserting the data, the red and black trees are as follows:
Program Run Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Red and black Trees