473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with RedBlackTree

I cannot get quite well with the "Insert " method in the RedBlackTree,
could anybody give me a complete implementation of it?

Thanks!

Jun 6 '07 #1
1 1375
hi dude:
here's list of C red black tree implementation.
skip the comment if you are not Chinese:)
/**//*-----------------------------------------------------------
RB-Tree
:
1) <<Introduction to algorithm>>
2) <<STL >>
3) sgi-stl stl_tree.h
4) http://epaperpress.com/sortsearch/index.html
5) http://www.ececs.uc.edu/~franco/C321.../redblack.html

(http://www.cppblog.com/converse/)
:
1)
2)
3) ( )
4) ,
5) ,

-------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef int KEY;

enum NODECOLOR
{
BLACK = 0,
RED = 1
};

typedef struct RBTree
{
struct RBTree *parent;
struct RBTree *left, *right;
KEY key;
NODECOLOR color;
}RBTree, *PRBTree;

PRBTree RB_InsertNode(PRBTree root, KEY key);
PRBTree RB_InsertNode_Fixup(PRBTree root, PRBTree z);

PRBTree RB_DeleteNode(PRBTree root, KEY key);
PRBTree RB_DeleteNode_Fixup(PRBTree root, PRBTree z);

PRBTree Find_Node(PRBTree root, KEY key);
void Left_Rotate(PRBTree A, PRBTree& root);
void Right_Rotate(PRBTree A, PRBTree& root);
void Mid_Visit(PRBTree T);
void Mid_DeleteTree(PRBTree T);
void Print_Node(PRBTree node);

/**//*-----------------------------------------------------------
| A B
| / \ == / \
| a B A y
| / \ / \
| b y a b
-----------------------------------------------------------*/
void Left_Rotate(PRBTree A, PRBTree& root)
{
PRBTree B;
B = A->right;

if (NULL == B)
return;

A->right = B->left;
if (NULL != B->left)
B->left->parent = A;
B->parent = A->parent;
// A->parent = NULL
if (A == root)
{
root = B;
}
else if (A == A->parent->left)
{
A->parent->left = B;
}
else
{
A->parent->right = B;
}
B->left = A;
A->parent = B;
}

/**//*-----------------------------------------------------------
| A B
| / \ / \
| B y == a A
| / \ / \
|a b b y
-----------------------------------------------------------*/
void Right_Rotate(PRBTree A, PRBTree& root)
{
PRBTree B;
B = A->left;

if (NULL == B)
return;

A->left = B->right;
if (NULL != B->right)
B->right->parent = A;
B->parent = A->parent;
// A->parent = NULL
if (A == root)
{
root = B;
}
else if (A == A->parent->left)
{
A->parent->left = B;
}
else
{
A->parent->right = B;
}
A->parent = B;
B->right = A;
}

/**//*-----------------------------------------------------------
| : key
| : root, key
| : , NULL
-------------------------------------------------------------*/
PRBTree Find_Node(PRBTree root, KEY key)
{
PRBTree x;

// key node
x = root;
do
{
if (key == x->key)
break;
if (key < x->key)
{
if (NULL != x->left)
x = x->left;
else
break;
}
else
{
if (NULL != x->right)
x = x->right;
else
break;
}
} while (NULL != x);

return x;
}

/**//*-----------------------------------------------------------
| : key
| : root, key
| : root
-------------------------------------------------------------*/
PRBTree RB_InsertNode(PRBTree root, KEY key)
{
PRBTree x, y;

PRBTree z;
if (NULL == (z = (PRBTree)malloc(sizeof(RBTree))))
{
printf("Memory alloc error\n");
return NULL;
}
z->key = key;

// z
x = root, y = NULL;
while (NULL != x)
{
y = x;
if (z->key < x->key)
{
if (NULL != x->left)
{
x = x->left;
}
else
{
break;
}
}
else
{
if (NULL != x->right)
{
x = x->right;
}
else
{
break;
}
}
}

// z
z->parent = y;
if (NULL == y)
{
root = z;
}
else
{
if (z->key < y->key)
y->left = z;
else
y->right = z;
}
// z red red
z->left = z->right = NULL;
z->color = RED;

//
return RB_InsertNode_Fixup(root, z);
}

/**//*-----------------------------------------------------------
| : key
| : root, z
| : root
-------------------------------------------------------------*/
PRBTree RB_InsertNode_Fixup(PRBTree root, PRBTree z)
{
PRBTree y;
while (root != z && RED == z->parent->color) // z
red
{
if (z->parent == z->parent->parent->left) //

{
y = z->parent->parent->right; // y z

if (NULL != y && RED == y->color) //
red
{
z->parent->color = BLACK; // z
B
y->color =
BLACK; // z B
z->parent->parent->color = RED; // z
B
z = z->parent-
>parent; // z
}

else //
b
{
if (z == z->parent->right) //

{
z = z->parent;
Left_Rotate(z, root);
}
z->parent->color = BLACK; //
B
z->parent->parent->color = RED; //
R
Right_Rotate(z->parent->parent, root);
}
}

else //

{
y = z->parent->parent->left; // y z

if (NULL != y && RED == y->color) // y
red
{
z->parent->color = BLACK; //
B
y->color =
BLACK; // B
z->parent->parent->color = RED; //
R
z = z->parent-
>parent; // z
}

else //
y B
{
if (z == z->parent->left) //

{
z = z->parent;
Right_Rotate(z, root);
}
z->parent->color = BLACK; //
B
z->parent->parent->color = RED; //
RED
Left_Rotate(z->parent->parent, root);
}
}
} // while(RED == z->parent->color)

// B
root->color = BLACK;

return root;
}

/**//*-----------------------------------------------------------
| : key
| : root, key
| : root
-------------------------------------------------------------*/
PRBTree RB_DeleteNode(PRBTree root, KEY key)
{
PRBTree x, y, z, x_parent;

z = Find_Node(root, key);
if (NULL == z)
return root;

// z ,y == z
// ,y z
if (NULL == z->left || NULL == z->right)
y = z;
else
{
y = z->right;
while (NULL != y->left)
y = y->left;
}

// x y , NULL
if (NULL != y->left)
x = y->left;
else
x = y->right;

// x y
if (NULL != x)
x->parent = y->parent;
if (NULL == y->parent)
root = x;
else if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;

// y key z , y
if (y != z)
{
z->key = y->key;
}

// y B,
if (BLACK == y->color && NULL != x)
RB_DeleteNode_Fixup(root, x);

free(y);

return root;
}

/**//*-----------------------------------------------------------
| : key
| : root, x
| : root
-------------------------------------------------------------*/
PRBTree RB_DeleteNode_Fixup(PRBTree root, PRBTree x)
{
PRBTree w;

while (x != root && BLACK == x->color)
{
if (x == x->parent-
>left) //
x
{
w = x->parent-
>right; //
w x

if (NULL == w)
continue;

if (RED == w-
>color) //
w
{
w->color = BLACK;
x->parent->color = RED;
Left_Rotate(x->parent, root);
w = x->parent->right;
}
if (NULL != w->left && BLACK == w->left->color &&
NULL != w->right && BLACK == w->right->color)
{
w->color = RED;
x = x->parent;
}
else
{
if (NULL != w->right && BLACK == w->right->color)
{
w->left->color = BLACK;
w->color = RED;
Right_Rotate(w, root);
w = x->parent->right;
}

w->color = x->parent->color;
x->parent->color = BLACK;
w->right->color = BLACK;
Left_Rotate(x->parent, root);
x = root;
}
}
else
{
w = x->parent->left;
if (NULL == w)
continue;
if (RED == w->color)
{
w->color = BLACK;
x->parent->color = RED;
Left_Rotate(x->parent, root);
w = x->parent->left;
}
if (NULL != w->left && BLACK == w->left->color &&
NULL != w->right && BLACK == w->right->color)
{
w->color = RED;
x = x->parent;
}
else
{
if (NULL != w->left && BLACK == w->left->color)
{
w->right->color = BLACK;
w->color = RED;
Left_Rotate(w, root);
w = x->parent->left;
}

w->color = x->parent->color;
x->parent->color = BLACK;
w->left->color = BLACK;
Right_Rotate(x->parent, root);
x = root;
}
}
}

x->color = BLACK;

return root;
}

void Print_Node(PRBTree node)
{
char* color[] = {"BLACK", "RED"};
printf("Key = %d,\tcolor = %s", node->key, color[node->color]);
if (NULL != node->parent)
printf(",\tparent = %d", node->parent->key);
if (NULL != node->left)
printf(",\tleft = %d", node->left->key);
if (NULL != node->right)
printf(",\tright = %d", node->right->key);
printf("\n");
}

//
void Mid_Visit(PRBTree T)
{
if (NULL != T)
{
if (NULL != T->left)
Mid_Visit(T->left);
Print_Node(T);
if (NULL != T->right)
Mid_Visit(T->right);
}
}

//
void Mid_DeleteTree(PRBTree T)
{
if (NULL != T)
{
if (NULL != T->left)
Mid_DeleteTree(T->left);
PRBTree temp = T->right;
free(T);
T = NULL;
if (NULL != temp)
Mid_DeleteTree(temp);
}
}

void Create_New_Array(int array[], int length)
{
for (int i = 0; i < length; i++)
{
array[i] = rand() % 256;
}
}

int main(int argc, char *argv[])
{
//int array[10] = {80, 116, 81, 205, 82, 68, 151, 20, 109, 100};
int array[10];
srand(time(NULL));
Create_New_Array(array, 10);
PRBTree root = NULL;
int i;
for (i = 0; i < 10; i++)
{
root = RB_InsertNode(root, array[i]);
}

Mid_Visit(root);

//
int index = rand() % 10;
printf("delete node %d\n", array[index]);
root = RB_DeleteNode(root, array[index]);
Mid_Visit(root);

//
Mid_DeleteTree(root);

return 0;
}
On Jun 6, 11:34 am, Evil_man <Evilcheng...@gmail.comwrote:
I cannot get quite well with the "Insert " method in the RedBlackTree,
could anybody give me a complete implementation of it?

Thanks!

Jun 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.