2019独角兽企业重金招聘Python工程师标准>>>

Binarytree: Python Library for Studying Binary Trees

     

Introduction

Are you studying binary trees for your next exam, assignment or technical interview?

Binarytree is a Python library which provides a simple API to generate, visualize, inspect and manipulate binary trees. It allows you to skip the tedious work of setting up test data, and dive straight into practising your algorithms. Heaps and BSTs (binary search trees) are also supported.

Announcements

  • Binarytree version 4.0 is now out!
  • Please see the releases page for details on the latest updates.

Requirements

  • Python 2.7, 3.4, 3.5 or 3.6.

Installation

To install a stable version from PyPi:

~$ pip install binarytree

To install the latest version directly from GitHub:

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

You may need to use sudo depending on your environment.

Getting Started

By default, binarytree uses the following class to represent a node:

class Node(object):def __init__(self, value, left=None, right=None):self.value = value  # The node valueself.left = left    # Left childself.right = right  # Right child

Generate and pretty-print various types of binary trees:

>>> from binarytree import tree, 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
#

Use the binarytree.Node class to build your own trees:

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

Inspect tree properties:

>>> from binarytree import Node
>>>
>>> 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.height
2
>>> root.is_balanced
True
>>> root.is_bst
False
>>> root.is_complete
True
>>> root.is_max_heap
False
>>> root.is_min_heap
True
>>> root.is_perfect
False
>>> root.is_strict
True
>>> root.leaf_count
3
>>> root.max_leaf_depth
2
>>> root.max_node_value
5
>>> root.min_leaf_depth
1
>>> root.min_node_value
1
>>> root.size
5>>> 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)]]

Use level-order (breadth-first) indexes to manipulate nodes:

>>> from binarytree import Node
>>>
>>> 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
>>> del root[1]
>>> root.pprint(index=True)
#
#    0-1_
#        \
#        2-3

Traverse the trees using different algorithms:

>>> from binarytree import Node
>>>
>>> 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 are also supported:

>>> from binarytree import build
>>>
>>> # 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]

Check out the documentation for more details!

Contributing

Please have a look at this page before submitting a pull request. Thanks!

中文翻译

转载于:https://my.oschina.net/u/2245781/blog/1841729

BinaryTree-学习二叉树的Python库相关推荐

  1. python构建二叉树_BinaryTree:学习二叉树的Python库

    Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 简介: 您是否在为考试.作业或技术面试学习二叉树? Binarytree是一个Python库,它通过一个简 ...

  2. pygame是python的一个库吗,python学习pygame,,基本库导入impor

    python学习pygame,,基本库导入impor 基本库导入 import pygame import sys from pygame.locals import * 初始化 pygame.ini ...

  3. 用于小型图形挖掘研究的瑞士军刀:空手道俱乐部的图表学习Python库

    作者 | Benedek Rozemberczki 译者 | 天道酬勤 责编 | Carol 出品 | AI科技大本营(ID:rgznai100) 空手道俱乐部(Karate Club)是Networ ...

  4. 关于深度学习、NLP和计算机视觉的30个顶级Python库

    双语原文链接:Top Python Libraries for Deep Learning, Natural Language Processing & Computer Vision 请注意 ...

  5. 从Theano到Lasagne:基于Python的深度学习的框架和库

    从Theano到Lasagne:基于Python的深度学习的框架和库 [日期:2015-08-03] 来源:http://creative-punch.net/  作者:Creative Punch ...

  6. python爬虫库的功能_Python学习爬虫掌握的库资料大全和框架的选择的分析

    学Python,想必大家都是从爬虫开始的吧.毕竟网上类似的资源很丰富,开源项目也非常多. Python学习网络爬虫主要分3个大的版块:抓取,分析,存储 当我们在浏览器中输入一个url后回车,后台会发生 ...

  7. DL框架之Keras:深度学习框架Keras框架的简介、安装(Python库)、相关概念、Keras模型使用、使用方法之详细攻略

    DL框架之Keras:深度学习框架Keras框架的简介.安装(Python库).相关概念.Keras模型使用.使用方法之详细攻略 目录 Keras的简介 1.Keras的特点 2.Keras四大特性 ...

  8. python库怎么学啊最好_最常用的几个python库--学习引导

    核心库 1.NumPy 当我们用python来处理科学计算任务时,不可避免的要用到来自SciPy Stack的帮助.SciPy Stack是一个专为python中科学计算而设计的软件包,注意不要将它与 ...

  9. ​关于深度学习、NLP和计算机视觉的30个顶级Python库

    正文字数:2214  阅读时长:3分钟 再次感谢艾哈迈德·阿尼斯(Ahmed Anis)为收集这些数据做出的贡献,并感谢KDnuggets的其他工作人员的意见,见解和建议. 作者 / Matthew ...

最新文章

  1. 怎样处理重命名系列案例代码
  2. 独家|盘点5个TensorFlow和机器学习课程,程序员福利(附资源)
  3. Linux Kernel TCP/IP Stack — L2 Layer — Traffic Control(流量控制)的实现原理
  4. windows系统连接***后不能上网
  5. 浅谈 Python 程序和 C 程序的整合
  6. Topcoder SRM 648 (div.2)
  7. i9 9900k mysql_i9-9900K和9900KS有什么区别?i9-9900KS和i9-9900K区别对比评测
  8. grep从文件末尾开始找_新人自学前端到什么程度才能找工作?
  9. 海上瓶子下有东西吗_放置在车内的饮用水,经过暴晒后,还能喝吗?有异味,是毒素吗?...
  10. CSDN《程序员》杂志创始人蒋涛-推荐《程序员职场第一课》
  11. 刷题记录 CF每日一题打卡 2020.5月26-6月2
  12. 使用内存映射文件在进程间共享数据
  13. UI自动化测试 浅谈
  14. 时序逻辑电路的基础知识(结合Verilog)
  15. 菜鸟也疯狂,易语言自绘控件__按钮篇,用所有者自绘方式实现
  16. 复合函数求导定义证明,复合函数求导法则的又一证明
  17. java读取文本文件,并且去除重复字段
  18. Citrix ADC 13.0 下载 百度网盘 按您的方式进行应用交付
  19. 创作者基金 11 月亮点
  20. java获取浏览器url_java 打开浏览器 url

热门文章

  1. IT人怎能忘记这些开源?
  2. Sqoop在导入MySQL数据时遇到Timestamp列为0000-00-00 00:00:00报错
  3. 《再不疯狂,我们就老了》 -- [澳]塞巴斯蒂安·特里
  4. 关于JAVA的参数列表传值的问题
  5. winpcap的环境配置
  6. 给力开源,.Net开源地址大收集
  7. 网络管理员&MCSE2003之5:第1章 帐户和资源管理
  8. Netty面试题 汇总
  9. oracle 快速备份表数据
  10. 【思科】BGP的community属性解析