国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中的二叉樹查找算法模塊使用指南

2020-02-23 05:31:09
字體:
來源:轉載
供稿:網友

python中的二叉樹模塊內容:

BinaryTree:非平衡二叉樹
 AVLTree:平衡的AVL樹
 RBTree:平衡的紅黑樹
以上是用python寫的,相面的模塊是用c寫的,并且可以做為Cython的包。

FastBinaryTree
 FastAVLTree
 FastRBTree
特別需要說明的是:樹往往要比python內置的dict類慢一些,但是它中的所有數據都是按照某個關鍵詞進行排序的,故在某些情況下是必須使用的。

安裝和使用

安裝方法

安裝環境:

ubuntu12.04, python 2.7.6

安裝方法

下載源碼,地址:https://bitbucket.org/mozman/bintrees/src
進入源碼目錄,看到setup.py文件,在該目錄內運行   

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'>

  
- 逐個增加鍵值對: .__setitem__(k,v) .復雜度O(log(n))(后續說明中,都會有復雜度標示,為了簡單,直接標明:O(log(n)).)

看例子:

>>> 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'})

  
- 批量添加: .update(E)  E是dict/iterable,將E批量更新入btree. O(E*log(n))

看例子:

>>> 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'})

  
- 查找某個key是否存在: .__contains__(k)  如果含有鍵k,則返回True,否則返回False. O(log(n))

看例子:

>>> 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            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桃园县| 射洪县| 翼城县| 东源县| 桂林市| 山西省| 永善县| 明水县| 庆阳市| 永嘉县| 乌海市| 长乐市| 子洲县| 武功县| 墨脱县| 永宁县| 吴忠市| 巴东县| 林口县| 神池县| 花垣县| 板桥市| 喀喇沁旗| 平安县| 普陀区| 定陶县| 贵定县| 泾川县| 绥江县| 绥阳县| 碌曲县| 色达县| 龙川县| 虹口区| 延川县| 嵊州市| 巨野县| 永城市| 怀集县| 瓦房店市| 柞水县|