### 网络科技

今日:654| 主题:246247

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

383 14

### 立即注册CoLaBug.com会员，免费获得投稿人的专业资料，享用更多功能，玩转个人品牌！

x
BinaryTree: Python Library for Learning Binary Trees

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 :
[code]~\$ git clone https://github.com/joowani/binarytree.git
~\$ python binarytree/setup.py install[/code]   You may need to use sudo depending on your environment setup.
Getting Started

BinaryTreeuses the following class to represent a tree node:
[code]class Node(object):

def __init__(self, value):
self.value = value
self.left = None
self.right = None[/code]  Generate and pretty-print binary trees:
[code]from binarytree import tree, bst, heap, pprint

# Generate a random binary tree
my_tree = tree(height=5, balanced=False)

# Generate a random binary search tree
my_bst = bst(height=5)

# Generate random max heap
my_heap = heap(height=1, max=True)

# Pretty print the trees in stdout
pprint(my_tree)
pprint(my_bst)
pprint(my_heap)[/code]   List representations are also supported:
[code]from heapq import heapify
from binarytree import tree, convert, pprint

my_list = [7, 3, 2, 6, 9, 4, 1, 5, 8]

# Convert the list into a tree structure
my_tree = convert(my_list)

# Convert the list into a heap structure
heapify(my_list)
my_tree = convert(my_list)

# Convert the tree back to a list
my_list = convert(my_tree)

# Pretty-printing also works on lists
pprint(my_list)[/code]  Inspect a tree to quickly see its various properties:
[code]from binarytree import tree, inspect

my_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'])[/code]  Import the Node class directly to build your own trees:
[code]from binarytree import Node, pprint

root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

pprint(root)[/code]  If the default Node class does not meet your requirements, you can define and use your own custom node specification:
[code]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 specification
setup(
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)[/code]

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

 very good

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

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

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

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

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

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

 撸过...

• ## 盘点国内知名ASO优化工具与APP统计工具

据蝉大师ASO分析工具监控到的数据显示，12月初至 [...]

• ## 聊聊 Spring Cloud Config

一般服务器的应用都有以下几种类型, 其中 [...]

• ## 轻轻的我来了 唯美清新花朵图片

唯美清新花朵图片 　　轻轻的我来了，挥一挥衣 [...]

• ## 美国密歇根州签署法令：自动驾驶汽车上路合

对于自动驾驶技术很多人有不同的看法，但不管怎 [...]

• ## 赵丽颖入职互联网公司 担爱奇艺首席推荐官

12月9日消息，今日下午，在爱奇艺创新大厦举办了 [...]

• ## 我怀念的 微信背景图片

太爱了，所以我没有哭，没有说。——《我怀念的 [...]

• ## 前海人寿发布声明：未来逐步卖出格力股票

前海人寿12月9日晚间就有关投资格力电器发布声明 [...]

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.