Linux红黑树(二)——访问节点
核心对红黑树使用两点说明
1、头文件
<Documentation/rbtree.txt>
Linux's rbtree implementation lives in the file "lib/rbtree.c". To use it,
"#include <linux/rbtree.h>".
2、自我封装
<linux/rbtree.h>
To use rbtrees you'll have to implementyour own insert and search cores. This will avoid us to use callbacks and todrop drammatically performances. I know it's not the cleaner way, but in C (not in C++) to get performances andgenericity...
【这里中心思想: 在使用rbtree时候,你需要自己实现你的插入和查询的核心代码,即使用核心API,自我封装。这可以避免使用回调函数和性能上的损失。(由于键值不同,调用用户定义的函数,就需要采用回调的形式;显然这里没有使用)】
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.
【在Linux rbtree实现了速度优化,具有比传统树的实现要少一个间接层(和更好的缓存)。每个struct rb_node实例是嵌入在数据组织成的结构体中,以此代替使用指针来区分rb_node和数据结构。并且相比采用回调函数的指针形式,用户可以通过调用rbtree提供的函数,写自己的树搜索和插入函数。锁也留给使用rbtree代码的用户自己管理。】
以上两大段,读懂还是读不懂都无关紧要!因为你只需要明白2点即可:1、struct rb_node实例是嵌入在用户数据结构体当中的,这是和链表一样的通用做法;2、rbtree提供最基础的操作API,不进行封装,用户自己管理操作红黑树的代码
1、访问元素(包含数据的结构体)
<Documentation/rbtree.txt>
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).
<linux/rbtree.h>
#define rb_entry(ptr, type, member) container_of(ptr, type, member)
2、查找树元素
<Documentation/rbtree.txt>
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: struct mytype *my_search(struct rb_root *root, char *string)
{
struct rb_node *node = root->rb_node; while (node) {
struct mytype *data = container_of(node, struct mytype, node);
int result; result = strcmp(string, data->keystring); if (result < 0)
node = node->rb_left;
else if (result > 0)
node = node->rb_right;
else
return data;
}
return NULL;
}
由于红黑树也是二叉查找树,因此对树元素的查找和二叉查找树的查找算法一致。
- 若b是空树,则搜索失败,否则转向2
- 若x等于b的根节点的数据域之值,则查找成功;否则转向3
- 若x小于b的根节点的数据域之值,则搜索左子树,转向1;否则转向4
- 查找右子树,转向1
参考 维基二叉查找树
3、树节点的访问
对二叉查找树"中序"遍历可以得到键值有序的序列。内核提供了四个有序访问红黑树节点函数,它们只工作在二叉树情况下,不供其他多叉树使用。一般不需要重新和修改,只有在需要加锁的情况下,才允许这么做。
3.1、获取树的第一个有序节点
树根左子树的最左边的节点
<lib/rbtree.c>
/*
* This function returns the first node (in sort order) of the tree.
*/
struct rb_node *rb_first(const struct rb_root *root)
{
struct rb_node *n; n = root->rb_node;
if (!n)//树空
return NULL;
while (n->rb_left)//树最左端
n = n->rb_left;
return n;
}
EXPORT_SYMBOL(rb_first);
3.2、获取树的最后一个有序节点
树根右子树最右边的节点
<lib/rbtree.c>
struct rb_node *rb_last(const struct rb_root *root)
{
struct rb_node *n; n = root->rb_node;
if (!n)//树空
return NULL;
while (n->rb_right)//树最右端
n = n->rb_right;
return n;
}
EXPORT_SYMBOL(rb_last);
3.3、获取当前节点的有序后继节点
当前节点的右孩子,或者右子树最左端的节点,或者祖先节点
<lib/rbtree.c>
struct rb_node *rb_next(const struct rb_node *node)
{
struct rb_node *parent; if (RB_EMPTY_NODE(node))
return NULL; /*
* If we have a right-hand child, go down and then left as far
* as we can.
*/
if (node->rb_right) {
node = node->rb_right;
while (node->rb_left)
node=node->rb_left;
return (struct rb_node *)node;
} /*
* No right-hand children. Everything down and left is smaller than us,
* so any 'next' node must be in the general direction of our parent.
* Go up the tree; any time the ancestor is a right-hand child of its
* parent, keep going up. First time it's a left-hand child of its
* parent, said parent is our 'next' node.
*/
while ((parent = rb_parent(node)) && node == parent->rb_right)
node = parent; return parent;
}
EXPORT_SYMBOL(rb_next);
注:最低端叶子节点省略了,而且其他分支没有画出,即图示不是完整的红黑树,只是拿树中一部分来说明!
3.4、获取当前节点的有序前驱节点
当前节点的左孩子,或者左子树的最右端节点,或者它的祖先节点
<lib/rbtree.c>
struct rb_node *rb_prev(const struct rb_node *node)
{
struct rb_node *parent; if (RB_EMPTY_NODE(node))
return NULL; /*
* If we have a left-hand child, go down and then right as far
* as we can.
*/
if (node->rb_left) {
node = node->rb_left;
while (node->rb_right)
node=node->rb_right;
return (struct rb_node *)node;
} /*
* No left-hand children. Go up till we find an ancestor which
* is a right-hand child of its parent.
*/
while ((parent = rb_parent(node)) && node == parent->rb_left)
node = parent; return parent;
}
EXPORT_SYMBOL(rb_prev);
4、有序遍历树节点
<Documentation/rbtree.txt>
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: struct rb_node *node;
for (node = rb_first(&mytree); node; node = rb_next(node))
printk("key=%s\n", rb_entry(node, struct mytype, node)->keystring);
声明:
>> 知识要传播,劳动要尊重! 受益于开源,回馈于社会! 大家共参与,服务全人类!
>> 本博文由my_live_123原创(http://blog.csdn.net/cwcmcw),转载请注明出处! ^_^
Linux红黑树(二)——访问节点的更多相关文章
- 红黑树(二)之 C语言的实现
概要 红黑树在日常的使用中比较常用,例如Java的TreeMap和TreeSet,C++的STL,以及Linux内核中都有用到.之前写过一篇文章专门介绍红黑树的理论知识,本文将给出红黑数的C语言的实现 ...
- Linux红黑树(一)——数据结构
摘要 兹博文探讨四个重点:1.简单介绍红黑树:2.红黑树节点数据结构:3.红黑树节点中父节点指针域和自身节点颜色有机结合:4.定义红黑树和操作树节点父节点指针和节点颜色的接口,包括一系列宏和两个函数. ...
- 安排:《蚂蚁花呗1234面:Redis+分布式架构+MySQL+linux+红黑树》
前言: 大厂面试机会难得,为了提高面试通关率,建议朋友们在面试前先复盘自己的知识栈,依据掌握程度划分重要.优先级,系统地去学习!如果不准备充分就去参加面试,既会失去进入大厂的机会,更是对自己的不负责. ...
- 红黑树(三)之 Linux内核中红黑树的经典实现
概要 前面分别介绍了红黑树的理论知识 以及 通过C语言实现了红黑树.本章继续会红黑树进行介绍,下面将Linux 内核中的红黑树单独移植出来进行测试验证.若读者对红黑树的理论知识不熟悉,建立先学习红黑树 ...
- 红黑树之 原理和算法详细介绍(阿里面试-treemap使用了红黑树) 红黑树的时间复杂度是O(lgn) 高度<=2log(n+1)1、X节点左旋-将X右边的子节点变成 父节点 2、X节点右旋-将X左边的子节点变成父节点
红黑树插入删除 具体参考:红黑树原理以及插入.删除算法 附图例说明 (阿里的高德一直追着问) 或者插入的情况参考:红黑树原理以及插入.删除算法 附图例说明 红黑树与AVL树 红黑树 的时间复杂度 ...
- JDK8 HashMap--getTreeNode()获取红黑树指定key的节点
/*获取红黑树的指定节点*/ final TreeNode<K,V> getTreeNode(int h, Object k) { return ((parent != null) ? r ...
- 浅谈AVL树,红黑树,B树,B+树原理及应用(转)
出自:https://blog.csdn.net/whoamiyang/article/details/51926985 背景:这几天在看<高性能Mysql>,在看到创建高性能的索引,书上 ...
- 浅谈AVL树,红黑树,B树,B+树原理及应用
背景:这几天在看<高性能Mysql>,在看到创建高性能的索引,书上说mysql的存储引擎InnoDB采用的索引类型是B+Tree,那么,大家有没有产生这样一个疑问,对于数据索引,为什么要使 ...
- 通过分析 JDK 源代码研究 TreeMap 红黑树算法实现
本文转载自http://www.ibm.com/developerworks/cn/java/j-lo-tree/ 目录: TreeSet 和 TreeMap 的关系 TreeMap 的添加节点 Tr ...
随机推荐
- Webservice-WSDL详解(三)
怎样向别人介绍WS的功能呢?一般咱们会写接口文档,亦或口头告诉使用的人.这些方式都存在问题:其中一个我上篇中说过,客户端是无法直接使用服务端接口的:二是程序员在电脑前,想使用WS时,他们的工具(如Ec ...
- MySQL教程及经常使用命令1.1
在线教程 21分钟 MySQL 新手教程 w3school在线教程(MYSQL) 变量 查看系统变量 show global variables 查看详细变量 show global variable ...
- Android中文API(129) —— AudioManager
前言 本章内容是android.media.AudioManager,版本为Android 3.2 r1,翻译来自"文炜",欢迎访问他的博客:"http://www.cn ...
- net异步编程之await
net异步编程之await 初探asp.net异步编程之await 终于毕业了,也顺利进入一家期望的旅游互联网公司.27号入职.放肆了一个多月没写代码,好方啊. 另外一下观点均主要针对于await ...
- 基于visual Studio2013解决C语言竞赛题之1008整除数
题目 解决代码及点评 /************************************************************************/ ...
- uva 10313 Pay the Price(完全背包)
题目连接:10313 - Pay the Price 题目大意:有0~300这300种价值的金额. 现在可能给出参数: 1个:n, 输出可以组成价值n的方式的个数. 2个:n, a输出用个数小于a的价 ...
- μC/OS学习资料(附Ebook)
注意:下载地址位于文末. μC/OS-各版本源码 <嵌入式实时操作系统μC/OS-II> <嵌入式实时操作系统μC/OS-III> <μC/OSII2.52源码中文译注- ...
- 使用【百度云推送】第三方SDK实现推送功能具体解释
之前介绍过怎样使用shareSDK实现新浪微博分享功能,今天介绍怎样使用百度云推送SDK实现Android手机后台推送功能. 执行效果例如以下 第一步,假设使用百度的SDK,当然要先成为百度的开发人员 ...
- C# - 使用皮肤
运行效果: 项目目录结构: 主窗体代码: using System; using System.Collections.Generic; using System.ComponentModel; us ...
- Win7+花生壳6.0+tomcat打做自己的web服务器(搭建自己的网站)(参考)
链接地址:http://blog.csdn.net/zhu_9527/article/details/23344623?utm_source=tuicool&utm_medium=referr ...