首页 | 资讯动态 | linux基础 | 系统管理 | 网络管理 | 编程开发 | linux数据库 | 服务器技术 | linux相关 | linux认证 | 嵌入式 | 下载中心 | 专题 | linux招聘 | 镜像站
OKLinux中文技术站
·设为首页
·加入收藏
·联系我们
系统管理: 中文环境 系统管理 桌面应用 内核技术 | Linux基础: 基础入门 安装配置 常用命令 经验技巧 软件应用 | Linux数据库: Mysql Postgre Oracle DB2 Sybase other
网络管理: 网络安全 网络应用 Linux服务器 环境配置 黑客安全 | 编程开发: PHP CC++ Python Perl Shell 嵌入式开发 java jsp | PHP技术: PHP基础 PHP技巧 PHP应用 PHP文摘
搜索中心 Linux招聘 Linux专题 Apache | Linux相关: 硬件相关 Linux解决方案 Linux认证 企业应用 其它Unix | 相关下载: 资料下载 参考手册 开发工具 服务器类 软路由 其它
 技术搜索:
会员中心 注册会员 高级搜索  
  → 当前位置:首页>编程开发>其他编程>正文

Linux内核中的红黑树的使用

http://www.oklinux.cn  2008-09-23  linuxidc   会员收藏  游客收藏  【 】 
您查看的文章来源于http://www.oklinux.cn

最近需要使用红黑树,在网上查找资料的时候无意中发现linux内核中有个红黑树的实现,并且其代码非常的独立,现把它摘录出来。我摘录自2.6.24的内核,分为两个文件rbtree.h和rbtree.c,rbtree.h位于内核源码的include/linux目录中,rbtree.c位于内核源码的lib目录中。

rbtree.h中删除#include 和#include 两行,添加#include

对于rb_node的声明删除掉最后的__attribute__((aligned(sizeof(long))))

最后在rbtree.h中添加如下一些宏定义:

#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})


在使用rbtree的时候,每个rbtree的元素都由使用者自己定义如:

struct my_item
{
struct rb_node node;
struct my_data_struct data;
};


其中的struct my_data_struct是自己的数据区域

struct my_data_struct
{
long key;
long value;
};


下面需要实现两个函数,第一个是一个search函数,形式如下:

struct my_item* _my_rbtree_search (struct rb_root* root, long key);


其中的root参数是根节点指针;key是关键字,rbtree根据它来排序以及查找。

该函数的实现如下:

struct my_item*
_my_rbtree_search (struct rb_root* root, long key)
{
struct rb_node *node;
struct my_item* item;
node = root->rb_node;

while (node)
{
item = rb_entry (node, struct my_item, node);
if (item->data.key > key)
node = node->rb_left;
else if (item->data.key < key)
node = node->rb_right;
else
{
return item; /* found it */
}
}
return NULL;
}


下面还需要实现一个插入的函数:

void
_my_rbtree_insert (struct rb_root* root, struct my_item* item)
{
struct rb_node **link, *parent;
long value;
struct my_item* p_item;
link = &(root->rb_node);
value = item->data.key;
/* Go to the bottom of the tree */
while (*link)
{
parent = *link;
p_item = rb_entry(parent, struct my_item, node);
if (p_item->data.key > value)
link = &((*link)->rb_left);
else if (p_item->data.key < value)
link = &((*link)->rb_right);
else
return;
}
/* Put the new node there */
rb_link_node(&(item->node), parent, link);
rb_insert_color(&(item->node), root);
}


拥有了以上两个函数我们就可以实现search、insert、remove、modify、traverse等操作,下面给出大概的伪代码吧

struct my_data_struct search (struct rb_root* root, long key)
{
struct my_data_struct data;
struct my_item *item;
memset (&data, 0, sizeof(struct my_data));
item = _my_rbtree_search (root, key);
if (NULL != item)
{
memcpy (&data, item->data, sizeof(struct my_data_struct));
}
return data;
}

int insert (struct rb_root* root, long key, long value)
{
struct my_item *item = (struct my_item*) malloc (sizeof(struct my_item));
if (NULL == item)
{
return -1;
}
item->data.key = key;
item->data.value = value;
_my_rbtree_insert (root, item);
return 0;
}

int remove (struct rb_root* root, long key)
{
struct my_item* item;
item = _my_rbtree_search (root, key);
if (NULL != item)
{
rb_erase (&(item->node), root);
}
return 0;
}

int modify (struct rb_root* root, long key, long new_value)
{
struct my_item* item;
item = _my_rbtree_search (root, key);
if (NULL != item)
{
item->data.value = new_value;
}
return 0;
}

int traverse (struct rb_root* root)
{
struct rb_node* node;
struct my_item* item;
for (node = rb_first (root); node; node = rb_next (node))
{
item = rb_entry (node, struct my_item, node);
printf ("key:%d\tvalue:%d\n", item->data.key, item->data.value);
}
return 0;
}


上一篇:Linux下安装访问SQL SERVER2000数据库(附文件下载)   下一篇:Linux 无法执行SHELL命令的解决

收藏于收藏夹】 【评论】 【推荐】 【打印】 【关闭
相关文档
·Linux 无法执行SHELL命令的解决
·Linux的多个GCC版本
·Linux AS4下安装VNC4.0
·Linux下kdevelop
·Linux对一个3G的文本进行编码转换全过程
·菜鸟课堂 教你打造个人无敌系统全攻略
·把Linux下nand读操作搞定了
·Linux\Unix 系统编程 -- 关于缓冲设置时容易出现的错
·Linux\Unix 系统编程 -- 等待某个子进程结束的wait方
·不安装Linux也可学习Linux命令的方法
·Linux添加路径到PATH
·Linux Shell位置参数
·关于Linux fork()函数的工作机制
·Linux内存泄漏的检查方法
·初识Linux脚本编程(shell)
·Linux shell 检查进程PID
发表评论
密码: 匿名评论
评论内容:

(不超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)
 
  最新文档
·Linux对一个3G的文本进行编码转换全过
·Linux下kdevelop
·Linux AS4下安装VNC4.0
·Linux的多个GCC版本
·Linux 无法执行SHELL命令的解决
·菜鸟课堂 教你打造个人无敌系统全攻略
·把Linux下nand读操作搞定了
·Linux\Unix 系统编程 -- 关于缓冲设置
·Linux\Unix 系统编程 -- 等待某个子进
·不安装Linux也可学习Linux命令的方法
·Linux添加路径到PATH
·Linux Shell位置参数
  阅读排行
·Linux下Qtopia Core 4.3(QT/E)交叉编译
·开源空间 网络安全工具开发函数库Libne
·Linux编程时获取当前时间实例解析
·Linux环境下OpenGL编程学习
·Linux socket编程实例:echo服务器程序
·升级Redhat Linux 9.0内核有感
·Linux中断处理学习笔记
·Linux环境下重新编译GCC-4.3.0
·GNU/Linux应用程序编程:用管道进行编
·Linux系统中限制用户进程CPU及内存占用
·Linux下安装g77 fortran complier过程
·解决Linux中Matlab中文乱码问题
·Linux多线程编程学习之线程同步
·Linux环境下Wine的中文显示以及freetyp
·如何在Ubuntu 7.0上实现C/C++开发环境
网摘收藏: