免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: Godbach
打印 上一主题 下一主题

Linux内核中的红黑树 [复制链接]

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
41 [报告]
发表于 2009-01-04 10:06 |只看该作者
原帖由 lbaby 于 2009-1-4 10:05 发表
http://lxr.linux.no/linux+v2.6.28/include/linux/hash.h

另外内核里也有不错的 hash 实现,Godbach 兄可以去看看


多谢lbaby兄指点啊。。

论坛徽章:
0
42 [报告]
发表于 2009-01-04 10:21 |只看该作者
好文章,收藏。

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
43 [报告]
发表于 2009-01-05 22:28 |只看该作者
static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
                                struct rb_node ** rb_link)
node为当前要插入的节点;
parent为node的父节点;
rb_link为node节点要插入的位置,即parent->left 或parent->right。这个可有前面在查找过程中确定。


如果rb_link_node如我解释的一样,那么也就是调用该函数的时候应该保证tree至少有一个节点,也就是根节点存在。不然的话,parent本身就为空,无法执行link

论坛徽章:
0
44 [报告]
发表于 2009-01-11 00:35 |只看该作者
看了,但是“知其然而不知其所以然”,版主大哥,。。

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
45 [报告]
发表于 2009-01-11 12:31 |只看该作者
原帖由 maxxfire 于 2009-1-11 00:35 发表
看了,但是“知其然而不知其所以然”,版主大哥,。。


其实这里讲的基本上就是《算法导论》中讲的红黑树的实现啊。如果不明白这个,是不是本身对红黑树不太了解啊?

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
46 [报告]
发表于 2009-01-11 20:31 |只看该作者

回复 #45 Godbach 的帖子

算法导论的中文版,红黑树那一章讲得有点云里雾里的,呵呵。可能英文版讲得好一点吧

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
47 [报告]
发表于 2009-01-11 21:46 |只看该作者
原帖由 dreamice 于 2009-1-11 20:31 发表
算法导论的中文版,红黑树那一章讲得有点云里雾里的,呵呵。可能英文版讲得好一点吧


呵呵,insert部分大致明白了。erase比较复杂。

论坛徽章:
0
48 [报告]
发表于 2009-01-16 22:49 |只看该作者

论坛徽章:
1
CU十二周年纪念徽章
日期:2013-10-24 15:41:34
49 [报告]
发表于 2009-01-17 19:41 |只看该作者
很好,顶一下,学习!!

论坛徽章:
0
50 [报告]
发表于 2009-01-17 22:58 |只看该作者
这两天在看内核中的文档,里面也有很详细的讲解。
Documentation/rbtree.txt

如下:

Red-black Trees (rbtree) in Linux
January 18, 2007
Rob Landley <rob@landley.net>
=============================

What are red-black trees, and what are they for?
------------------------------------------------

Red-black trees are a type of self-balancing binary search tree, used for
storing sortable key/value data pairs.  This differs from radix trees (which
are used to efficiently store sparse arrays and thus use long integer indexes
to insert/access/delete nodes) and hash tables (which are not kept sorted to
be easily traversed in order, and must be tuned for a specific size and
hash function where rbtrees scale gracefully storing arbitrary keys).

Red-black trees are similar to AVL trees, but provide faster real-time bounded
worst case performance for insertion and deletion (at most two rotations and
three rotations, respectively, to balance the tree), with slightly slower
(but still O(log n)) lookup time.

To quote Linux Weekly News:

    There are a number of red-black trees in use in the kernel.
    The anticipatory, deadline, and CFQ I/O schedulers all employ
    rbtrees to track requests; the packet CD/DVD driver does the same.
    The high-resolution timer code uses an rbtree to organize outstanding
    timer requests.  The ext3 filesystem tracks directory entries in a
    red-black tree.  Virtual memory areas (VMAs) are tracked with red-black
    trees, as are epoll file descriptors, cryptographic keys, and network
    packets in the "hierarchical token bucket" scheduler.

This document covers use of the Linux rbtree implementation.  For more
information on the nature and implementation of Red Black Trees,  see:

  Linux Weekly News article on red-black trees
    http://lwn.net/Articles/184495/

  Wikipedia entry on red-black trees
    http://en.wikipedia.org/wiki/Red-black_tree

Linux implementation of red-black trees
---------------------------------------

Linux's rbtree implementation lives in the file "lib/rbtree.c".  To use it,
"#include <linux/rbtree.h>".

The Linux rbtree implementation is optimized for speed, and thus has one
less layer of indirection (and better cache locality) than more traditional
tree implementations.  Instead of using pointers to separate rb_node and data
structures, each instance of struct rb_node is embedded in the data structure
it organizes.  And instead of using a comparison callback function pointer,
users are expected to write their own tree search and insert functions
which call the provided rbtree functions.  Locking is also left up to the
user of the rbtree code.

Creating a new rbtree
---------------------

Data nodes in an rbtree tree are structures containing a struct rb_node member:

  1.   struct mytype {
  2.           struct rb_node node;
  3.           char *keystring;
  4.   };
复制代码

When dealing with a pointer to the embedded struct rb_node, the containing data
structure may be accessed with the standard container_of() macro.  In addition,
individual members may be accessed directly via rb_entry(node, type, member).

At the root of each rbtree is an rb_root structure, which is initialized to be
empty via:

  1.   struct rb_root mytree = RB_ROOT;
复制代码

Searching for a value in an rbtree
----------------------------------

Writing a search function for your tree is fairly straightforward: start at the
root, compare each value, and follow the left or right branch as necessary.

Example:

  1.   struct mytype *my_search(struct rb_root *root, char *string)
  2.   {
  3.           struct rb_node *node = root->rb_node;

  4.           while (node) {
  5.                   struct mytype *data = container_of(node, struct mytype, node);
  6.                 int result;

  7.                 result = strcmp(string, data->keystring);

  8.                 if (result < 0)
  9.                           node = node->rb_left;
  10.                 else if (result > 0)
  11.                           node = node->rb_right;
  12.                 else
  13.                           return data;
  14.         }
  15.         return NULL;
  16.   }
复制代码

Inserting data into an rbtree
-----------------------------

Inserting data in the tree involves first searching for the place to insert the
new node, then inserting the node and rebalancing ("recoloring") the tree.

The search for insertion differs from the previous search by finding the
location of the pointer on which to graft the new node.  The new node also
needs a link to its parent node for rebalancing purposes.

Example:

  1.   int my_insert(struct rb_root *root, struct mytype *data)
  2.   {
  3.           struct rb_node **new = &(root->rb_node), *parent = NULL;

  4.           /* Figure out where to put new node */
  5.           while (*new) {
  6.                   struct mytype *this = container_of(*new, struct mytype, node);
  7.                   int result = strcmp(data->keystring, this->keystring);

  8.                 parent = *new;
  9.                   if (result < 0)
  10.                           new = &((*new)->rb_left);
  11.                   else if (result > 0)
  12.                           new = &((*new)->rb_right);
  13.                   else
  14.                           return FALSE;
  15.           }

  16.           /* Add new node and rebalance tree. */
  17.           rb_link_node(data->node, parent, new);
  18.           rb_insert_color(data->node, root);

  19.         return TRUE;
  20.   }
复制代码

Removing or replacing existing data in an rbtree
------------------------------------------------

To remove an existing node from a tree, call:

  1.   void rb_erase(struct rb_node *victim, struct rb_root *tree);
复制代码

Example:

  1.   struct mytype *data = mysearch(mytree, "walrus");

  2.   if (data) {
  3.           rb_erase(data->node, mytree);
  4.           myfree(data);
  5.   }
复制代码

To replace an existing node in a tree with a new one with the same key, call:

  1.   void rb_replace_node(struct rb_node *old, struct rb_node *new,
  2.                           struct rb_root *tree);
复制代码

Replacing a node this way does not re-sort the tree: If the new node doesn't
have the same key as the old node, the rbtree will probably become corrupted.

Iterating through the elements stored in an rbtree (in sort order)
------------------------------------------------------------------

Four functions are provided for iterating through an rbtree's contents in
sorted order.  These work on arbitrary trees, and should not need to be
modified or wrapped (except for locking purposes):

  1.   struct rb_node *rb_first(struct rb_root *tree);
  2.   struct rb_node *rb_last(struct rb_root *tree);
  3.   struct rb_node *rb_next(struct rb_node *node);
  4.   struct rb_node *rb_prev(struct rb_node *node);
复制代码

To start iterating, call rb_first() or rb_last() with a pointer to the root
of the tree, which will return a pointer to the node structure contained in
the first or last element in the tree.  To continue, fetch the next or previous
node by calling rb_next() or rb_prev() on the current node.  This will return
NULL when there are no more nodes left.

The iterator functions return a pointer to the embedded struct rb_node, from
which the containing data structure may be accessed with the container_of()
macro, and individual members may be accessed directly via
rb_entry(node, type, member).

Example:

  1.   struct rb_node *node;
  2.   for (node = rb_first(&mytree); node; node = rb_next(node))
  3.           printk("key=%s\n", rb_entry(node, int, keystring));

复制代码

[ 本帖最后由 scutan 于 2009-1-17 23:06 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP