Linux红黑树(二)——访问节点

本文介绍了Linux红黑树的使用,强调了需要自我封装插入和查询核心代码以提高性能。红黑树的每个struct rb_node实例嵌入在数据结构中,提供基础API,用户自定义搜索和插入函数。此外,文章详细讲解了如何访问和遍历树节点,包括获取第一个和最后一个有序节点、后继节点和前驱节点的方法。
摘要由CSDN通过智能技术生成

核心对红黑树使用两点说明

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代码的用户自己管理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值