Python中C++中的红/黑树
pyredblack的Python项目详细描述
版权所有(c)2015 Will Roberts<;wildwilhelm@gmail.com>
在麻省理工学院许可下获得许可(有关详细信息,请参见LICENSE.rst)。
Cython与红黑树的接口,用C++实现。
Red-black trees是一种self-balancing binary tree。他们 按顺序维护它们的条目,并为 插入、查找和删除。你可以read more about red-black trees和see animations of insertion, lookup, and deletion
此包提供字典并基于 红黑树;可以作为 内置的dict和set类型,除了它们维护 按顺序排列的内容。
字典(rbdict):
>>> import pyredblack >>> d = pyredblack.rbdict(Germany = 'Berlin', Hungary = 'Budapest', Ireland = 'Dublin', Portugal = 'Lisbon', Cyprus = 'Nicosia', Greenland = 'Nuuk', Iceland = 'Reykjavik', Macedonia = 'Skopje', Bulgaria = 'Sofia', Sweden = 'Stockholm') >>> len(d) 10 >>> d['Ireland'] 'Dublin' >>> d.keys() ['Bulgaria', 'Cyprus', 'Germany', 'Greenland', 'Hungary', 'Iceland', 'Ireland', 'Macedonia', 'Portugal', 'Sweden'] >>> d.values() ['Sofia', 'Nicosia', 'Berlin', 'Nuuk', 'Budapest', 'Reykjavik', 'Dublin', 'Skopje', 'Lisbon', 'Stockholm'] >>> d.popitem() ('Bulgaria', 'Sofia') >>> d.popitem() ('Cyprus', 'Nicosia') >>> d.popitem() ('Germany', 'Berlin')
设置(rbset):
>>> fruit = pyredblack.rbset(['apple', 'orange', 'apple', 'pear', 'orange', 'banana']) >>> 'orange' in fruit True >>> 'crabgrass' in fruit False >>> a = pyredblack.rbset('abracadabra') >>> b = pyredblack.rbset('alacazam') >>> list(a) ['a', 'b', 'c', 'd', 'r'] >>> list(a - b) ['b', 'd', 'r'] >>> list(a | b) ['a', 'b', 'c', 'd', 'l', 'm', 'r', 'z'] >>> list(a & b) ['a', 'c'] >>> list(a ^ b) ['b', 'd', 'l', 'm', 'r', 'z'] >>> a.pop() 'a' >>> a.pop() 'b' >>> a.pop() 'c'
要求
- Python2.7,Python3.2+
- Cython(和C++编译器)
待办事项
- 在字典上实现切片