RedBlackTree.h
------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
/*
Each child of the Red Black Tree
*/
class Node
{
public:
char *data; /*data*/
char color; /*color*/
Node *left, *right, *parent; /*lelf, right, parent*/
/*copy constructor*/
Node(char *data)
{
this->data = new char[strlen(data) + 1];
strcpy(this->data, data);
color = NULL;
}
/*constructor*/
Node()
{
this->data = new char[1];
color = NULL;
}
/*destructor*/
~Node()
{
delete (this->data);
color = NULL;
}
};
/*
This is a class of Red Black Tre
*/
class RedBlackTree
{
private:
Node *Root; /*root*/
Node *Nil; /*leaf*/
public:
/*constructor*/
RedBlackTree ();
/*destructor*/
~RedBlackTree ();
Node* tree_successor ( Node* x );
/*left rotate*/
int leftRotate ( Node *x );
/*right rotate*/
int rightRotate ( Node *x );
/*insert key*/
int insert_key ( char *data );
/*fix up*/
int fixup ( Node *z );
/*delete key*/
int delete_key ( Node *z );
/*delete fixup*/
int delete_fixup ( Node *x );
/*in order traverse*/
void inOrderTraverse ( Node *n );
/*post order delete*/
void postOrderDelete ( Node *n );
};
RedBlackTree.cpp
------------------------------------------------------------------------------------------
#include "RedBlackTree.h"
/**
* tree successor
* @param: x is Node type data
* @return: return y is a another Node type pointer
*/
Node* RedBlackTree::tree_successor( Node* x )
{
if ( x->right != Nil )
{
x = x->right;
while ( x->left != Nil )
{
x = x->left;
}
return x;
}
Node* y = x->parent;
while ( y != Nil && x == y->right )
{
x = y;
y = y->parent;
}
return y;
}
/**
* left rotate
* @param: x is a node type data
* @return: 0 by default
*/
int RedBlackTree::leftRotate( Node *x )
{
Node *y;
y = x->right;
x->right = y->left;
y->left->parent = x;
y->parent = x->parent;
if ( x->parent == Nil )
Root = y;
else if ( x == x->parent->left )
x->parent->left = y;
else
x->parent->right = y;
y->left = x;
x->parent = y;
return 0;
}
/**
* right rotate
* @param: x is a Node type pointer
* @return: 0 by default
*/
int RedBlackTree::rightRotate( Node *x )
{
Node *y;
y = x->left;
x->left = y->right;
y->right->parent = x;
y->parent = x->parent;
if ( x->parent == Nil )
Root = y;
else if ( x == x->parent->right )
x->parent->right = y;
else
x->parent->left = y;
y->right = x;
x->parent = y;
return 0;
}
/**
* insert key
* @param: data is a char type pointer
* @return: 0 by default
* @see: fixup()
*/
int RedBlackTree::insert_key( char *data )
{
Node *x, *y, *z;
y = Nil;
x = Root;
while ( x != Nil )
{
y = x;
if ( strcmp( data, x->data ) < 0 )
x = x->left;
else if ( strcmp( data, x->data ) > 0 )
x = x->right;
else
return -1;
}
z = new Node(data);
z->parent = y;
z->color = 'R';
z->left = z->right = Nil;
if ( y == Nil )
Root = z;
else
{
if ( strcmp( data, y->data ) <= 0 )
y->left = z;
else
y->right = z;
}
fixup( z );
x = y = NULL;
return 0;
}
/**
* fixup the node
* @param: z is a Node type pointer
* @return: 0 by default
* @see: leftRotate(Node*)
* @see: rightRotate(Node*)
*/
int RedBlackTree::fixup( Node *z )
{
Node *y;
while ( z->parent->color == 'R' )
{
if ( z->parent == z->parent->parent->left )
{
y = z->parent->parent->right;
if ( y->color == 'R' )
{
z->parent->color = 'B';
y->color = 'B';
z->parent->parent->color = 'R';
z = z->parent->parent;
}
else
{
if ( z == z->parent->right )
{
z = z->parent;
leftRotate( z );
}
z->parent->color = 'B';
z->parent->parent->color = 'R';
rightRotate( z->parent->parent );
}
}
else
{
y = z->parent->parent->left;
if ( y->color == 'R' )
{
z->parent->color = 'B';
y->color = 'B';
z->parent->parent->color = 'R';
z = z->parent->parent;
}
else
{
if ( z == z->parent->left )
{
z = z->parent;
rightRotate( z );
}
z->parent->color = 'B';
z->parent->parent->color = 'R';
leftRotate( z->parent->parent );
}
}
}
Root->color = 'B';
y = NULL;
return 0;
}
/**
* delete key
* @param: z is a Node type param
* @see: tree_successor(Node*)
* @see: delete_fixup(Node*)
* @return:0 by default
*/
int RedBlackTree::delete_key( Node *z )
{
Node *x, *y;
if ( z->left == Nil || z->right == Nil )
{
y = z;
}
else
{
y = this->tree_successor( z );
}
if ( y->left != Nil )
{
x = y->left;
}
else
{
x = y->right;
}
x->parent = y->parent;
if ( y->parent == Nil )
{
Root = x;
}
else
{
if ( y == y->parent->left )
{
y->parent->left = x;
}
else
{
y->parent->right = x;
}
}
if ( y != z )
{
strcpy( y->data, z->data );
//z->data = y->data;
}
if ( y->color == 'B' )
{
delete_fixup( x );
}
return 0;
}
/**
* delete fixup
* @param: x is a Node type param
* @return: 0 by default
* @see: leftRotate(Node*)
* @see: rightRotate(Node*)
*/
int RedBlackTree::delete_fixup( Node *x )
{
Node *w;
while ( x != Root && x->color == 'B' )
{
if ( x == x->parent->left )
{
w = x->parent->right;
if ( w->color == 'R' )
{
w->color = 'B';
x->parent->color = 'R';
leftRotate( x->parent );
w = x->parent->right;
}
if ( w->left->color == 'B' && w->right->color == 'B' )
{
w->color = 'R';
x = x->parent;
}
else if ( w->right->color == 'B' )
{
w->left->color = 'B';
w->color = 'R';
rightRotate( w );
w = x->parent->right;
}
w->color = x->parent->color;
x->parent->color = 'B';
w->right->color = 'B';
leftRotate( x->parent );
x = Root;
}
else
{
w = x->parent->left;
if ( w->color == 'R' )
{
w->color = 'B';
x->parent->color = 'R';
rightRotate( x->parent );
w = x->parent->left;
}
if ( w->right->color == 'B' && w->left->color == 'B' )
{
w->color = 'R';
x = x->parent;
}
else if ( w->left->color == 'B' )
{
w->right->color = 'B';
w->color = 'R';
leftRotate( w );
w = x->parent->left;
}
w->color = x->parent->color;
x->parent->color = 'B';
w->left->color = 'B';
rightRotate( x->parent );
x = Root;
}
}
x->color = 'B';
return 0;
}
/**
* in order traverse
* @param: n is a Node type param
* @desc: recursive calling
*/
void RedBlackTree::inOrderTraverse( Node *n )
{
if ( n != Nil )
{
inOrderTraverse( n->left );
puts( n->data );
inOrderTraverse( n->right );
}
}
/**
* post order delete
* @param: n is a Node type
* @desc: recursive calling
*/
void RedBlackTree::postOrderDelete( Node *n )
{
if ( n != Nil )
{
postOrderDelete( n->left );
postOrderDelete( n->right );
delete( n );
}
}
/**
* Constructor
* Nil equal Node
* Root equal Nil
*/
RedBlackTree::RedBlackTree()
{
this->Nil = new Node();
this->Root = Nil;
}
/**
* destructor
* @see: postOrderDelete(Node*)
*/
RedBlackTree::~RedBlackTree()
{
postOrderDelete( this->Root );
delete (this->Nil);
}
------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
/*
Each child of the Red Black Tree
*/
class Node
{
public:
char *data; /*data*/
char color; /*color*/
Node *left, *right, *parent; /*lelf, right, parent*/
/*copy constructor*/
Node(char *data)
{
this->data = new char[strlen(data) + 1];
strcpy(this->data, data);
color = NULL;
}
/*constructor*/
Node()
{
this->data = new char[1];
color = NULL;
}
/*destructor*/
~Node()
{
delete (this->data);
color = NULL;
}
};
/*
This is a class of Red Black Tre
*/
class RedBlackTree
{
private:
Node *Root; /*root*/
Node *Nil; /*leaf*/
public:
/*constructor*/
RedBlackTree ();
/*destructor*/
~RedBlackTree ();
Node* tree_successor ( Node* x );
/*left rotate*/
int leftRotate ( Node *x );
/*right rotate*/
int rightRotate ( Node *x );
/*insert key*/
int insert_key ( char *data );
/*fix up*/
int fixup ( Node *z );
/*delete key*/
int delete_key ( Node *z );
/*delete fixup*/
int delete_fixup ( Node *x );
/*in order traverse*/
void inOrderTraverse ( Node *n );
/*post order delete*/
void postOrderDelete ( Node *n );
};
RedBlackTree.cpp
------------------------------------------------------------------------------------------
#include "RedBlackTree.h"
/**
* tree successor
* @param: x is Node type data
* @return: return y is a another Node type pointer
*/
Node* RedBlackTree::tree_successor( Node* x )
{
if ( x->right != Nil )
{
x = x->right;
while ( x->left != Nil )
{
x = x->left;
}
return x;
}
Node* y = x->parent;
while ( y != Nil && x == y->right )
{
x = y;
y = y->parent;
}
return y;
}
/**
* left rotate
* @param: x is a node type data
* @return: 0 by default
*/
int RedBlackTree::leftRotate( Node *x )
{
Node *y;
y = x->right;
x->right = y->left;
y->left->parent = x;
y->parent = x->parent;
if ( x->parent == Nil )
Root = y;
else if ( x == x->parent->left )
x->parent->left = y;
else
x->parent->right = y;
y->left = x;
x->parent = y;
return 0;
}
/**
* right rotate
* @param: x is a Node type pointer
* @return: 0 by default
*/
int RedBlackTree::rightRotate( Node *x )
{
Node *y;
y = x->left;
x->left = y->right;
y->right->parent = x;
y->parent = x->parent;
if ( x->parent == Nil )
Root = y;
else if ( x == x->parent->right )
x->parent->right = y;
else
x->parent->left = y;
y->right = x;
x->parent = y;
return 0;
}
/**
* insert key
* @param: data is a char type pointer
* @return: 0 by default
* @see: fixup()
*/
int RedBlackTree::insert_key( char *data )
{
Node *x, *y, *z;
y = Nil;
x = Root;
while ( x != Nil )
{
y = x;
if ( strcmp( data, x->data ) < 0 )
x = x->left;
else if ( strcmp( data, x->data ) > 0 )
x = x->right;
else
return -1;
}
z = new Node(data);
z->parent = y;
z->color = 'R';
z->left = z->right = Nil;
if ( y == Nil )
Root = z;
else
{
if ( strcmp( data, y->data ) <= 0 )
y->left = z;
else
y->right = z;
}
fixup( z );
x = y = NULL;
return 0;
}
/**
* fixup the node
* @param: z is a Node type pointer
* @return: 0 by default
* @see: leftRotate(Node*)
* @see: rightRotate(Node*)
*/
int RedBlackTree::fixup( Node *z )
{
Node *y;
while ( z->parent->color == 'R' )
{
if ( z->parent == z->parent->parent->left )
{
y = z->parent->parent->right;
if ( y->color == 'R' )
{
z->parent->color = 'B';
y->color = 'B';
z->parent->parent->color = 'R';
z = z->parent->parent;
}
else
{
if ( z == z->parent->right )
{
z = z->parent;
leftRotate( z );
}
z->parent->color = 'B';
z->parent->parent->color = 'R';
rightRotate( z->parent->parent );
}
}
else
{
y = z->parent->parent->left;
if ( y->color == 'R' )
{
z->parent->color = 'B';
y->color = 'B';
z->parent->parent->color = 'R';
z = z->parent->parent;
}
else
{
if ( z == z->parent->left )
{
z = z->parent;
rightRotate( z );
}
z->parent->color = 'B';
z->parent->parent->color = 'R';
leftRotate( z->parent->parent );
}
}
}
Root->color = 'B';
y = NULL;
return 0;
}
/**
* delete key
* @param: z is a Node type param
* @see: tree_successor(Node*)
* @see: delete_fixup(Node*)
* @return:0 by default
*/
int RedBlackTree::delete_key( Node *z )
{
Node *x, *y;
if ( z->left == Nil || z->right == Nil )
{
y = z;
}
else
{
y = this->tree_successor( z );
}
if ( y->left != Nil )
{
x = y->left;
}
else
{
x = y->right;
}
x->parent = y->parent;
if ( y->parent == Nil )
{
Root = x;
}
else
{
if ( y == y->parent->left )
{
y->parent->left = x;
}
else
{
y->parent->right = x;
}
}
if ( y != z )
{
strcpy( y->data, z->data );
//z->data = y->data;
}
if ( y->color == 'B' )
{
delete_fixup( x );
}
return 0;
}
/**
* delete fixup
* @param: x is a Node type param
* @return: 0 by default
* @see: leftRotate(Node*)
* @see: rightRotate(Node*)
*/
int RedBlackTree::delete_fixup( Node *x )
{
Node *w;
while ( x != Root && x->color == 'B' )
{
if ( x == x->parent->left )
{
w = x->parent->right;
if ( w->color == 'R' )
{
w->color = 'B';
x->parent->color = 'R';
leftRotate( x->parent );
w = x->parent->right;
}
if ( w->left->color == 'B' && w->right->color == 'B' )
{
w->color = 'R';
x = x->parent;
}
else if ( w->right->color == 'B' )
{
w->left->color = 'B';
w->color = 'R';
rightRotate( w );
w = x->parent->right;
}
w->color = x->parent->color;
x->parent->color = 'B';
w->right->color = 'B';
leftRotate( x->parent );
x = Root;
}
else
{
w = x->parent->left;
if ( w->color == 'R' )
{
w->color = 'B';
x->parent->color = 'R';
rightRotate( x->parent );
w = x->parent->left;
}
if ( w->right->color == 'B' && w->left->color == 'B' )
{
w->color = 'R';
x = x->parent;
}
else if ( w->left->color == 'B' )
{
w->right->color = 'B';
w->color = 'R';
leftRotate( w );
w = x->parent->left;
}
w->color = x->parent->color;
x->parent->color = 'B';
w->left->color = 'B';
rightRotate( x->parent );
x = Root;
}
}
x->color = 'B';
return 0;
}
/**
* in order traverse
* @param: n is a Node type param
* @desc: recursive calling
*/
void RedBlackTree::inOrderTraverse( Node *n )
{
if ( n != Nil )
{
inOrderTraverse( n->left );
puts( n->data );
inOrderTraverse( n->right );
}
}
/**
* post order delete
* @param: n is a Node type
* @desc: recursive calling
*/
void RedBlackTree::postOrderDelete( Node *n )
{
if ( n != Nil )
{
postOrderDelete( n->left );
postOrderDelete( n->right );
delete( n );
}
}
/**
* Constructor
* Nil equal Node
* Root equal Nil
*/
RedBlackTree::RedBlackTree()
{
this->Nil = new Node();
this->Root = Nil;
}
/**
* destructor
* @see: postOrderDelete(Node*)
*/
RedBlackTree::~RedBlackTree()
{
postOrderDelete( this->Root );
delete (this->Nil);
}