Python中的二叉树查找算法模块使用指南
|
python中的二叉树模块内容: BinaryTree:非平衡二叉树 FastBinaryTree 安装和使用 安装方法 安装环境: ubuntu12.04,python 2.7.6 安装方法 下载源码,地址:https://bitbucket.org/mozman/bintrees/src python setup.py install 安装成功,ok!下面就看如何使用了。 应用 bintrees提供了丰富的API,涵盖了通常的多种应用。下面逐条说明其应用。 - 引用 如果按照一般模块的思路,输入下面的命令引入上述模块 >>> import bintrees Warning: FastBinaryTree not available,using Python version BinaryTree. Warning: FastAVLTree not available,using Python version AVLTree. Warning: FastRBTree not available,using Python version RBTree. 正确的引入方式是: >>> 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','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','blog': 'http://blog.csdn.net/qiwsir'})
>>> btree.__delitem__(5) #删除key=5的key-value,即:5:'tea' 被删除.
>>> btree
BinaryTree({2: 'phone','blog': 'http://blog.csdn.net/qiwsir'})
- 根据key值得到该kye的value: .__getitem__(key) 看例子:
>>> btree
BinaryTree({2: 'phone','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__'
- 迭代器: .__iter__() 看例子:
>>> btree
BinaryTree({2: 'phone','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
- 树的数据长度: .__len__(),返回btree的长度。O(1) 看例子:
>>> btree
BinaryTree({2: 'phone','blog': 'http://blog.csdn.net/qiwsir'})
>>> btree.__len__()
5
- 找出key最大的k-v对: .__max__(),按照key排列,返回key最大的键值对。
看例子:
>>> btree
BinaryTree({2: 'phone',9: 'scree'})
>>> btree.__max__()
(9,'scree')
>>> btree.__min__()
(2,'phone')
- 两棵树的关系运算 看例子:
>>> other = [(3,'http://www.jb51.net'),'qiwsir')]
>>> bother = BinaryTree() #再建一个树
>>> bother.update(other) #加入数据
>>> bother
BinaryTree({3: 'http://www.jb51.net',7: 'qiwsir'})
>>> btree
BinaryTree({2: 'phone',9: 'scree'})
>>> btree.__and__(bother) #重叠部分部分
BinaryTree({7: 'computer'})
>>> btree.__or__(bother) #全部
BinaryTree({2: 'phone',3: 'http://www.jb51.net,9: 'scree'})
>>> btree.__sub__(bother) #btree不与bother重叠的部分
BinaryTree({2: 'phone',9: 'scree'})
>>> btree.__xor__(bother) #两者非重叠部分
BinaryTree({2: 'phone',9: 'scree'})
- 输出字符串模样,注意仅仅是输出的模样罢了: .__repr__() 看例子:
>>> btree
BinaryTree({2: 'phone',9: 'scree'})
>>> btree.__repr__()
"BinaryTree({2: 'phone',9: 'scree'})"
- 清空树中的所有数据 :.clear(),O(log(n)) 看例子:
>>> bother
BinaryTree({3: 'http://blog.csdn.net/qiwsir',7: 'qiwsir'})
>>> bother.clear()
>>> bother
BinaryTree({})
>>> bool(bother)
False
- 浅拷贝: .copy(),官方文档上说是浅拷贝,但是我做了操作实现,是下面所示,还不是很理解其“浅”的含义。O(n*log(n)) 看例子:
>>> btree
BinaryTree({2: 'phone',9: 'scree'})
>>> ctree = btree.copy()
>>> ctree
BinaryTree({2: 'phone',9: 'scree'})
>>> btree.__setitem__("github","qiwsir") #增加btree的数据
>>> btree
BinaryTree({2: 'phone','github': 'qiwsir'})
>>> ctree
BinaryTree({2: 'phone',9: 'scree'}) #这是不是在说明属于深拷贝呢?
>>> ctree.__delitem__(7) #删除ctree的一个数据
>>> ctree
BinaryTree({2: 'phone',9: 'scree'})
>>> btree
BinaryTree({2: 'phone','github': 'qiwsir'})
- 移除树中的一个数据: .discard(key),这个功能与.__delitem__(key)类似.两者都不反悔值。O(log(n)) 看例子:
>>> 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>",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'
- 根据key查找,并返回或返回备用值: .get(key[,d])。如果key在树中存在,则返回value,否则如果有d,则返回d值。O(log(n)) 看例子:
>>> btree
BinaryTree({2: 'phone','github': 'qiwsir'})
>>> btree.get(2,"algorithm")
'phone'
>>> btree.get("python","algorithm") #没有key='python'的值,返回'algorithm'
'algorithm'
>>> btree.get("python") #如果不指定第二个参数,若查不到,则返回None
>>>
- 判断树是否为空: is_empty().根据树数据的长度,如果数据长度为0,则为空。O(1) (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
