青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆-167  評論-8  文章-0  trackbacks-0
我曾經寫過兩個兩個紅黑樹的實現(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加入紅黑樹的支持, 找出來曾經實現(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 2011-05-20 17:21 老馬驛站 閱讀(283) 評論(0)  編輯 收藏 引用 所屬分類: c++algorithm
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久精品99国产精品| 久久激情中文| 欧美精品www在线观看| 亚洲国产成人高清精品| 免费在线看成人av| 免费亚洲电影在线| 亚洲精品一区二区三| 亚洲精品国产精品乱码不99| 久久一区国产| 99精品久久久| 香蕉免费一区二区三区在线观看| 久久综合久久综合久久综合| 91久久综合| 亚洲一区二区三区成人在线视频精品| 麻豆精品在线视频| 一本色道久久综合亚洲精品婷婷| 亚洲免费电影在线| 国产深夜精品| 亚洲国产一区在线| 国产精品视频久久| 欧美激情一区二区| 国产精品乱码一区二三区小蝌蚪| 性做久久久久久| 久久米奇亚洲| 亚洲一区二区四区| 久久久久久久尹人综合网亚洲 | 午夜久久电影网| 伊人久久综合| 亚洲五月婷婷| 亚洲精品国产视频| 亚洲欧美日本国产有色| 在线看日韩欧美| 亚洲视频一起| 日韩一级片网址| 久久精品男女| 亚洲视频在线免费观看| 久久久999| 午夜精品一区二区在线观看 | 免费视频一区| 国产精品日韩在线| 亚洲国内自拍| 怡红院精品视频| 午夜精品久久久久久久久 | 欧美人与性动交cc0o| 欧美一级精品大片| 欧美日韩在线亚洲一区蜜芽| 久久综合五月| 国产日韩欧美在线| 99在线热播精品免费| 亚洲人午夜精品| 久久久九九九九| 欧美在线视频观看| 欧美三区美女| 日韩视频一区二区| 亚洲美女av黄| 欧美粗暴jizz性欧美20| 欧美大片第1页| 一区二区三区我不卡| 香港久久久电影| 欧美亚洲日本网站| 欧美视频免费| 99re6这里只有精品视频在线观看| 黄色综合网站| 久久免费高清| 免费欧美日韩| 亚洲国产精品久久91精品| 久久久久免费视频| 裸体歌舞表演一区二区| 狠狠色综合色综合网络| 久久久噜噜噜久久中文字免| 久久人人爽人人| 亚洲国产精品电影| 欧美成人69av| 亚洲黄色在线| 亚洲性av在线| 国产精品久久久久久久久免费| 亚洲精品黄网在线观看| 亚洲天堂黄色| 国产伦精品一区二区三区照片91 | 欧美日韩国产片| 亚洲精品在线视频观看| 亚洲图片欧美日产| 国产欧美视频在线观看| 欧美一区网站| 欧美激情91| 一区二区毛片| 国产视频精品va久久久久久| 久久天堂av综合合色| 亚洲人成人99网站| 亚洲一区二区视频在线观看| 国产精品尤物| 看片网站欧美日韩| 日韩视频中文字幕| 久久国产精彩视频| 亚洲精品久久久蜜桃| 欧美午夜宅男影院在线观看| 欧美一级淫片aaaaaaa视频| 蜜臀va亚洲va欧美va天堂| 亚洲精品综合| 国产情人节一区| 欧美国产日韩一区| 亚洲欧美国产高清| 欧美激情在线播放| 午夜日韩激情| 日韩一级成人av| 国产一区二区毛片| 欧美日产一区二区三区在线观看| 亚洲天堂网在线观看| 欧美.日韩.国产.一区.二区| 亚洲午夜精品国产| 精久久久久久| 国产精品乱子乱xxxx| 另类综合日韩欧美亚洲| 亚洲在线视频一区| 亚洲激情视频在线观看| 久久精品盗摄| 亚洲欧美清纯在线制服| 亚洲国产婷婷香蕉久久久久久99| 欧美日韩精品久久久| 卡通动漫国产精品| 亚洲欧美一区二区原创| 亚洲精选在线观看| 欧美电影免费观看网站| 久久成人资源| 亚洲尤物在线| 99精品国产热久久91蜜凸| 亚洲第一页在线| 国产亚洲欧美在线| 国产精品一区二区欧美| 欧美日韩一区二区欧美激情| 狂野欧美激情性xxxx欧美| 午夜亚洲一区| 亚洲影视在线| 一区二区免费在线播放| 亚洲精品三级| 亚洲欧洲日产国产综合网| 欧美成人视屏| 欧美不卡激情三级在线观看| 久久久国产视频91| 久久精品一本| 久久久久久久国产| 久久精品五月| 久久久欧美精品| 久久亚洲精品网站| 噜噜噜噜噜久久久久久91| 久久久久久一区二区| 久久精品国产第一区二区三区最新章节| 亚洲精品一区二区网址| 亚洲国产日韩欧美一区二区三区| 国产亚洲一区二区三区在线观看| 欧美性猛交xxxx乱大交蜜桃| 欧美日韩在线一区二区| 国产精品ⅴa在线观看h| 国产精品美女一区二区| 国产精品毛片a∨一区二区三区| 欧美精品一区二区三区久久久竹菊 | 欧美成人精品一区二区三区| 久久精品综合| 蜜桃久久av一区| 欧美精品成人| 欧美色精品天天在线观看视频 | 亚洲一区二区三区乱码aⅴ蜜桃女| 亚洲精品一区二区三区99| 亚洲狼人精品一区二区三区| av成人毛片| 欧美在线首页| 牛人盗摄一区二区三区视频| 欧美日本一区二区三区| 国产精品国产| 狠狠色丁香婷婷综合久久片| 亚洲国产精品一区二区尤物区| 亚洲国内精品在线| 亚洲一区bb| 久久综合九色综合欧美狠狠| 亚洲黄色片网站| 亚洲一区精品在线| 久久久噜噜噜久久中文字免| 欧美日韩aaaaa| 国产乱码精品一区二区三区忘忧草 | 亚洲欧洲在线观看| 一区二区三区精品在线| 久久精精品视频| 欧美人与性动交cc0o| 国产视频在线观看一区二区| 亚洲成人影音| 亚洲欧美精品在线观看| 久久精品男女| 亚洲精品字幕| 久久久噜噜噜久久人人看| 欧美视频在线播放| 在线日韩欧美| 欧美一区二区三区四区在线观看| 久久精品免费电影| 99国产精品久久久久老师 | 老司机免费视频一区二区三区| 欧美激情精品久久久久久久变态| 99精品黄色片免费大全| 久久综合成人精品亚洲另类欧美 | 欧美自拍丝袜亚洲| 欧美色欧美亚洲高清在线视频|