那谁的技术博客

感兴趣领域:高性能服务器编程,存储,算法,Linux内核
随笔 - 210, 文章 - 0, 评论 - 1183, 引用 - 0
数据加载中……

[数据结构]红黑树的实现源码

于2007.11.28:这份代码是有问题的,修正的在这里:
http://www.cppblog.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://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;
}

posted on 2006-10-07 14:32 那谁 阅读(5627) 评论(12)  编辑 收藏 引用 所属分类: 算法与数据结构

评论

# re: [数据结构]红黑树的实现源码  回复  更多评论   

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为它的祖父节点
}
================================
上面代码中z->parent->parent->left好像会越界!
请问楼主验证过自己的实现是完全正确的吗
2006-10-15 23:25 | BadNicky

# re: [数据结构]红黑树的实现源码  回复  更多评论   

你是对的
2006-10-18 08:32 | BadNicky

# re: [数据结构]红黑树的实现源码  回复  更多评论   

错了还不改?

我试了一下,若按算法导论书中,设置叶结点为NIL[T],
NIL->color=BLACK;
NIL->key=0;
// 以下NIL一定要指向本身
NIL->left=NIL;
NIL->parent=NIL;
NIL->right=NIL;
那么在控制指针越界访问方面会有很大的优势!
2006-10-31 22:47 | pengkuny

# re: [数据结构]红黑树的实现源码  回复  更多评论   

错拉错拉BadNicky和我都错拉
while (z->parent->color == RED)
{
if(z->parent == z->parent->parent->left)
/*既然z的父亲p[z]是red,那么p[z]一定不是根结点, p[p[z]]也就一定存在,不会越界.如果你不愿意使用我上面提到的使用NIL,那么你只需要稍微为:*/
while (NULL != z->parent && z->parent->color == RED)
那么下面一系列操作都不会越界.
2006-10-31 23:18 | pengkuny

# re: [数据结构]红黑树的实现源码  回复  更多评论   

编译时出的警告
PRBTree RB_DeleteNode(PRBTree root, KEY key)
{
PRBTree x, y, z, x_parent;
中 x_parent 没有定义 怎么解决阿
2007-04-15 20:49 | miami

# re: [数据结构]红黑树的实现源码  回复  更多评论   

这个要赞,最好理解的红黑树代码,注释很清晰
2007-06-06 10:57 | j

# re: [数据结构]红黑树的实现源码  回复  更多评论   

代码有Bug.
你依次插入以下数据:
int ss[20] = {89, 56 ,457 , 556, 445,
456, 125, 4571, 741, 7456,
545, 5662, 475, 1234, 455,
778, 662, 159, 753, 4565};

删除 556,4565 后,剩余的18个节点所构成的RBTree中,再删除457时出错。删除完后 x == NULL ,因此 RB_DeleteNode() 不会调用 RB_DeleteNode_Fixup(),从而返回。而返回后的树,将会有一路的黑高度为2,其余所有路径上黑高度为3。不再满足RBTree条件。
2007-08-06 11:54 | swxlion

# re: [数据结构]红黑树的实现源码[未登录]  回复  更多评论   

@swxlion

我测试了一下你说的情况,应该还是平衡的,所有根节点到空结点上的黑颜色结点数目都是3,你可以在调用那三次删除操作之后调用Mid_Visit函数看看.

另外,我原来对红黑树一个性质的理解是错误的:
3) 每个叶子结点(空结点被认为是叶子结点)是黑色的

在算法导论中对这条性质的定义是:
Every leaf (NIL) is black.

结果我就错以为是上面描述的那种性质,事实上,在算法导论中,一个空结点也就是上文中的"NIL"才是叶子节点.
所以对这个性质的定义应该是:
3)空节点是黑色的(红黑树中,根节点的parent以及所有叶节点lchild、rchild都不指向NULL,而是指向一个定义好的空节点)。

详见:
http://lucizm.blog.sohu.com/69218113.html

另外,这里补充几个有用的链接,包括了红黑树和AVL树的一些比较,分别是:
http://blog.csdn.net/naivebaby/archive/2006/11/04/1366579.aspx
http://blog.chinaunix.net/u1/35281/showart_279925.html

2007-11-04 16:15 | 创系

# re: [数据结构]红黑树的实现源码  回复  更多评论   

你的程序有bug.删除时x==NULL时会出错。正如swxlion所言
2007-11-28 13:21 | LL

# re: [数据结构]红黑树的实现源码  回复  更多评论   

这份代码确实是有问题的,修正的放在这里:
http://www.cppblog.com/converse/archive/2007/11/28/37430.html
2007-11-28 14:30 | 创系

# re: [数据结构]红黑树的实现源码  回复  更多评论   

http://arreter-masturber-et-castration-chimique.gem-elb.info
http://video-sexe-hard.vagine-elb.info
http://zoophile-gay-sexe-video.lexington-elb.info
http://gratuit-sex-travesti.gem-elb.info
http://porno-xxx-sexe-cul.lexington-elb.info
http://rencontre-sexe-live.gem-elb.info
http://donna-porcella.asiannudes-tun.info
http://gay-sex-toys.asiannudes-tun.info
http://photo-de-femme-bite.vagine-elb.info
http://local-travesti-madrid.es-free-full-porn-video-clip.denrico-elb.info
http://salon-du-sexe-en-belgique.lexington-elb.info
http://chichi-xxx-it.teresa-tun.info
http://wwwvideo-porno-cazzoit.asiannudes-tun.info
http://salope-brune-bas.gem-elb.info
http://bionda-sborrata.teresa-tun.info
http://sexe-lolita-nymphette.lexington-elb.info
http://cazzone-enorme.asiannudes-tun.info
http://enculer-tres-soumise.gem-elb.info
http://annuaire-amaeur-sexe-amatrice-chaude.gem-elb.info
http://baiser-avec-cam.denrico-elb.info
http://annonces-salope-telephone-pas-calais.gem-elb.info
http://etudiante-sexe-sein.vagine-elb.info
http://video-gratuit-chatte-noire.gem-elb.info
http://pedo-sexe.gem-elb.info
http://vip-video-porno-gratis.teresa-tun.info
2007-12-23 08:07 | dvsvsvdcdsdd

# re: [数据结构]红黑树的实现源码  回复  更多评论   

嘿嘿 还是用STL中的RB-TREE放心!
2008-03-29 21:10 | BaluWu

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理