请选择 进入手机版 | 继续访问电脑版

网络科技

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

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

[复制链接]
頭痛~出賣了我 发表于 2016-10-10 03:58:31
451 14
BinaryTree: Python Library for Learning Binary Trees

  

Studying binary trees for your exam/assignment/interview? Try BinaryTree

Studying binary trees for your exam/assignment/interview? Try BinaryTree-1-网络科技-Tree,result,print,node,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 :
  1. ~$ pip install binarytree
复制代码
  To install the latest version directly fromGitHub:
  1. ~$ git clone https://github.com/joowani/binarytree.git
  2. ~$ 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:
  1. class Node(object):
  2.     def __init__(self, value):
  3.         self.value = value
  4.         self.left = None
  5.         self.right = None
复制代码
Generate and pretty-print binary trees:
  1. from binarytree import tree, bst, heap, pprint
  2. # Generate a random binary tree
  3. my_tree = tree(height=5, balanced=False)
  4. # Generate a random binary search tree
  5. my_bst = bst(height=5)
  6. # Generate random max heap
  7. my_heap = heap(height=1, max=True)
  8. # Pretty print the trees in stdout
  9. pprint(my_tree)
  10. pprint(my_bst)
  11. pprint(my_heap)
复制代码
  List representations are also supported:
  1. from heapq import heapify
  2. from binarytree import tree, convert, pprint
  3. my_list = [7, 3, 2, 6, 9, 4, 1, 5, 8]
  4. # Convert the list into a tree structure
  5. my_tree = convert(my_list)
  6. # Convert the list into a heap structure
  7. heapify(my_list)
  8. my_tree = convert(my_list)
  9. # Convert the tree back to a list
  10. my_list = convert(my_tree)
  11. # Pretty-printing also works on lists
  12. pprint(my_list)
复制代码
Inspect a tree to quickly see its various properties:
  1. from binarytree import tree, inspect
  2. my_tree = tree(height=10)
  3. result = inspect(my_tree)
  4. print(result['height'])
  5. print(result['is_bst'])
  6. print(result['is_height_balanced'])
  7. print(result['is_max_heap'])
  8. print(result['is_min_heap'])
  9. print(result['is_weight_balanced'])
  10. print(result['leaf_count'])
  11. print(result['max_leaf_depth'])
  12. print(result['max_value'])
  13. print(result['min_leaf_depth'])
  14. print(result['min_value'])
  15. print(result['node_count'])
复制代码
Import the Node class directly to build your own trees:
  1. from binarytree import Node, pprint
  2. root = Node(1)
  3. root.left = Node(2)
  4. root.right = Node(3)
  5. root.left.left = Node(4)
  6. root.left.right = Node(5)
  7. pprint(root)
复制代码
If the default Node class does not meet your requirements, you can define and use your own custom node specification:
  1. from binarytree import setup, tree, pprint
  2. # Define your own null/sentinel value (default: None)
  3. null = -1
  4. # Define own node class (default: binarytree.Node)
  5. class MyNode(object):
  6.     def __init__(self, data, left, right):
  7.         self.data = data
  8.         self.l_child = left
  9.         self.r_child = right
  10. # Call setup in the beginning to apply the custom specification
  11. setup(
  12.     node_init_func=lambda v: MyNode(v, null, null),
  13.     node_class=MyNode,
  14.     null_value=null,
  15.     value_attr='data',
  16.     left_attr='l_child',
  17.     right_attr='r_child'
  18. )
  19. my_custom_tree = tree()
  20. pprint(my_custom_tree)
复制代码



上一篇: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
撸过...
回复 支持 反对

使用道具 举报

我要投稿

回页顶回复上一篇下一篇回列表
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表