python setup.py install
>>> import bintrees
>>> from bintrees import BinaryTree #只引入了BinartTree >>> from bintrees import * #三个模块都引入了
>>> btree = BinaryTree() >>> btree BinaryTree({}) >>> type(btree) <class 'bintrees.bintree.BinaryTree'>
>>> btree.__setitem__("Tom","headmaster") >>> btree BinaryTree({'Tom': 'headmaster'}) >>> btree.__setitem__("blog","http://blog.csdn.net/qiwsir") >>> btree BinaryTree({'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'})
>>> adict = [(2,"phone"),(5,"tea"),(9,"scree"),(7,"computer")] >>> btree.update(adict) >>> btree BinaryTree({2: 'phone', 5: 'tea', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'})
>>> btree BinaryTree({2: 'phone', 5: 'tea', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'}) >>> btree.__contains__(5) True >>> btree.__contains__("blog") True >>> btree.__contains__("qiwsir") False >>> btree.__contains__(1) False
>>> btree BinaryTree({2: 'phone', 5: 'tea', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'}) >>> btree.__delitem__(5) #删除key=5的key-value,即:5:'tea' 被删除. >>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'})
>>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'}) >>> btree.__getitem__("blog") 'http://blog.csdn.net/qiwsir' >>> btree.__getitem__(7) 'computer' >>> btree._getitem__(5) #在btree中没有key=5,于是报错。 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'BinaryTree' object has no attribute '_getitem__'
>>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'}) >>> aiter = btree.__iter__() >>> aiter <generator object <genexpr> at 0xb7416dec> >>> aiter.next() #注意:next()一个之后,该值从list中删除 2 >>> aiter.next() 7 >>> list(aiter) [9, 'Tom', 'blog'] >>> list(aiter) #结果是空 [] >>> bool(aiter) #but,is True True
>>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'}) >>> btree.__len__() 5
>>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'}) >>> btree.__max__() (9, 'scree') >>> btree.__min__() (2, 'phone')
>>> other = [(3,'http://blog.csdn.net/qiwsir'),(7,'qiwsir')] >>> bother = BinaryTree() #再建一个树 >>> bother.update(other) #加入数据 >>> bother BinaryTree({3: 'http://blog.csdn.net/qiwsir', 7: 'qiwsir'}) >>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'}) >>> btree.__and__(bother) #重叠部分部分 BinaryTree({7: 'computer'}) >>> btree.__or__(bother) #全部 BinaryTree({2: 'phone', 3: 'http://blog.csdn.net/qiwsir', 7: 'computer', 9: 'scree'}) >>> btree.__sub__(bother) #btree不与bother重叠的部分 BinaryTree({2: 'phone', 9: 'scree'}) >>> btree.__xor__(bother) #两者非重叠部分 BinaryTree({2: 'phone', 3: 'http://blog.csdn.net/qiwsir', 9: 'scree'})
>>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'}) >>> btree.__repr__() "BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'})"
>>> bother BinaryTree({3: 'http://blog.csdn.net/qiwsir', 7: 'qiwsir'}) >>> bother.clear() >>> bother BinaryTree({}) >>> bool(bother) False
>>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'}) >>> ctree = btree.copy() >>> ctree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'}) >>> btree.__setitem__("github","qiwsir") #增加btree的数据 >>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'github': 'qiwsir'}) >>> ctree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'}) #这是不是在说明属于深拷贝呢? >>> ctree.__delitem__(7) #删除ctree的一个数据 >>> ctree BinaryTree({2: 'phone', 9: 'scree'}) >>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'github': 'qiwsir'})
>>> ctree BinaryTree({2: 'phone', 9: 'scree'}) >>> ctree.discard(2) #删除后,不返回值,或者返回None >>> ctree BinaryTree({9: 'scree'}) >>> ctree.discard(2) #如果删除的key不存在,也返回None >>> ctree.discard(3) >>> ctree.__delitem__(3) #但是,.__delitem__(key)则不同,如果key不存在,会报错。 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/site-packages/bintrees/abctree.py", line 264, in __delitem__ self.remove(key) File "/usr/local/lib/python2.7/site-packages/bintrees/bintree.py", line 124, in remove raise KeyError(str(key)) KeyError: '3'
>>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'github': 'qiwsir'}) >>> btree.get(2,"algorithm") 'phone' >>> btree.get("python","algorithm") #没有key='python'的值,返回'algorithm' 'algorithm' >>> btree.get("python") #如果不指定第二个参数,若查不到,则返回None >>>
>>> ctree BinaryTree({9: 'scree'}) >>> ctree.clear() #清空数据 >>> ctree BinaryTree({}) >>> ctree.is_empty() True >>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'github': 'qiwsir'}) >>> btree.is_empty() False
>>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'github': 'qiwsir'}) >>> for (k,v) in btree.items(): ... print k,v ... 2 phone 7 computer 9 scree github qiwsir >>> for k in btree.keys(): ... print k ... 2 7 9 github >>> for v in btree.values(): ... print v ... phone computer scree qiwsir >>> for (k,v) in btree.items(reverse=True): #反序 ... print k,v ... github qiwsir 9 scree 7 computer 2 phone >>> btree BinaryTree({2: 'phone', 5: None, 7: 'computer', 8: 'eight', 9: 'scree', 'github': 'qiwsir'}) >>> for (k,v) in btree.iter_items(6,9): #要求迭代6<=key<9的键值对数据 ... print k,v ... 7 computer 8 eight >>>
>>> ctree = btree.copy() >>> ctree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'github': 'qiwsir'}) >>> ctree.pop(2) #删除key=2的数据,返回其value 'phone' >>> ctree.pop(2) #删除一个不存在的key,报错 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/site-packages/bintrees/abctree.py", line 350, in pop value = self.get_value(key) File "/usr/local/lib/python2.7/site-packages/bintrees/abctree.py", line 557, in get_value raise KeyError(str(key)) KeyError: '2' >>> ctree.pop_item() #随机返回一个(key,value),并已删除之 (7, 'computer') >>> ctree BinaryTree({9: 'scree', 'github': 'qiwsir'}) >>> ctree.pop(7,"sing") #如果没有,可以返回指定值 'sing'
>>> btree BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'github': 'qiwsir'}) >>> btree.set_default(7) #存在则返回 'computer' >>> btree.set_default(8,"eight") #不存在,则返回后备指定值,并加入到树 'eight' >>> btree BinaryTree({2: 'phone', 7: 'computer', 8: 'eight', 9: 'scree', 'github': 'qiwsir'}) >>> btree.set_default(5) #如果不指定值,则会加入None >>> btree BinaryTree({2: 'phone', 5: None, 7: 'computer', 8: 'eight', 9: 'scree', 'github': 'qiwsir'}) >>> btree.get(2) #注意,.get(key)与.set_default(key[,d])的区别 'phone' >>> btree.get(3,"mobile") #不存在的 key,返回但不增加到树 'mobile' >>> btree BinaryTree({2: 'phone', 7: 'computer', 8: 'eight', 9: 'scree', 'github': 'qiwsir'})
>>> ctree BinaryTree({2: 'phone', 5: None, 7: 'computer', 8: 'eight', 9: 'scree', 'github': 'qiwsir'}) >>> ctree.remove_items([5,6]) #key=6,不存在,报错 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/site-packages/bintrees/abctree.py", line 271, in remove_items self.remove(key) File "/usr/local/lib/python2.7/site-packages/bintrees/bintree.py", line 124, in remove raise KeyError(str(key)) KeyError: '6' >>> ctree BinaryTree({2: 'phone', 7: 'computer', 8: 'eight', 9: 'scree', 'github': 'qiwsir'}) >>> ctree.remove_items([2,7,'github']) #按照 列表中顺序逐个删除 >>> ctree BinaryTree({8: 'eight', 9: 'scree'})
Python中的二叉树查找算法模块,布布扣,bubuko.com
原文:http://blog.csdn.net/qiwsir/article/details/36715163