网络科技

    今日:654| 主题:246247
收藏本版
互联网、科技极客的综合动态。

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

[复制链接]
頭痛~出賣了我 发表于 2016-10-10 03:58:31
383 14

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

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
BinaryTree: Python Library for Learning Binary Trees

  
Studying binary trees for your exam/assignment/interview? Try BinaryTree-1 (binary,search,trees,studying,studying,abroad,52studying)
  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]~$ pip install binarytree[/code]   To install the latest version directly fromGitHub:
  [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]
友荐云推荐




上一篇:Forget the iPhone 7 — here are 9 reasons the 2017 iPhone will blow everyone awa
下一篇:33 photos of Google's rise from a Stanford dorm room to world domination (G
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

杜久超 发表于 2016-10-10 05:42:10
因为谦卑,所以高贵.因为懂得,所以慈悲;因为陌生,所以勇敢,因为距离,所以美丽.
回复 支持 反对

使用道具 举报

重修 发表于 2016-10-11 03:28:22
very good
回复 支持 反对

使用道具 举报

香波 发表于 2016-10-11 03:28:38
朋友妻不可欺睡睡觉还可以
回复 支持 反对

使用道具 举报

末路 发表于 2016-10-11 03:41:56
锄禾日当午,发帖真辛苦。谁知坛中餐,帖帖皆辛苦!
回复 支持 反对

使用道具 举报

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

使用道具 举报

念珍 发表于 2016-10-11 05:25:18
在乎的人不明白,明白的人不在乎。
回复 支持 反对

使用道具 举报

酸性脾气 发表于 2016-10-11 06:13:24
以后要跟頭痛~出賣了我好好学习学习!
回复 支持 反对

使用道具 举报

mn来吧 发表于 2016-10-11 18:08:19
嘘,低调。
回复 支持 反对

使用道具 举报

爱无余地 发表于 2016-11-11 11:52:48
撸过...
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

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

返回顶部 返回列表