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

隨筆-381  評論-37  文章-0  trackbacks-0
筆者曾經寫過兩個兩個紅黑樹的實現, 分別在:
http://www.shnenglu.com/converse/archive/2006/10/07/13413.html
http://www.shnenglu.com/converse/archive/2007/11/28/37430.html

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

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

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

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

    紅黑樹的幾個性質:
    1) 每個結點只有紅和黑兩種顏色
    2) 根結點是黑色的
    3)空節點是黑色的(紅黑樹中,根節點的parent以及所有葉節點lchild、rchild都不指向NULL,而是指向一個定義好的空節點)。
    4) 如果一個結點是紅色的,那么它的左右兩個子結點的顏色是黑色的
    5) 對于每個結點而言,從這個結點到葉子結點的任何路徑上的黑色結點
    的數目相同
-------------------------------------------------------------*/
 
#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 2009-02-22 02:14 小王 閱讀(435) 評論(0)  編輯 收藏 引用 所屬分類: 算法與數據結構

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            99re亚洲国产精品| 亚洲韩国精品一区| 欧美国产日韩一二三区| 久久动漫亚洲| 欧美午夜免费影院| 亚洲风情亚aⅴ在线发布| 国产欧美亚洲精品| 亚洲天堂久久| 中文国产一区| 欧美精品七区| 亚洲电影观看| 亚洲娇小video精品| 久久免费视频网站| 久久精品日韩欧美| 国产亚洲精品美女| 欧美一区二区视频免费观看| 亚洲欧美日韩精品综合在线观看| 欧美日本精品在线| 亚洲国产第一| 亚洲精品在线观看免费| 欧美www在线| 亚洲国产精品福利| 亚洲精品免费一区二区三区| 免费黄网站欧美| 欧美黄色一级视频| 91久久精品视频| 欧美经典一区二区| 亚洲精品乱码久久久久久黑人| 亚洲日本精品国产第一区| 免费欧美网站| 亚洲电影在线播放| 亚洲无限乱码一二三四麻| 欧美日韩国产精品一卡| 99re8这里有精品热视频免费| 亚洲精品在线电影| 欧美日韩在线播放三区四区| 中国成人亚色综合网站| 欧美一区午夜精品| 黄色成人av网| 欧美国产日韩精品免费观看| 99在线|亚洲一区二区| 亚洲综合色视频| 国产一区二区三区四区在线观看| 欧美在线播放| 亚洲高清一二三区| 亚洲一区www| 韩国成人福利片在线播放| 久久露脸国产精品| 亚洲精品视频二区| 久久国产精品久久久久久久久久| 黄色一区三区| 欧美日韩成人综合天天影院| 亚洲欧美成人综合| 欧美成在线视频| 亚洲一区二区三区中文字幕| 国产午夜精品全部视频在线播放 | 亚洲国产岛国毛片在线| 亚洲午夜成aⅴ人片| 国产一区二区三区在线观看免费视频| 久久av二区| 日韩香蕉视频| 免费日韩精品中文字幕视频在线| 99国产精品久久| 国产一区91| 欧美看片网站| 久久久精品一区二区三区| 亚洲毛片av| 蜜臀久久99精品久久久久久9| 在线中文字幕不卡| 在线国产亚洲欧美| 国产精品国产三级国产aⅴ无密码| 久久久久久久综合日本| 在线视频一区观看| 亚洲高清免费视频| 久久久久国内| 亚洲欧美中文另类| 一本色道久久综合精品竹菊| 国产一区二区中文| 欧美午夜激情视频| 欧美国产一区在线| 久久免费高清| 亚洲欧美日韩精品久久| 亚洲青涩在线| 免费观看成人| 久久精品一区二区三区不卡| 一区二区久久久久久| 1024欧美极品| 黄色精品免费| 国产一区二区成人久久免费影院| 欧美色欧美亚洲另类二区 | 免费观看成人| 久久精品国产亚洲aⅴ| 亚洲婷婷国产精品电影人久久| 亚洲高清视频在线| 欧美韩日一区| 欧美.com| 欧美a级一区| 麻豆国产精品一区二区三区 | 久久久久99| 欧美在线中文字幕| 性欧美video另类hd性玩具| 亚洲免费视频一区二区| 亚洲午夜激情网页| 亚洲视频电影在线| 亚洲小说春色综合另类电影| 一区二区三区视频在线看| 99热在线精品观看| 一区二区电影免费观看| 99re视频这里只有精品| 亚洲精品综合| 在线一区二区三区四区| 亚洲一区二区免费视频| 亚洲免费在线视频| 午夜激情综合网| 欧美综合国产| 欧美a级片网站| 欧美精品日韩精品| 欧美日韩精品一区二区天天拍小说 | 噜噜噜躁狠狠躁狠狠精品视频| 久久久久女教师免费一区| 久久理论片午夜琪琪电影网| 久久视频在线视频| 麻豆av一区二区三区久久| 欧美wwwwww| 欧美三级视频在线播放| 国产精品视频久久| 好吊成人免视频| 亚洲国产日韩在线| 亚洲午夜国产一区99re久久| 亚洲欧美亚洲| 麻豆av福利av久久av| 欧美激情精品久久久久久免费印度| 亚洲第一黄色网| 一区二区精品国产| 久久久国际精品| 欧美黄色精品| 国产美女精品| 亚洲国产精品一区二区久| 一区二区三区 在线观看视| 欧美一级欧美一级在线播放| 久久综合狠狠综合久久综合88| 亚洲国产裸拍裸体视频在线观看乱了中文 | 亚洲高清av| 亚洲一区欧美二区| 牛夜精品久久久久久久99黑人| 亚洲人体一区| 欧美一区二区三区在线观看| 欧美91福利在线观看| 国产精品系列在线播放| 亚洲精品美女91| 欧美一区二区三区免费视| 欧美高清在线视频| 亚洲一区美女视频在线观看免费| 久久久久久久久一区二区| 欧美性大战久久久久| 亚洲国产精品ⅴa在线观看| 亚洲综合国产| 亚洲激情视频网| 欧美在线观看一区| 欧美午夜精彩| 亚洲精品国产精品国自产在线| 午夜精品成人在线| 亚洲国产精品尤物yw在线观看| 午夜在线a亚洲v天堂网2018| 欧美激情一区二区三区全黄| 韩日欧美一区| 新片速递亚洲合集欧美合集| 亚洲人成人一区二区在线观看| 欧美一级一区| 国产精品久久久久久久app| 亚洲欧洲在线一区| 久久综合色88| 亚洲欧美综合国产精品一区| 欧美日韩精品欧美日韩精品一| 精品动漫一区| 久久精品成人一区二区三区 | 美女视频黄a大片欧美| 国产视频综合在线| 亚洲欧美日韩视频一区| 亚洲精品自在在线观看| 欧美v国产在线一区二区三区| 好吊妞**欧美| 久久久久久久尹人综合网亚洲| 一区二区三区www| 欧美日韩高清区| 一区二区三区久久精品| 91久久视频| 欧美日韩国产999| 日韩一区二区免费看| 亚洲国产毛片完整版| 蜜臀久久久99精品久久久久久| 伊人成综合网伊人222| 久久久久久综合| 久久久久久9| 一区二区在线视频播放| 免费欧美日韩| 免费日韩成人| 99精品热视频| 99视频精品全部免费在线| 欧美午夜精品久久久久免费视 |