[數據結構]紅黑樹的實現源碼
于2007.11.28:這份代碼是有問題的,修正的在這里:
http://m.shnenglu.com/converse/archive/2007/11/28/37430.html
半年之前寫的一個紅黑樹的實現算法了,當時有點忙沒有寫相應的文檔,一下子幾乎全都忘記了,作一個記錄,改天有空了來補充說明文檔.

/**//*-----------------------------------------------------------
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/html/RedBlack/redblack.html

作者:李創 (http://m.shnenglu.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;
}
http://m.shnenglu.com/converse/archive/2007/11/28/37430.html
半年之前寫的一個紅黑樹的實現算法了,當時有點忙沒有寫相應的文檔,一下子幾乎全都忘記了,作一個記錄,改天有空了來補充說明文檔.

/**//*-----------------------------------------------------------
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/html/RedBlack/redblack.html
作者:李創 (http://m.shnenglu.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;
}
posted on 2006-10-07 14:32 那誰 閱讀(5681) 評論(12) 編輯 收藏 引用 所屬分類: 算法與數據結構

