研究二叉树的python库

binarytree的Python项目详细描述


Build StatusPackage VersionPython VersionsTest CoverageIssues OpenMIT License

Demo GIF

导言

你在为下一次考试、作业或技术面试学习二叉树吗

Binarytree是一个Python库,它提供了一个简单的API来生成, 可视化、检查和操作二叉树。它允许您跳过 建立测试数据的繁琐工作,并直接开始练习 算法还支持堆和bst(二进制搜索树)。

公告

  • 有关最新更新的详细信息,请参见releases

要求

  • Python2.7、3.4、3.5、3.6或3.7

安装

要从PyPi安装稳定版本:

~$ pip install binarytree

直接从GitHub安装最新版本:

~$ pip install -e git+git@github.com:joowani/binarytree.git@master#egg=binarytree

根据环境的不同,您可能需要使用sudo

开始

默认情况下,binarytree使用以下类表示节点:

classNode(object):def__init__(self,value,left=None,right=None):self.value=value# The node valueself.left=left# Left childself.right=right# Right child

生成并漂亮地打印各种类型的二叉树:

>>>frombinarytreeimporttree,bst,heap>>>>>># Generate a random binary tree and return its root node>>>my_tree=tree(height=3,is_perfect=False)>>>>>># Generate a random BST and return its root node>>>my_bst=bst(height=3,is_perfect=True)>>>>>># Generate a random max heap and return its root node>>>my_heap=heap(height=3,is_max=True,is_perfect=False)>>>>>># Pretty-print the trees in stdout>>>print(my_tree)##        _______1_____#       /             \#      4__          ___3#     /   \        /    \#    0     9      13     14#         / \       \#        7   10      2#>>>print(my_bst)##            ______7_______#           /              \#        __3__           ___11___#       /     \         /        \#      1       5       9         _13#     / \     / \     / \       /   \#    0   2   4   6   8   10    12    14#>>>print(my_heap)##              _____14__#             /         \#        ____13__        9#       /        \      / \#      12         7    3   8#     /  \       /#    0    10    6#

使用binarytree.Node类构建自己的树:

>>>frombinarytreeimportNode>>>>>>root=Node(1)>>>root.left=Node(2)>>>root.right=Node(3)>>>root.left.right=Node(4)>>>>>>print(root)##      __1#     /   \#    2     3#     \#      4#

检查树属性:

>>>frombinarytreeimportNode>>>>>>root=Node(1)>>>root.left=Node(2)>>>root.right=Node(3)>>>root.left.left=Node(4)>>>root.left.right=Node(5)>>>>>>print(root)##        __1#       /   \#      2     3#     / \#    4   5#>>>root.height2>>>root.is_balancedTrue>>>root.is_bstFalse>>>root.is_completeTrue>>>root.is_max_heapFalse>>>root.is_min_heapTrue>>>root.is_perfectFalse>>>root.is_strictTrue>>>root.leaf_count3>>>root.max_leaf_depth2>>>root.max_node_value5>>>root.min_leaf_depth1>>>root.min_node_value1>>>root.size5>>>root.properties# To see all at once:{'height':2,'is_balanced':True,'is_bst':False,'is_complete':True,'is_max_heap':False,'is_min_heap':True,'is_perfect':False,'is_strict':True,'leaf_count':3,'max_leaf_depth':2,'max_node_value':5,'min_leaf_depth':1,'min_node_value':1,'size':5}>>>root.leaves[Node(3),Node(4),Node(5)]>>>root.levels[[Node(1)],[Node(2),Node(3)],[Node(4),Node(5)]]

使用level-order (breadth-first)索引操作节点:

>>>frombinarytreeimportNode>>>>>>root=Node(1)# index: 0, value: 1>>>root.left=Node(2)# index: 1, value: 2>>>root.right=Node(3)# index: 2, value: 3>>>root.left.right=Node(4)# index: 4, value: 4>>>root.left.right.left=Node(5)# index: 9, value: 5>>>>>>print(root)##      ____1#     /     \#    2__     3#       \#        4#       /#      5#>>># Use binarytree.Node.pprint instead of print to display indexes>>>root.pprint(index=True)##       _________0-1_#      /             \#    1-2_____        2-3#            \#           _4-4#          /#        9-5#>>># Return the node/subtree at index 9>>>root[9]Node(5)>>># Replace the node/subtree at index 4>>>root[4]=Node(6,left=Node(7),right=Node(8))>>>root.pprint(index=True)##       ______________0-1_#      /                  \#    1-2_____             2-3#            \#           _4-6_#          /     \#        9-7     10-8#>>># Delete the node/subtree at index 1>>>delroot[1]>>>root.pprint(index=True)##    0-1_#        \#        2-3

使用不同的算法遍历树:

>>>frombinarytreeimportNode>>>>>>root=Node(1)>>>root.left=Node(2)>>>root.right=Node(3)>>>root.left.left=Node(4)>>>root.left.right=Node(5)>>>>>>print(root)##        __1#       /   \#      2     3#     / \#    4   5#>>>root.inorder[Node(4),Node(2),Node(5),Node(1),Node(3)]>>>root.preorder[Node(1),Node(2),Node(4),Node(5),Node(3)]>>>root.postorder[Node(4),Node(5),Node(2),Node(3),Node(1)]>>>root.levelorder[Node(1),Node(2),Node(3),Node(4),Node(5)]>>>list(root)# Equivalent to root.levelorder[Node(1),Node(2),Node(3),Node(4),Node(5)]

也支持List representations

>>>frombinarytreeimportbuild>>>>>># Build a tree from list representation>>>values=[7,3,2,6,9,None,1,5,8]>>>root=build(values)>>>print(root)##            __7#           /   \#        __3     2#       /   \     \#      6     9     1#     / \#    5   8#>>># Convert the tree back to list representation>>>root.values[7,3,2,6,9,None,1,5,8]

查看documentation了解更多详细信息!

贡献

请在提交拉取请求之前查看此page。谢谢!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在ArrayList中比较数字   java在Kotlin中使异步调用同步   让“Scala编程”junit示例在IntelliJ中工作的java问题   java Servlet侦听器未在ContextListener中设置属性   将Microsoft SQL Server数据库连接到我的Java项目   加载资源时出现java“需要注册工厂”异常   java如何使用POI检查excel中的重复记录?   java如何更改机器生成的代码   java如何确保重写的方法是同步的   用Spring编写Hibernate时的java XML奥秘   java管理mysql数据库中存储的用户权限   java如何运行。来自Javascript的jar方法   java我想在Web应用程序中进行身份验证&对桌面应用程序使用相同的凭据。我该怎么做?