• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            那誰的技術博客

            感興趣領域:高性能服務器編程,存儲,算法,Linux內核
            隨筆 - 210, 文章 - 0, 評論 - 1183, 引用 - 0
            數(shù)據(jù)加載中……

            紅黑樹的實現(xiàn)源碼(第二次修訂版)

            我曾經(jīng)寫過兩個兩個紅黑樹的實現(xiàn), 分別在:
            http://www.shnenglu.com/converse/archive/2006/10/07/13413.html
            http://www.shnenglu.com/converse/archive/2007/11/28/37430.html

            最近因為要給ccache加入紅黑樹的支持, 找出來曾經(jīng)實現(xiàn)的代碼作為參考, 這才發(fā)現(xiàn)原來的實現(xiàn)都是有問題的,也怪我的測試用例寫的不好, 僅僅對插入操作進行了測試, 我向所有因為閱讀了這份代碼而造成困惑的朋友表示道歉.

            這次重新實現(xiàn), 所有的代碼推倒重新編寫, 參考了linux內核中紅黑樹的實現(xiàn)算法, 并且對測試用例進行了加強,希望這是最后一個對紅黑樹算法的修訂版本.

            /*-----------------------------------------------------------
                RB-Tree的插入和刪除操作的實現(xiàn)算法
                參考資料:
                1) <<Introduction to algorithm>>
                2) http://lxr.linux.no/linux/lib/rbtree.c

                作者:http://www.shnenglu.com/converse/
                您可以自由的傳播,修改這份代碼,轉載處請注明原作者

                紅黑樹的幾個性質:
                1) 每個結點只有紅和黑兩種顏色
                2) 根結點是黑色的
                3)空節(jié)點是黑色的(紅黑樹中,根節(jié)點的parent以及所有葉節(jié)點lchild、rchild都不指向NULL,而是指向一個定義好的空節(jié)點)。
                4) 如果一個結點是紅色的,那么它的左右兩個子結點的顏色是黑色的
                5) 對于每個結點而言,從這個結點到葉子結點的任何路徑上的黑色結點
                的數(shù)目相同
            -------------------------------------------------------------*/
             
            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>

            typedef int key_t;
            typedef int data_t;

            typedef enum color_t
            {
                RED = 0,
                BLACK = 1
            }color_t;

            typedef struct rb_node_t
            {
                struct rb_node_t *left, *right, *parent;
                key_t key;
                data_t data;
                color_t color;
            }rb_node_t;

            /* forward declaration */
            rb_node_t* rb_insert(key_t key, data_t data, rb_node_t* root);
            rb_node_t* rb_search(key_t key, rb_node_t* root);
            rb_node_t* rb_erase(key_t key, rb_node_t* root);

            int main()
            {
                int i, count = 900000;
                key_t key;
                rb_node_t* root = NULL, *node = NULL;
               
                srand(time(NULL));
                for (i = 1; i < count; ++i)
                {
                    key = rand() % count;
                    if ((root = rb_insert(key, i, root)))
                    {
                        printf("[i = %d] insert key %d success!\n", i, key);
                    }
                    else
                    {
                        printf("[i = %d] insert key %d error!\n", i, key);
                        exit(-1);
                    }

                    if ((node = rb_search(key, root)))
                    {
                        printf("[i = %d] search key %d success!\n", i, key);
                    }
                    else
                    {
                        printf("[i = %d] search key %d error!\n", i, key);
                        exit(-1);
                    }
                    if (!(i % 10))
                    {
                        if ((root = rb_erase(key, root)))
                        {
                            printf("[i = %d] erase key %d success\n", i, key);
                        }
                        else
                        {
                            printf("[i = %d] erase key %d error\n", i, key);
                        }
                    }
                }

                return 0;
            }

            static rb_node_t* rb_new_node(key_t key, data_t data)
            {
                rb_node_t *node = (rb_node_t*)malloc(sizeof(struct rb_node_t));

                if (!node)
                {
                    printf("malloc error!\n");
                    exit(-1);
                }
                node->key = key, node->data = data;

                return node;
            }

            /*-----------------------------------------------------------
            |   node           right
            |   / \    ==>     / \
            |   a  right     node  y
            |       / \           / \
            |       b  y         a   b
             -----------------------------------------------------------*/
            static rb_node_t* rb_rotate_left(rb_node_t* node, rb_node_t* root)
            {
                rb_node_t* right = node->right;

                if ((node->right = right->left))
                {
                    right->left->parent = node;
                }
                right->left = node;

                if ((right->parent = node->parent))
                {
                    if (node == node->parent->right)
                    {
                        node->parent->right = right;
                    }
                    else
                    {
                        node->parent->left = right;
                    }
                }
                else
                {
                    root = right;
                }
                node->parent = right;

                return root;
            }

            /*-----------------------------------------------------------
            |       node           left
            |       / \            / \
            |    left  y   ==>    a   node
            |   / \               / \
            |  a   b             b   y
            -----------------------------------------------------------*/
            static rb_node_t* rb_rotate_right(rb_node_t* node, rb_node_t* root)
            {
                rb_node_t* left = node->left;

                if ((node->left = left->right))
                {
                    left->right->parent = node;
                }
                left->right = node;

                if ((left->parent = node->parent))
                {
                    if (node == node->parent->right)
                    {
                        node->parent->right = left;
                    }
                    else
                    {
                        node->parent->left = left;
                    }
                }
                else
                {
                    root = left;
                }
                node->parent = left;

                return root;
            }

            static rb_node_t* rb_insert_rebalance(rb_node_t *node, rb_node_t *root)
            {
                rb_node_t *parent, *gparent, *uncle, *tmp;

                while ((parent = node->parent) && parent->color == RED)
                {
                    gparent = parent->parent;

                    if (parent == gparent->left)
                    {
                        uncle = gparent->right;
                        if (uncle && uncle->color == RED)
                        {
                            uncle->color = BLACK;
                            parent->color = BLACK;
                            gparent->color = RED;
                            node = gparent;
                        }
                        else
                        {
                            if (parent->right == node)
                            {
                                root = rb_rotate_left(parent, root);
                                tmp = parent;
                                parent = node;
                                node = tmp;
                            }

                            parent->color = BLACK;
                            gparent->color = RED;
                            root = rb_rotate_right(gparent, root);
                        }
                    }
                    else
                    {
                        uncle = gparent->left;
                        if (uncle && uncle->color == RED)
                        {
                            uncle->color = BLACK;
                            parent->color = BLACK;
                            gparent->color = RED;
                            node = gparent;
                        }
                        else
                        {
                            if (parent->left == node)
                            {
                                root = rb_rotate_right(parent, root);
                                tmp = parent;
                                parent = node;
                                node = tmp;
                            }

                            parent->color = BLACK;
                            gparent->color = RED;
                            root = rb_rotate_left(gparent, root);
                        }
                    }
                }

                root->color = BLACK;

                return root;
            }

            static rb_node_t* rb_erase_rebalance(rb_node_t *node, rb_node_t *parent, rb_node_t *root)
            {
                rb_node_t *other, *o_left, *o_right;

                while ((!node || node->color == BLACK) && node != root)
                {
                    if (parent->left == node)
                    {
                        other = parent->right;
                        if (other->color == RED)
                        {
                            other->color = BLACK;
                            parent->color = RED;
                            root = rb_rotate_left(parent, root);
                            other = parent->right;
                        }
                        if ((!other->left || other->left->color == BLACK) &&
                            (!other->right || other->right->color == BLACK))
                        {
                            other->color = RED;
                            node = parent;
                            parent = node->parent;
                        }
                        else
                        {
                            if (!other->right || other->right->color == BLACK)
                            {
                                if ((o_left = other->left))
                                {
                                    o_left->color = BLACK;
                                }
                                other->color = RED;
                                root = rb_rotate_right(other, root);
                                other = parent->right;
                            }
                            other->color = parent->color;
                            parent->color = BLACK;
                            if (other->right)
                            {
                                other->right->color = BLACK;
                            }
                            root = rb_rotate_left(parent, root);
                            node = root;
                            break;
                        }
                    }
                    else
                    {
                        other = parent->left;
                        if (other->color == RED)
                        {
                            other->color = BLACK;
                            parent->color = RED;
                            root = rb_rotate_right(parent, root);
                            other = parent->left;
                        }
                        if ((!other->left || other->left->color == BLACK) &&
                            (!other->right || other->right->color == BLACK))
                        {
                            other->color = RED;
                            node = parent;
                            parent = node->parent;
                        }
                        else
                        {
                            if (!other->left || other->left->color == BLACK)
                            {
                                if ((o_right = other->right))
                                {
                                    o_right->color = BLACK;
                                }
                                other->color = RED;
                                root = rb_rotate_left(other, root);
                                other = parent->left;
                            }
                            other->color = parent->color;
                            parent->color = BLACK;
                            if (other->left)
                            {
                                other->left->color = BLACK;
                            }
                            root = rb_rotate_right(parent, root);
                            node = root;
                            break;
                        }
                    }
                }

                if (node)
                {
                    node->color = BLACK;
                }

                return root;
            }

            static rb_node_t* rb_search_auxiliary(key_t key, rb_node_t* root, rb_node_t** save)
            {
                rb_node_t *node = root, *parent = NULL;
                int ret;

                while (node)
                {
                    parent = node;
                    ret = node->key - key;
                    if (0 < ret)
                    {
                        node = node->left;
                    }
                    else if (0 > ret)
                    {
                        node = node->right;
                    }
                    else
                    {
                        return node;
                    }
                }

                if (save)
                {
                    *save = parent;
                }

                return NULL;
            }

            rb_node_t* rb_insert(key_t key, data_t data, rb_node_t* root)
            {
                rb_node_t *parent = NULL, *node;

                parent = NULL;
                if ((node = rb_search_auxiliary(key, root, &parent)))
                {
                    return root;
                }

                node = rb_new_node(key, data);
                node->parent = parent;
                node->left = node->right = NULL;
                node->color = RED;

                if (parent)
                {
                    if (parent->key > key)
                    {
                        parent->left = node;
                    }
                    else
                    {
                        parent->right = node;
                    }
                }
                else
                {
                    root = node;
                }

                return rb_insert_rebalance(node, root);
            }

            rb_node_t* rb_search(key_t key, rb_node_t* root)
            {
                return rb_search_auxiliary(key, root, NULL);
            }

            rb_node_t* rb_erase(key_t key, rb_node_t *root)
            {
                rb_node_t *child, *parent, *old, *left, *node;
                color_t color;

                if (!(node = rb_search_auxiliary(key, root, NULL)))
                {
                    printf("key %d is not exist!\n");
                    return root;
                }

                old = node;

                if (node->left && node->right)
                {
                    node = node->right;
                    while ((left = node->left) != NULL)
                    {
                        node = left;
                    }
                    child = node->right;
                    parent = node->parent;
                    color = node->color;

                    if (child)
                    {
                        child->parent = parent;
                    }
                    if (parent)
                    {
                        if (parent->left == node)
                        {
                            parent->left = child;
                        }
                        else
                        {
                            parent->right = child;
                        }
                    }
                    else
                    {
                        root = child;
                    }

                    if (node->parent == old)
                    {
                        parent = node;
                    }

                    node->parent = old->parent;
                    node->color = old->color;
                    node->right = old->right;
                    node->left = old->left;

                    if (old->parent)
                    {
                        if (old->parent->left == old)
                        {
                            old->parent->left = node;
                        }
                        else
                        {
                            old->parent->right = node;
                        }
                    }
                    else
                    {
                        root = node;
                    }

                    old->left->parent = node;
                    if (old->right)
                    {
                        old->right->parent = node;
                    }
                }
                else
                {
                    if (!node->left)
                    {
                        child = node->right;
                    }
                    else if (!node->right)
                    {
                        child = node->left;
                    }
                    parent = node->parent;
                    color = node->color;

                    if (child)
                    {
                        child->parent = parent;
                    }
                    if (parent)
                    {
                        if (parent->left == node)
                        {
                            parent->left = child;
                        }
                        else
                        {
                            parent->right = child;
                        }
                    }
                    else
                    {
                        root = child;
                    }
                }

                free(old);

                if (color == BLACK)
                {
                    root = rb_erase_rebalance(child, parent, root);
                }

                return root;
            }





            posted on 2008-11-10 17:50 那誰 閱讀(13685) 評論(18)  編輯 收藏 引用 所屬分類: 算法與數(shù)據(jù)結構

            評論

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            我暈倒,有沒有優(yōu)雅一點的,至少短一點的實現(xiàn)?
            2008-11-11 15:42 | Wang Feng

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            @Wang Feng
            樓上的朋友,如果你不滿意,大可以自己也去實現(xiàn)一下看看:)
            2008-11-11 23:33 | 創(chuàng)

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            寫得很藝術
            nice
            2008-11-12 12:28 | haskell

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)[未登錄]  回復  更多評論   

            csdn中的那個也是你發(fā)的吧?呵.....
            2008-11-18 16:53 | lin

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            @lin
            CSDN?我不知道這個事情,這個帖子是我在cppblog的原創(chuàng).
            2008-11-19 00:05 | 創(chuàng)

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            搬個凳子慢慢看
            2009-01-15 01:02 | linnet

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)[未登錄]  回復  更多評論   

            怎麼無法編譯呢?
            2009-04-29 03:33 | candy

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)[未登錄]  回復  更多評論   

            @candy
            你的編譯命令是??
            我這邊直接gcc就行了
            2009-04-29 12:49 | 那誰

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)[未登錄]  回復  更多評論   

            請高手指教,謝謝。
            我將main函數(shù)改為,
            int main()
            {
            int i;
            key_t key;
            rb_node_t* root = NULL, *node = NULL;

            while(1)
            {
            printf("insert: ");
            scanf("%d", &key);
            if(key <= 0)
            break;
            if ((root = rb_insert(key, i, root)))
            {
            printf("[i = %d] insert key %d success!\n", i, key);
            }
            else
            {
            printf("[i = %d] insert key %d error!\n", i, key);
            exit(-1);
            }
            }

            while(1)
            {
            printf("delete: ");
            scanf("%d", &key);
            if(key <= 0)
            break;
            if ((root = rb_erase(key, root)))
            {
            printf("[i = %d] erase key %d success\n", i, key);
            }
            else
            {
            printf("[i = %d] erase key %d error\n", i, key);
            }
            }
            return 0;
            }
            編譯并運行,
            insert: 9
            [i = 2] insert key 9 success!
            insert: 0
            delete: 9
            [i = 2] erase key 9 error
            delete: 0
            請問是什么原因,謝謝。
            2010-02-04 16:51 | Jason

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            When students want to uderstand about the <a href="http://www.exclusivepapers.com/papers-for-money.php">papers for money</a> they can <a href="http://www.exclusivepapers.com/buy-essay.php">buy essay</a> related to this post. Because the critical essay creating has to be a really important thing.
            2010-02-22 15:02 | EllavC

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            @EllavC
            數(shù)依然是有問題的, 將你的測試用例 改為下面的試試
            //-------------------
            int main()
            {
            int i, count = 10000;
            key_t key;
            rb_node_t* root = NULL, *node = NULL;

            srand(time(NULL));
            for (i = 1; i < count; ++i)
            {
            key = rand() % count;
            if ((root = rb_insert(key, i, root)))
            {
            printf("[i = %d] insert key %d success!\n", i, key);
            }
            else
            {
            printf("[i = %d] insert key %d error!\n", i, key);
            exit(-1);
            }

            if ((node = rb_search(key, root)))
            {
            if (i != node->data) {
            printf("[i = %d] search key %d success! <-- X\n", node->data, node->key);
            printf("[i = %d] != [node->data = %d ] <-- [key = %d], [node->key = %d]\n", i, node->data, key, node->key);
            }
            else
            printf("[i = %d] search key %d success!\n", node->data, node->key);
            }
            else
            {
            printf("[i = %d] search key %d error!\n", i, key);
            exit(-1);
            }
            if (!(i % 10))
            {
            if ((root = rb_erase(key, root)))
            {
            printf("[i = %d] erase key %d success\n", i, key);
            }
            else
            {
            printf("[i = %d] erase key %d error\n", i, key);
            }
            }
            }

            return 0;
            }
            //-------------------
            //-------------------
            // 結果的部分內容
            /*
            [i = 9984] != [node->data = 6004 ] <-- [key = 5893], [node->key = 5893]
            [i = 9985] insert key 4330 success!
            [i = 2445] search key 4330 success! <-- X
            [i = 9985] != [node->data = 2445 ] <-- [key = 4330], [node->key = 4330]
            [i = 9986] insert key 1927 success!
            [i = 227] search key 1927 success! <-- X
            [i = 9986] != [node->data = 227 ] <-- [key = 1927], [node->key = 1927]
            [i = 9987] insert key 7978 success!
            [i = 647] search key 7978 success! <-- X
            [i = 9987] != [node->data = 647 ] <-- [key = 7978], [node->key = 7978]
            [i = 9988] insert key 7759 success!
            [i = 4671] search key 7759 success! <-- X
            [i = 9988] != [node->data = 4671 ] <-- [key = 7759], [node->key = 7759]
            [i = 9989] insert key 1231 success!
            [i = 4022] search key 1231 success! <-- X
            [i = 9989] != [node->data = 4022 ] <-- [key = 1231], [node->key = 1231]
            [i = 9990] insert key 1550 success!
            [i = 9990] search key 1550 success!
            [i = 9990] erase key 1550 success
            [i = 9991] insert key 7396 success!
            [i = 7878] search key 7396 success! <-- X
            [i = 9991] != [node->data = 7878 ] <-- [key = 7396], [node->key = 7396]
            [i = 9992] insert key 5687 success!
            [i = 9992] search key 5687 success!
            [i = 9993] insert key 1755 success!
            [i = 9993] search key 1755 success!
            [i = 9994] insert key 9388 success!
            [i = 9994] search key 9388 success!
            [i = 9995] insert key 8158 success!
            [i = 9352] search key 8158 success! <-- X
            [i = 9995] != [node->data = 9352 ] <-- [key = 8158], [node->key = 8158]
            [i = 9996] insert key 7122 success!
            [i = 56] search key 7122 success! <-- X
            [i = 9996] != [node->data = 56 ] <-- [key = 7122], [node->key = 7122]
            [i = 9997] insert key 6220 success!
            [i = 8154] search key 6220 success! <-- X
            [i = 9997] != [node->data = 8154 ] <-- [key = 6220], [node->key = 6220]
            [i = 9998] insert key 9219 success!
            [i = 9998] search key 9219 success!
            */
            //---------------
            /*
            你的測試用例全部正確是因為你沒有去判斷通過key查到的node里面的內容是否和放入的內容一致,我在此比了一下,問題暴露出來了
            */
            2011-07-19 15:46 | rc8523

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            @rc8523
            <-- X 代表的是有問題的地方
            下面一行是節(jié)點詳細內容
            2011-07-19 15:48 | rc8523

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            @rc8523
            因為rb_insert()了已經(jīng)存在的key后,并不返回NULL,而是
            直接返回root指針,所以rb_search()的返回值在這種情況下
            只是上次插入的節(jié)點
            2012-01-05 14:57 | qman007

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            你現(xiàn)在的實現(xiàn)還是有問題吧 好好看看
            2012-04-18 09:19 | Apollo Fang

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            替換數(shù)據(jù)不是更好?
            2012-04-19 10:26 | Apollo Fang

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            也怪我的測試用例寫的不好, 僅僅對插入操作進行了測試, 我向所有因為閱讀了這份代碼而造成困
            2012-08-03 19:25 | regalos originales aniversario

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)  回復  更多評論   

            例寫的不好, 僅僅對插入操作進行了測試, 我向所有因為閱讀了
            2012-08-04 15:17 | wholesale kurti

            # re: 紅黑樹的實現(xiàn)源碼(第二次修訂版)   回復  更多評論   

            也怪我的測試用例寫的不好, 僅僅對插入操作進行了測試, 我向所有因為閱讀了這份代碼而造成困
            2012-11-27 20:19 | abogados divorcios madrid
            久久婷婷五月综合97色一本一本| 麻豆久久| 久久综合88熟人妻| 漂亮人妻被中出中文字幕久久 | 91精品国产高清久久久久久国产嫩草 | 亚洲va久久久久| 亚洲国产高清精品线久久| 狠狠久久综合| 日韩精品无码久久一区二区三| 国产精品日韩深夜福利久久| 国产福利电影一区二区三区久久久久成人精品综合 | 亚洲精品乱码久久久久久按摩 | 东方aⅴ免费观看久久av| 久久丫忘忧草产品| 日韩av无码久久精品免费| 无码精品久久久天天影视| 久久夜色精品国产噜噜麻豆| 国产精品无码久久综合| 久久精品国产免费一区| 国产成人久久久精品二区三区| 日本免费久久久久久久网站| 久久综合九色综合久99| 日韩AV毛片精品久久久| 久久AV高潮AV无码AV| 996久久国产精品线观看| 久久国产三级无码一区二区| 久久精品极品盛宴观看| 久久精品亚洲日本波多野结衣 | 亚洲国产精品无码久久九九| 97精品伊人久久久大香线蕉 | 无码人妻久久一区二区三区免费 | 久久亚洲中文字幕精品一区| 久久国产精品成人片免费| 国产高潮久久免费观看| 精品久久人人爽天天玩人人妻| 狠狠色丁香婷婷久久综合不卡| 香蕉久久AⅤ一区二区三区| 99久久无码一区人妻a黑| 中文字幕精品久久久久人妻| 久久精品国产免费一区| 亚洲国产精品无码久久一区二区|