• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中的二叉树查找算法模块使用指南

Python中的二叉树查找算法模块使用指南

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了python实现二叉树,python 二叉树,二叉树的建立 python,python 二叉树的深度,python 二叉树的遍历等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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
</div>

安装成功,ok!下面就看如何使用了。

应用

bintrees提供了丰富的API,涵盖了通常的多种应用。下面逐条说明其应用。

- 引用

如果按照一般模块的思路,输入下面的命令引入上述模块

>>> import bintrees
</div>

 
错了,这是错的,出现如下警告:(×××不可用,用×××)

  Warning: FastBinaryTree not available, using Python version BinaryTree.

  Warning: FastAVLTree not available, using Python version AVLTree.

  Warning: FastRBTree not available, using Python version RBTree.
</div>

正确的引入方式是:

  >>> from bintrees import BinaryTree   #只引入了BinartTree
  >>> from bintrees import *       #三个模块都引入了
</div>

- 实例化

看例子:

>>> btree = BinaryTree()
  >>> btree
  BinaryTree({})
  >>> type(btree)
  <class 'bintrees.bintree.BinaryTree'>
</div>

  
- 逐个增加键值对: .__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'})
</div>

  
- 批量添加: .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'})
</div>

  
- 查找某个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
</div>

  
- 根据key删除某个key-value: .__delitem__(key), O(log(n))

看例子:

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

- 根据key值得到该kye的value: .__getitem__(key)

看例子:

>>> 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__'
</div>

- 迭代器: .__iter__()

看例子:

>>> 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
</div>

- 树的数据长度: .__len__(),返回btree的长度。O(1)

看例子:

>>> btree
  BinaryTree({2: 'phone', 7: 'computer', 9: 'scree', 'Tom': 'headmaster', 'blog': 'http://blog.csdn.net/qiwsir'})
  >>> btree.__len__()
  5
</div>

- 找出key最大的k-v对: .__max__(),按照key排列,返回key最大的键值对。


- 找出key最小的键值对: .__min__()

看例子:

>>> btree
 BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'})
 >>> btree.__max__()
 (9, 'scree')
 >>> btree.__min__()
 (2, 'phone')
</div>

- 两棵树的关系运算

看例子:

>>> other = [(3,'http://www.jb51.net'),(7,'qiwsir')]
 >>> bother = BinaryTree()  #再建一个树
 >>> bother.update(other) #加入数据

 >>> bother
 BinaryTree({3: 'http://www.jb51.net', 7: 'qiwsir'})
 >>> btree
 BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'})
 
 >>> btree.__and__(bother)  #重叠部分部分
 BinaryTree({7: 'computer'})

 >>> btree.__or__(bother) #全部
 BinaryTree({2: 'phone', 3: 'http://www.jb51.net, 7: 'computer', 9: 'scree'})

 >>> btree.__sub__(bother)  #btree不与bother重叠的部分
 BinaryTree({2: 'phone', 9: 'scree'})
 
 >>> btree.__xor__(bother)  #两者非重叠部分
 BinaryTree({2: 'phone', 3: 'http://www.jb51.net, 9: 'scree'})
</div>

- 输出字符串模样,注意仅仅是输出的模样罢了: .__repr__()

看例子:

>>> btree
  BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'})
  >>> btree.__repr__()
  "BinaryTree({2: 'phone', 7: 'computer', 9: 'scree'})"
</div>

- 清空树中的所有数据 :.clear(),O(log(n))

看例子:

>>> bother  
 BinaryTree({3: 'http://blog.csdn.net/qiwsir', 7: 'qiwsir'})
 >>> bother.clear()
 >>> bother
 BinaryTree({})
 >>> bool(bother)
 False
</div>

- 浅拷贝: .copy(),官方文档上说是浅拷贝,但是我做了操作实现,是下面所示,还不是很理解其“浅”的含义。O(n*log(n))

看例子:

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


 

您可能想查找下面的文章:

  • Python中的二叉树查找算法模块使用指南
  • python数据结构之二叉树的统计与转换实例
  • python数据结构之二叉树的遍历实例
  • python数据结构之二叉树的建立实例
  • python数据结构树和二叉树简介
  • python数据结构之二叉树的统计与转换实例
  • python数据结构之二叉树的遍历实例
  • python数据结构之二叉树的建立实例
  • python数据结构树和二叉树简介

相关文章

  • python3中bytes和string之间的互相转换
  • Python实现的RSS阅读器实例
  • 浅谈python字典多键值及重复键值的使用
  • python使用正则表达式提取网页URL的方法
  • python基于queue和threading实现多线程下载实例
  • Python实现冒泡,插入,选择排序简单实例
  • Python的Bottle框架的一些使用技巧介绍
  • 使用Python将数组的元素导出到变量中(unpacking)
  • 浅要分析Python程序与C程序的结合使用
  • 详解Python的Django框架中的中间件

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 简单介绍Python中的round()方法
    • Python列表(list)、字典(dict)、字符串(string)基本操作小结
    • python 从远程服务器下载东西的代码
    • Python的Twisted框架中使用Deferred对象来管理回调函数
    • Python3写入文件常用方法实例分析
    • 详细介绍Python函数中的默认参数
    • Python中的os.path路径模块中的操作方法总结
    • 举例讲解Python中的list列表数据结构用法
    • python黑魔法之编码转换
    • Python中getattr函数和hasattr函数作用详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有