红黑树的C语言实现

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010842019/article/details/79230424

https://tool.lu/coderunner/ 【在线C语言编译】

// 红黑树的C语言实现
#include <stdio.h>
#include <stdlib.h>
const int RED = 0;
const int BLACK = 1;

//---编译条件严格,自己引用自己 需要额外定义一个数据类型 使用typedef关键字
/*  https://tool.lu/coderunner/
该部分有些编译器会报错,如https://tool.lu/coderunner/ 不识别rb_node , 
    自己引用自己导致 所以需要特殊处理 
   1.声明一个结构体rb_node_p 
   2.指定该结构体类型为rb_node,
   3.定义rb_node_p结构体
   4.在rb_node_p结构体中使用类型  rb_node

struct rb_node_p;  //1
typedef struct rb_node_p rb_node;  //2

struct rb_node_p{   //3
    rb_node* lchild, *rchild, *parent;  //4
    int key, colour;
};

//------------------------
//http://www.runoob.com/try/runcode.php?filename=helloworld&type=c
struct rb_node{
    rb_node* lchild, *rchild, *parent;
    int key, colour;
};*/
//------------------------

struct rb_node_p;  //1
typedef struct rb_node_p rb_node;  //2

struct rb_node_p{   //3
    rb_node* lchild, *rchild, *parent;  //4
    int key, colour;
};



rb_node* root;

rb_node* get_node( rb_node* parent, int key);
void rb_insert(int key);
rb_node* rb_search(int key);
void rb_delete(int key);
rb_node* clock_wise( rb_node* node);
rb_node* counter_clock_wise( rb_node* node);
void show_rb_tree( rb_node* node);

rb_node* get_node( rb_node* parent, int key){
    rb_node *tmp = (rb_node*)malloc(sizeof(rb_node));
    tmp->key = key;
    tmp->colour = RED;
    tmp->parent = parent;
    tmp->lchild = tmp->rchild = NULL;
    return tmp;
}

rb_node* clock_wise(rb_node* node){
    if(node == NULL || node->lchild == NULL)
        return NULL;

    rb_node *rb_1=node, *rb_2=node->lchild, *rb_3=node->lchild->rchild;
    if(rb_1->parent != NULL){
        if(rb_1->parent->lchild == rb_1)
            rb_1->parent->lchild = rb_2;
        else
            rb_1->parent->rchild = rb_2;
    }else if(rb_1 == root){
        root = rb_2;
    }
    rb_2->parent = rb_1->parent;

    rb_1->parent = rb_2;
    rb_2->rchild = rb_1;

    rb_1->lchild = rb_3;
    if(rb_3 != NULL)rb_3->parent = rb_1;

    return rb_2;    
}

rb_node* counter_clock_wise(rb_node* node){
    if(node == NULL || node->rchild == NULL)
        return NULL;

    rb_node *rb_1=node, *rb_2=node->rchild, *rb_3=node->rchild->lchild;
    if(rb_1->parent != NULL){
        if(rb_1->parent->lchild == rb_1)
            rb_1->parent->lchild = rb_2;
        else 
            rb_1->parent->rchild = rb_2;
    }
    else if(rb_1 == root){
        root = rb_2;
    }
    rb_2->parent = rb_1->parent;

    rb_1->parent = rb_2;
    rb_2->lchild = rb_1;

    rb_1->rchild = rb_3;
    if(rb_3 != NULL)rb_3->parent = rb_1;

    return rb_2;
}

rb_node* rb_search(int key){
    rb_node *p = root;
    while(p != NULL){
        if(key < p->key)
            p = p->lchild;
        else if(key > p->key)
            p = p->rchild;
        else
            break;
    }
    return p;
}

void rb_insert(int key){
    rb_node *p=root, *q=NULL;

    if(root == NULL){
        root = get_node(NULL, key);
        root->colour = BLACK;
        return;
    }

    while(p != NULL){
        q = p;
        if(key < p->key)
            p = p->lchild;
        else if(key > p->key)
            p = p->rchild;
        else return;
    }

    if(key < q->key)
        q->lchild = get_node(q, key);
    else
        q->rchild = get_node(q, key);

    while(q != NULL && q->colour == RED){
        p = q->parent;//p won't null, or BUG.

        if(p->lchild == q){
            if(q->rchild != NULL && q->rchild->colour == RED)
                counter_clock_wise(q);        
            q = clock_wise(p);
            q->lchild->colour = BLACK;
        }
        else{
            if(q->lchild != NULL && q->lchild->colour == RED)
                clock_wise(q);
            q = counter_clock_wise(p);
            q->rchild->colour = BLACK;
        }

        q = q->parent;
    }
    root->colour = BLACK;
}

void show_rb_tree(rb_node* node){
    if(node == NULL)
        return;
    if(node->colour == 0){
        if(node->lchild != NULL && node->rchild != NULL){
            if(node->lchild->colour == 0 && node->rchild->colour == 0){
                printf("(|红结点:%d|——|红左结点:%d|——|红右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }
            if(node->lchild->colour == 0 && node->rchild->colour == 1){
                printf("(|红结点:%d|——|红左结点:%d|——|黑右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }

            if(node->lchild->colour == 1 && node->rchild->colour == 0){
                printf("(|红结点:%d|——|黑左结点:%d|——|红右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }

            if(node->lchild->colour == 1 && node->rchild->colour == 1){
                printf("(|红结点:%d|——|黑左结点:%d|——|黑右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }
        } else if(node->lchild != NULL && node->rchild == NULL ){

            if(node->lchild->colour == 0 ){
                printf("(|红结点:%d|——|红左结点:%d|——|右空|)\n", node->key,node->lchild->key);
            }


            if(node->lchild->colour == 1 && node->rchild->colour == 0){
                printf("(|红结点:%d|——|黑左结点:%d|——|右空|)\n", node->key,node->lchild->key);
            }

        } else if(node->lchild == NULL && node->rchild != NULL ){

            if(node->rchild->colour == 0 ){
                printf("(|红结点:%d|——|左空|——|红右结点:%d|)\n", node->key,node->rchild->key);
            }


            if(node->rchild->colour == 1 ){
                printf("(|红结点:%d|——|左空|——|红右结点:%d|)\n", node->key,node->rchild->key);
            }

        } else {
            printf("(|红结点:%d|——|左空|——|右空|)\n", node->key);
        }
    }
    if(node->colour == 1){
        if(node->lchild != NULL && node->rchild != NULL){
            if(node->lchild->colour == 0 && node->rchild->colour == 0){
                printf("(|黑结点:%d|——|红左结点:%d|——|红右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }
            if(node->lchild->colour == 0 && node->rchild->colour == 1){
                printf("(|黑结点:%d|——|红左结点:%d|——|黑右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }

            if(node->lchild->colour == 1 && node->rchild->colour == 0){
                printf("(|黑结点:%d|——|黑左结点:%d|——|红右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }

            if(node->lchild->colour == 1 && node->rchild->colour == 1){
                printf("(|黑结点:%d|——|黑左结点:%d|——|黑右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }
        } else if(node->lchild != NULL && node->rchild == NULL ){

            if(node->lchild->colour == 0 ){
                printf("(|黑结点:%d|——|红左结点:%d|——|右空|)\n", node->key,node->lchild->key);
            }


            if(node->lchild->colour == 1 && node->rchild->colour == 0){
                printf("(|黑结点:%d|——|黑左结点:%d|——|右空|)\n", node->key,node->lchild->key);
            }

        } else if(node->lchild == NULL && node->rchild != NULL ){

            if(node->rchild->colour == 0 ){
                printf("(|黑结点:%d|——|左空|——|红右结点:%d|)\n", node->key,node->rchild->key);
            }


            if(node->rchild->colour == 1 ){
                printf("(|黑结点:%d|——|左空|——|红右结点:%d|)\n", node->key,node->rchild->key);
            }

        } else {
            printf("(|黑结点:%d|——|左空|——|右空|)\n", node->key);
        }
    }
    //---------------------
    if(node->lchild != NULL){
        //printf("遍历结点%d左子树\n",node->key);
        show_rb_tree(node->lchild);
    }
    if(node->rchild != NULL){
        //printf("遍历结点%d右子树\n",node->key);
        show_rb_tree(node->rchild);
    }



    //printf("[0]\n");
}

void rb_delete(int key){
    rb_node *v = rb_search(key), *u, *p, *c, *b;
    int tmp;
    if(v == NULL) return;

    u = v;
    if(v->lchild != NULL && v->rchild != NULL){
        u = v->rchild;
        while(u->lchild != NULL){
            u = u->lchild;
        }
        tmp = u->key;
        u->key = v->key;
        v->key = tmp;
    }

    //u is the node to free.
    if(u->lchild != NULL)
        c = u->lchild;
    else 
        c = u->rchild;

    p = u->parent;
    if(p != NULL){
        //remove u from rb_tree.
        if(p->lchild == u)
            p->lchild = c;
        else
            p->rchild = c;
    }
    else{
        //u is root.
        root = c;
        free((void*)u);
        return;
    }

    //u is not root and u is RED, this will not unbalance.
    if(u->colour == RED){
        free((void*)u);
        return;
    }

    free((void*)u);
    u = c;

    //u is the first node to balance.
    while(u != root){
        if(u != NULL && u->colour == RED){
            //if u is RED, change it to BLACK can finsh.
            u->colour = BLACK;
            return;
        }

        if(u == p->lchild)
            b = p->rchild;
        else 
            b = p->lchild;

        printf("%d\n", b->key);

        //b is borther of u. b can't be null, or the rb_tree is must not balance.
        if(b->colour == BLACK){
            //If b's son is RED, rotate the node.
            if(b->lchild != NULL && b->lchild->colour == RED){
                if(u == p->lchild){
                    b = clock_wise(b);
                    b->colour = BLACK;
                    b->rchild->colour = RED;

                    p = counter_clock_wise(p);
                    p->colour = p->lchild->colour;
                    p->lchild->colour = BLACK;
                    p->rchild->colour = BLACK;
                }
                else{
                    p = clock_wise(p);
                    p->colour = p->rchild->colour;
                    p->rchild->colour = BLACK;
                    p->lchild->colour = BLACK;
                }

                return;
            }
            else if(b->rchild != NULL && b->rchild->colour == RED){
                if(u == p->rchild){
                    b = counter_clock_wise(b);
                    b->colour = BLACK;
                    b->lchild->colour = RED;

                    p = clock_wise(p);
                    p->colour = p->rchild->colour;
                    p->rchild->colour = BLACK;
                    p->lchild->colour = BLACK;
                }
                else{
                    p = counter_clock_wise(p);
                    p->colour = p->lchild->colour;
                    p->lchild->colour = BLACK;
                    p->rchild->colour = BLACK;
                }        
                return;
            }
            else{//if b's sons are BLACK, make b RED and move up u.
                b->colour = RED;
                u = p;
                p = u->parent;
                continue;
            }
        }
        else{
            if(u == p->lchild){
                p = counter_clock_wise(p);
                p->colour = BLACK;
                p->lchild->colour = RED;
                p = p->lchild;
            }
            else{
                p = clock_wise(p);
                p->colour = BLACK;
                p->rchild->colour = RED;
                p = p->rchild;
            }
        }
    }
    root->colour = BLACK;
}




int main(){
    int i;
    root = NULL;
    int N = 10 ; // 循环次数
    for(i = 1; i <= N; i++){    
        rb_insert(i);
    }
    rb_delete(9);
    rb_delete(3);
    rb_delete(7);
    printf("根节点%d\n",root->key);
    show_rb_tree(root);
    printf("\n");

    printf("删除根节点节点%d后:\n",root->key);
    int  rootValue = root->key ;
    rb_delete(root->key);
    printf("当前根节点%d\n",root->key);
    show_rb_tree(root);
    printf("\n");

    printf("插入原根节点后 %d:\n",rootValue);
    rb_insert(rootValue);
    printf("当前根节点%d\n",root->key);
    show_rb_tree(root);
    printf("\n");

    for(i = 1; i <= N; i++){
        rb_node* node = rb_search(i);
        if( node != NULL ){
            if(node->colour == 0){
                printf("node%d的颜色是 红\n",i);
            }else{
                printf("node%d的颜色是 黑\n",i);
            }

        } else{
            printf("node%d不存在\n",i);
        }
    }



    return 0;
}
//输出结果:
//1
//6
//10
//4
//根节点6
//(|黑结点:6|——|红左结点:4|——|黑右结点:8|)
//(|红结点:4|——|黑左结点:2|——|黑右结点:5|)
//(|黑结点:2|——|红左结点:1|——|右空|)
//(|红结点:1|——|左空|——|右空|)
//(|黑结点:5|——|左空|——|右空|)
//(|黑结点:8|——|左空|——|红右结点:10|)
//(|红结点:10|——|左空|——|右空|)
//删除根节点节点6后:
//当前根节点8
//(|黑结点:8|——|红左结点:4|——|黑右结点:10|)
//(|红结点:4|——|黑左结点:2|——|黑右结点:5|)
//(|黑结点:2|——|红左结点:1|——|右空|)
//(|红结点:1|——|左空|——|右空|)
//(|黑结点:5|——|左空|——|右空|)
//(|黑结点:10|——|左空|——|右空|)
//插入原根节点后 6:
//当前根节点8
//(|黑结点:8|——|红左结点:4|——|黑右结点:10|)
//(|红结点:4|——|黑左结点:2|——|黑右结点:5|)
//(|黑结点:2|——|红左结点:1|——|右空|)
//(|红结点:1|——|左空|——|右空|)
//(|黑结点:5|——|左空|——|红右结点:6|)
//(|红结点:6|——|左空|——|右空|)
//(|黑结点:10|——|左空|——|右空|)
//node1的颜色是 红
//node2的颜色是 黑
//node3不存在
//node4的颜色是 红
//node5的颜色是 黑
//node6的颜色是 红
//node7不存在
//node8的颜色是 黑
//node9不存在
//node10的颜色是 黑
//

http://www.runoob.com/try/runcode.php?filename=helloworld&type=c 【C语言在线编译】

// 红黑树的C语言实现
#include <stdio.h>
#include <stdlib.h>
const int RED = 0;
const int BLACK = 1;

//---编译条件严格,自己引用自己 需要额外定义一个数据类型 使用typedef关键字
/*  https://tool.lu/coderunner/
struct rb_node_p;
typedef struct rb_node_p rb_node;

struct rb_node_p{
    rb_node* lchild, *rchild, *parent;
    int key, colour;
};
*/

/*  http://www.runoob.com/try/runcode.php?filename=helloworld&type=c
struct rb_node{
    rb_node* lchild, *rchild, *parent;
    int key, colour;
};

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

struct rb_node{
    rb_node* lchild, *rchild, *parent;
    int key, colour;
};

rb_node* root;

rb_node* get_node( rb_node* parent, int key);
void rb_insert(int key);
rb_node* rb_search(int key);
void rb_delete(int key);
rb_node* clock_wise( rb_node* node);
rb_node* counter_clock_wise( rb_node* node);
void show_rb_tree( rb_node* node);

rb_node* get_node( rb_node* parent, int key){
    rb_node *tmp = (rb_node*)malloc(sizeof(rb_node));
    tmp->key = key;
    tmp->colour = RED;
    tmp->parent = parent;
    tmp->lchild = tmp->rchild = NULL;
    return tmp;
}

rb_node* clock_wise(rb_node* node){
    if(node == NULL || node->lchild == NULL)
        return NULL;

    rb_node *rb_1=node, *rb_2=node->lchild, *rb_3=node->lchild->rchild;
    if(rb_1->parent != NULL){
        if(rb_1->parent->lchild == rb_1)
            rb_1->parent->lchild = rb_2;
        else
            rb_1->parent->rchild = rb_2;
    }else if(rb_1 == root){
        root = rb_2;
    }
    rb_2->parent = rb_1->parent;

    rb_1->parent = rb_2;
    rb_2->rchild = rb_1;

    rb_1->lchild = rb_3;
    if(rb_3 != NULL)rb_3->parent = rb_1;

    return rb_2;    
}

rb_node* counter_clock_wise(rb_node* node){
    if(node == NULL || node->rchild == NULL)
        return NULL;

    rb_node *rb_1=node, *rb_2=node->rchild, *rb_3=node->rchild->lchild;
    if(rb_1->parent != NULL){
        if(rb_1->parent->lchild == rb_1)
            rb_1->parent->lchild = rb_2;
        else 
            rb_1->parent->rchild = rb_2;
    }
    else if(rb_1 == root){
        root = rb_2;
    }
    rb_2->parent = rb_1->parent;

    rb_1->parent = rb_2;
    rb_2->lchild = rb_1;

    rb_1->rchild = rb_3;
    if(rb_3 != NULL)rb_3->parent = rb_1;

    return rb_2;
}

rb_node* rb_search(int key){
    rb_node *p = root;
    while(p != NULL){
        if(key < p->key)
            p = p->lchild;
        else if(key > p->key)
            p = p->rchild;
        else
            break;
    }
    return p;
}

void rb_insert(int key){
    rb_node *p=root, *q=NULL;

    if(root == NULL){
        root = get_node(NULL, key);
        root->colour = BLACK;
        return;
    }

    while(p != NULL){
        q = p;
        if(key < p->key)
            p = p->lchild;
        else if(key > p->key)
            p = p->rchild;
        else return;
    }

    if(key < q->key)
        q->lchild = get_node(q, key);
    else
        q->rchild = get_node(q, key);

    while(q != NULL && q->colour == RED){
        p = q->parent;//p won't null, or BUG.

        if(p->lchild == q){
            if(q->rchild != NULL && q->rchild->colour == RED)
                counter_clock_wise(q);        
            q = clock_wise(p);
            q->lchild->colour = BLACK;
        }
        else{
            if(q->lchild != NULL && q->lchild->colour == RED)
                clock_wise(q);
            q = counter_clock_wise(p);
            q->rchild->colour = BLACK;
        }

        q = q->parent;
    }
    root->colour = BLACK;
}

void show_rb_tree(rb_node* node){
    if(node == NULL)
        return;
    if(node->colour == 0){
        if(node->lchild != NULL && node->rchild != NULL){
            if(node->lchild->colour == 0 && node->rchild->colour == 0){
                printf("(|红结点:%d|——|红左结点:%d|——|红右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }
            if(node->lchild->colour == 0 && node->rchild->colour == 1){
                printf("(|红结点:%d|——|红左结点:%d|——|黑右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }

            if(node->lchild->colour == 1 && node->rchild->colour == 0){
                printf("(|红结点:%d|——|黑左结点:%d|——|红右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }

            if(node->lchild->colour == 1 && node->rchild->colour == 1){
                printf("(|红结点:%d|——|黑左结点:%d|——|黑右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }
        } else if(node->lchild != NULL && node->rchild == NULL ){

            if(node->lchild->colour == 0 ){
                printf("(|红结点:%d|——|红左结点:%d|——|右空|)\n", node->key,node->lchild->key);
            }


            if(node->lchild->colour == 1 && node->rchild->colour == 0){
                printf("(|红结点:%d|——|黑左结点:%d|——|右空|)\n", node->key,node->lchild->key);
            }

        } else if(node->lchild == NULL && node->rchild != NULL ){

            if(node->rchild->colour == 0 ){
                printf("(|红结点:%d|——|左空|——|红右结点:%d|)\n", node->key,node->rchild->key);
            }


            if(node->rchild->colour == 1 ){
                printf("(|红结点:%d|——|左空|——|红右结点:%d|)\n", node->key,node->rchild->key);
            }

        } else {
            printf("(|红结点:%d|——|左空|——|右空|)\n", node->key);
        }
    }
    if(node->colour == 1){
        if(node->lchild != NULL && node->rchild != NULL){
            if(node->lchild->colour == 0 && node->rchild->colour == 0){
                printf("(|黑结点:%d|——|红左结点:%d|——|红右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }
            if(node->lchild->colour == 0 && node->rchild->colour == 1){
                printf("(|黑结点:%d|——|红左结点:%d|——|黑右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }

            if(node->lchild->colour == 1 && node->rchild->colour == 0){
                printf("(|黑结点:%d|——|黑左结点:%d|——|红右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }

            if(node->lchild->colour == 1 && node->rchild->colour == 1){
                printf("(|黑结点:%d|——|黑左结点:%d|——|黑右结点:%d|)\n", node->key,node->lchild->key,node->rchild->key);
            }
        } else if(node->lchild != NULL && node->rchild == NULL ){

            if(node->lchild->colour == 0 ){
                printf("(|黑结点:%d|——|红左结点:%d|——|右空|)\n", node->key,node->lchild->key);
            }


            if(node->lchild->colour == 1 && node->rchild->colour == 0){
                printf("(|黑结点:%d|——|黑左结点:%d|——|右空|)\n", node->key,node->lchild->key);
            }

        } else if(node->lchild == NULL && node->rchild != NULL ){

            if(node->rchild->colour == 0 ){
                printf("(|黑结点:%d|——|左空|——|红右结点:%d|)\n", node->key,node->rchild->key);
            }


            if(node->rchild->colour == 1 ){
                printf("(|黑结点:%d|——|左空|——|红右结点:%d|)\n", node->key,node->rchild->key);
            }

        } else {
            printf("(|黑结点:%d|——|左空|——|右空|)\n", node->key);
        }
    }
    //---------------------
    if(node->lchild != NULL){
        //printf("遍历结点%d左子树\n",node->key);
        show_rb_tree(node->lchild);
    }
    if(node->rchild != NULL){
        //printf("遍历结点%d右子树\n",node->key);
        show_rb_tree(node->rchild);
    }



    //printf("[0]\n");
}

void rb_delete(int key){
    rb_node *v = rb_search(key), *u, *p, *c, *b;
    int tmp;
    if(v == NULL) return;

    u = v;
    if(v->lchild != NULL && v->rchild != NULL){
        u = v->rchild;
        while(u->lchild != NULL){
            u = u->lchild;
        }
        tmp = u->key;
        u->key = v->key;
        v->key = tmp;
    }

    //u is the node to free.
    if(u->lchild != NULL)
        c = u->lchild;
    else 
        c = u->rchild;

    p = u->parent;
    if(p != NULL){
        //remove u from rb_tree.
        if(p->lchild == u)
            p->lchild = c;
        else
            p->rchild = c;
    }
    else{
        //u is root.
        root = c;
        free((void*)u);
        return;
    }

    //u is not root and u is RED, this will not unbalance.
    if(u->colour == RED){
        free((void*)u);
        return;
    }

    free((void*)u);
    u = c;

    //u is the first node to balance.
    while(u != root){
        if(u != NULL && u->colour == RED){
            //if u is RED, change it to BLACK can finsh.
            u->colour = BLACK;
            return;
        }

        if(u == p->lchild)
            b = p->rchild;
        else 
            b = p->lchild;

        printf("%d\n", b->key);

        //b is borther of u. b can't be null, or the rb_tree is must not balance.
        if(b->colour == BLACK){
            //If b's son is RED, rotate the node.
            if(b->lchild != NULL && b->lchild->colour == RED){
                if(u == p->lchild){
                    b = clock_wise(b);
                    b->colour = BLACK;
                    b->rchild->colour = RED;

                    p = counter_clock_wise(p);
                    p->colour = p->lchild->colour;
                    p->lchild->colour = BLACK;
                    p->rchild->colour = BLACK;
                }
                else{
                    p = clock_wise(p);
                    p->colour = p->rchild->colour;
                    p->rchild->colour = BLACK;
                    p->lchild->colour = BLACK;
                }

                return;
            }
            else if(b->rchild != NULL && b->rchild->colour == RED){
                if(u == p->rchild){
                    b = counter_clock_wise(b);
                    b->colour = BLACK;
                    b->lchild->colour = RED;

                    p = clock_wise(p);
                    p->colour = p->rchild->colour;
                    p->rchild->colour = BLACK;
                    p->lchild->colour = BLACK;
                }
                else{
                    p = counter_clock_wise(p);
                    p->colour = p->lchild->colour;
                    p->lchild->colour = BLACK;
                    p->rchild->colour = BLACK;
                }        
                return;
            }
            else{//if b's sons are BLACK, make b RED and move up u.
                b->colour = RED;
                u = p;
                p = u->parent;
                continue;
            }
        }
        else{
            if(u == p->lchild){
                p = counter_clock_wise(p);
                p->colour = BLACK;
                p->lchild->colour = RED;
                p = p->lchild;
            }
            else{
                p = clock_wise(p);
                p->colour = BLACK;
                p->rchild->colour = RED;
                p = p->rchild;
            }
        }
    }
    root->colour = BLACK;
}




int main(){
    int i;
    root = NULL;
    int N = 10 ; // 循环次数
    for(i = 1; i <= N; i++){    
        rb_insert(i);
    }
    rb_delete(9);
    rb_delete(3);
    rb_delete(7);
    printf("根节点%d\n",root->key);
    show_rb_tree(root);
    printf("\n");

    printf("删除根节点节点%d后:\n",root->key);
    int  rootValue = root->key ;
    rb_delete(root->key);
    printf("当前根节点%d\n",root->key);
    show_rb_tree(root);
    printf("\n");

    printf("插入原根节点后 %d:\n",rootValue);
    rb_insert(rootValue);
    printf("当前根节点%d\n",root->key);
    show_rb_tree(root);
    printf("\n");

    for(i = 1; i <= N; i++){
        rb_node* node = rb_search(i);
        if( node != NULL ){
            if(node->colour == 0){
                printf("node%d的颜色是 红\n",i);
            }else{
                printf("node%d的颜色是 黑\n",i);
            }

        } else{
            printf("node%d不存在\n",i);
        }
    }



    return 0;
}
//输出结果:
//1
//6
//10
//4
//根节点6
//(|黑结点:6|——|红左结点:4|——|黑右结点:8|)
//(|红结点:4|——|黑左结点:2|——|黑右结点:5|)
//(|黑结点:2|——|红左结点:1|——|右空|)
//(|红结点:1|——|左空|——|右空|)
//(|黑结点:5|——|左空|——|右空|)
//(|黑结点:8|——|左空|——|红右结点:10|)
//(|红结点:10|——|左空|——|右空|)
//删除根节点节点6后:
//当前根节点8
//(|黑结点:8|——|红左结点:4|——|黑右结点:10|)
//(|红结点:4|——|黑左结点:2|——|黑右结点:5|)
//(|黑结点:2|——|红左结点:1|——|右空|)
//(|红结点:1|——|左空|——|右空|)
//(|黑结点:5|——|左空|——|右空|)
//(|黑结点:10|——|左空|——|右空|)
//插入原根节点后 6:
//当前根节点8
//(|黑结点:8|——|红左结点:4|——|黑右结点:10|)
//(|红结点:4|——|黑左结点:2|——|黑右结点:5|)
//(|黑结点:2|——|红左结点:1|——|右空|)
//(|红结点:1|——|左空|——|右空|)
//(|黑结点:5|——|左空|——|红右结点:6|)
//(|红结点:6|——|左空|——|右空|)
//(|黑结点:10|——|左空|——|右空|)
//node1的颜色是 红
//node2的颜色是 黑
//node3不存在
//node4的颜色是 红
//node5的颜色是 黑
//node6的颜色是 红
//node7不存在
//node8的颜色是 黑
//node9不存在
//node10的颜色是 黑
//
展开阅读全文

没有更多推荐了,返回首页