您的位置:首页 > 理论基础 > 数据结构算法

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

2007-05-07 02:04 741 查看

首发在:http://iunknown.javaeye.com/blog/75459

红黑树(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 。

代码

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;  

};  

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

代码

class SP_DictIterator {  

public:  

        virtual ~SP_DictIterator();  

        virtual const void * getNext( int * level = 0 ) = 0;  

};  

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

代码

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  

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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息