spdict:红黑树(RedBlackTree),平衡树(BalancedTree),SkipList 的实现

对着 MIT 的 [url=http://mitpress.mit.edu/algorithms/]《Introduction to Algorithms, Second Edition 》[/url] 看了一段时间,对里面的提到的几种字典数据结构算法很感兴趣,因此照着书上的描述做了一些实现。使用 C++ 实现了:BinarySearchTree, RedBlackTree, [url=http://www.bluerwhite.org/btree/]BalancedTree[/url], [url=http://www.csee.umbc.edu/courses/undergraduate/341/fall01/Lectures/SkipLists/skip_lists/skip_lists.html]SkipList[/url], SortedArray 。

源代码下载:
[url=http://freshmeat.net/redir/spdict/69457/url_tgz/spdict-0.2.src.tar.gz]http://spdict.googlecode.com/files/spdict-0.2.src.tar.gz[/url]
[url]http://code.google.com/p/spdict/downloads/list[/url]

只实现了字典的 3 个基本操作:search,insert,remove 。
[code]class SP_Dictionary {
public:
virtual ~SP_Dictionary();
virtual int insert( void * item ) = 0;
virtual const void * search( const void * key ) const = 0;
virtual void * remove( const void * key ) = 0;
virtual int getCount() const = 0;
virtual SP_DictIterator * getIterator() const = 0;
};
[/code]

另外还针对各种字典数据结构实现了 iterator 。
[code]class SP_DictIterator {
public:
virtual ~SP_DictIterator();
virtual const void * getNext( int * level = 0 ) = 0;
};
[/code]

在 version 0.2 中基于 SP_Dictionary 接口实现了一个 cache 类。
[code]class SP_Cache {
public:
virtual ~SP_Cache();
virtual int put( void * item, time_t expTime = 0 ) = 0;
virtual int get( const void * key, void * resultHolder ) = 0;
virtual int erase( const void * key ) = 0;
virtual void * remove( const void * key, time_t * expTime = 0 ) = 0;
virtual SP_CacheStatistics * getStatistics() = 0;
};
[/code]

程序包里面有一个测试程序,采用随机生成测试数据的方法,对各种不同的字典数据结构进行测试,可以方便地对比不同算法各种操作的性能。命令行的使用方法如下:

[code]bash-2.05a$ ./testdict -v
./testdict [-t type] [-c count]
-t type :
bst ( brinary search tree )
rb ( red-black tree )
bt ( balanced tree )
sl ( skip list )
sa ( sorted array )
-c count, test how many items
[/code]

程序在 linux 下开发,在 solaris 和 windows 平台也进行过测试。
linux/solaris 使用 Makefile 进行构建,windows 有相关的 vc++ 的工程文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值