libuv tree的實現(xiàn)

看libuv源碼的時候,發(fā)現(xiàn)不僅代碼中使用了雙向鏈表,還有一個伸展樹和紅黑樹的實現(xiàn),全部是linux內(nèi)核風(fēng)格的,數(shù)據(jù)和操作分開,通過宏封裝了指針的操作,實現(xiàn)的非常精妙。

把樹的源碼copy出來,發(fā)現(xiàn)使用起來也非常的簡單。看看如何使用的吧。

源碼在這里https://github.com/libuv/libuv/blob/v1.x/include/tree.h

由于紅黑樹比伸展樹牛逼,libuv也沒有使用伸展樹,下面就只聊聊紅黑樹了。

如何使用libuv的紅黑樹

下面的代碼是一個完整的例子。測試了插入,遍歷,查找,刪除,逆向遍歷。


#include "stdafx.h"
#include "tree.h"
#include <malloc.h>

struct node {
    RB_ENTRY(node) entry;
    int i;
};

int
intcmp(struct node *e1, struct node *e2)
{
    return (e1->i < e2->i ? -1 : e1->i > e2->i);
}

RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
RB_GENERATE(inttree, node, entry, intcmp)

int testdata[] = {
    20, 16, 17, 13, 3, 6, 1, 8, 2, 4, 10, 19, 5, 9, 12, 15, 18,
    7, 11, 14,30,31,32,33
};

int main()
{
    int i;
    struct node *n;

    for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
        if ((n = (struct node *)malloc(sizeof(struct node))) == NULL) {
            printf("malloc return null!!!\n");
            return -1;
        }
        n->i = testdata[i];
        RB_INSERT(inttree, &head, n);
    }

    printf("====================RB_FOREACH=========================\n");
    RB_FOREACH(n, inttree, &head) {
        printf("%d\t", n->i);
    }

    printf("====================RB_NFIND=========================\n");
    {
    struct node theNode;
    theNode.i = 28;
    n = RB_NFIND(inttree, &head, &theNode);
    printf("%d\n", n->i);
    }

    printf("====================RB_FIND=========================\n");
    {
    struct node theNode;
    theNode.i = 20;
    n = RB_FIND(inttree, &head, &theNode);
    printf("%d\n", n->i);
    }

    printf("====================RB_REMOVE=========================\n");
    {
        struct node theNode;
        theNode.i = 20;
        n = RB_FIND(inttree, &head, &theNode);
        printf("find %d first\n", n->i);
        n = RB_REMOVE(inttree, &head, n);
        printf("remove %d success\n", n->i);
    }

    printf("====================RB_FOREACH_REVERSE=========================\n");
    RB_FOREACH_REVERSE(n, inttree, &head) {
        printf("%d\t", n->i);
    }
    printf("\n");

    getchar();
    return (0);
}

程序運行結(jié)果

紅黑樹運行結(jié)果

可以注意以下幾點

  1. 數(shù)據(jù)結(jié)構(gòu)的定義
    使用RB_ENTRY插入了樹的數(shù)據(jù)結(jié)構(gòu),而自己的數(shù)據(jù)可以任意定義
struct node {
    RB_ENTRY(node) entry;
    int i;
};
  1. 比較函數(shù)
    intcmp實現(xiàn)了整數(shù)的比較,紅黑樹可以用來排序,可以按優(yōu)先級取出數(shù)據(jù),比隊列的查找速度快。libuv中的timer和signal都使用了rbt。

  2. RB_GENERATE產(chǎn)生代碼
    由于c語言沒有模板,也不是面向?qū)ο螅膊皇侨躅愋偷模酝ㄟ^宏生成各個不同名字的紅黑樹代碼是非常巧妙的,實際上和cpp的模板是一個效果啊。不過用宏來展開代碼沒法用斷點調(diào)試,我想作者是先寫好測試用例,或者通過打印來調(diào)試,最后沒問題在轉(zhuǎn)成宏的吧。另外,這種方式導(dǎo)致生成的代碼比較多,和模板的缺點是一樣的。

  3. 宏的技巧
    這里的宏都需要傳入名字,使用了字符串拼接的技術(shù):比如RB_ENTRY(node) entry;

紅黑樹插入刪除算法

由于算法確實比較復(fù)雜,以前研究過幾次,現(xiàn)在都記不清楚了,說明記住算法的步奏確實是沒有必要的,如果想研究算法,確認他的效率和正確性,有興趣的可以去看看這篇文章,我覺得講的還是很清楚的。https://zh.wikipedia.org/wiki/紅黑樹

另外《算法導(dǎo)論》中也對紅黑樹講的比較多。

疑問

對源碼中一個無用的宏RB_AUGMENT感覺很奇怪,不知道干什么的。有知道的同學(xué)留言啊。

#ifndef RB_AUGMENT
#define RB_AUGMENT(x)  do {} while (0)
#endif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容