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

关键字: 数据结构和算法   dictionary btree rbtree binary search    

对着 MIT 的 《Introduction to Algorithms, Second Edition 》 看了一段时间,对里面的提到的几种字典数据结构算法很感兴趣,因此照着书上的描述做了一些实现。使用 C++ 实现了:BinarySearchTree, RedBlackTree, BalancedTree, SkipList, SortedArray 。

源代码下载:
http://spdict.googlecode.com/files/spdict-0.1.src.tar.gz
http://code.google.com/p/spdict/downloads/list

只实现了字典的 3 个基本操作:search,insert,remove 。

代码
  1. class SP_Dictionary {  
  2. public:  
  3.         virtual ~SP_Dictionary();  
  4.         virtual int insert( void * item ) = 0;  
  5.         virtual const void * search( const void * key ) const = 0;  
  6.         virtual void * remove( const void * key ) = 0;  
  7.         virtual int getCount() const = 0;  
  8.         virtual SP_DictIterator * getIterator() const = 0;  
  9. };  
<script type="text/javascript">render_code();</script>

另外还针对各种字典数据结构实现了 iterator 。

代码
  1. class SP_DictIterator {  
  2. public:  
  3.         virtual ~SP_DictIterator();  
  4.         virtual const void * getNext( int * level = 0 ) = 0;  
  5. };  
<script type="text/javascript">render_code();</script>

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

代码
  1. bash-2.05a$ ./testdict -v  
  2. ./testdict [-t type] [-c count]  
  3.         -t type :  
  4.                  bst ( brinary search tree )  
  5.                  rb ( red-black tree )  
  6.                  bt ( balanced tree )  
  7.                  sl ( skip list )  
  8.                  sa ( sorted array )  
  9.         -c count, test how many items  
<script type="text/javascript">render_code();</script>

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

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值