### 网络科技

今日:199| 主题:277703

# [其他] Studying binary trees for your exam/assignment/interview? Try BinaryTree

451 14
 BinaryTree: Python Library for Learning Binary Trees    Studying binary trees for your exam/assignment/interview? Try BinaryTree   Introduction   Are you studying binary trees for your next exam, assignment or technical interview?    BinaryTreeis a minimal Python library which provides you with a simple API to generate, visualize and inspect binary trees so you can skip the tedious work of mocking up test trees, and dive right into practising your algorithms! Heaps and BSTs (binary search trees) are also supported.   Installation    To install a stable version from PyPi : ~\$ pip install binarytree复制代码  To install the latest version directly fromGitHub: ~\$ git clone https://github.com/joowani/binarytree.git~\$ python binarytree/setup.py install复制代码  You may need to use sudo depending on your environment setup.   Getting Started    BinaryTreeuses the following class to represent a tree node: class Node(object):    def __init__(self, value):        self.value = value        self.left = None        self.right = None复制代码 Generate and pretty-print binary trees: from binarytree import tree, bst, heap, pprint# Generate a random binary treemy_tree = tree(height=5, balanced=False)# Generate a random binary search treemy_bst = bst(height=5)# Generate random max heapmy_heap = heap(height=1, max=True)# Pretty print the trees in stdoutpprint(my_tree)pprint(my_bst)pprint(my_heap)复制代码  List representations are also supported: from heapq import heapifyfrom binarytree import tree, convert, pprintmy_list = [7, 3, 2, 6, 9, 4, 1, 5, 8]# Convert the list into a tree structuremy_tree = convert(my_list)# Convert the list into a heap structureheapify(my_list)my_tree = convert(my_list)# Convert the tree back to a listmy_list = convert(my_tree)# Pretty-printing also works on listspprint(my_list)复制代码 Inspect a tree to quickly see its various properties: from binarytree import tree, inspectmy_tree = tree(height=10)result = inspect(my_tree)print(result['height'])print(result['is_bst'])print(result['is_height_balanced'])print(result['is_max_heap'])print(result['is_min_heap'])print(result['is_weight_balanced'])print(result['leaf_count'])print(result['max_leaf_depth'])print(result['max_value'])print(result['min_leaf_depth'])print(result['min_value'])print(result['node_count'])复制代码 Import the Node class directly to build your own trees: from binarytree import Node, pprintroot = Node(1)root.left = Node(2)root.right = Node(3)root.left.left = Node(4)root.left.right = Node(5)pprint(root)复制代码 If the default Node class does not meet your requirements, you can define and use your own custom node specification: from binarytree import setup, tree, pprint# Define your own null/sentinel value (default: None)null = -1# Define own node class (default: binarytree.Node)class MyNode(object):    def __init__(self, data, left, right):        self.data = data        self.l_child = left        self.r_child = right# Call setup in the beginning to apply the custom specificationsetup(    node_init_func=lambda v: MyNode(v, null, null),    node_class=MyNode,    null_value=null,    value_attr='data',    left_attr='l_child',    right_attr='r_child')my_custom_tree = tree()pprint(my_custom_tree)复制代码

 因为谦卑,所以高贵.因为懂得,所以慈悲；因为陌生,所以勇敢,因为距离,所以美丽.

 very good

 朋友妻不可欺睡睡觉还可以

 锄禾日当午，发帖真辛苦。谁知坛中餐，帖帖皆辛苦！

rt春业逢o 发表于 2016-10-11 05:13:24
 生前何必久睡,死后自会长眠……

 在乎的人不明白，明白的人不在乎。

 以后要跟頭痛~出賣了我好好学习学习！

mn来吧 发表于 2016-10-11 18:08:19
 嘘，低调。

 撸过...

• ## 保洁大姐蹭伤豪车，因丈夫要与其离婚拒绝赔

微信号 hello908 一 [...]

• ## 保洁大姐蹭伤豪车丈夫想要离婚,夫妻感情不

微信号 点击上方“蓝字”关注本公众号最接地气的 [...]

• ## 成都保洁大姐蹭伤豪车，丈夫称要与其离婚！

微信号 sinasc 今天 [...]

• ## ćŹ˘äšé˘éĺćśä¸

ĺžŽäżĄĺˇ [...]

• ## 女人的衣柜里一定要有这件衣服，竟然怎么穿

微信号 chuanyidaba [...]

• ## 蹭伤豪车，丈夫离婚

微信号 lutaivsh 关 [...]

• ## ćľčŻ | čĺŚšĺżďź

ĺžŽäżĄĺˇ [...]

• ## 保洁大姐蹭伤豪车，因丈夫要与其离婚拒绝赔

微信号 jiamigai 点 [...]

• ## 超短发 体验不一样的自己

微信号 TUO-KE 点击 [...]

• ## çč´ľĺŚăććă

ĺžŽäżĄĺˇ [...]