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 :

~\$ pip install binarytree

~\$ 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 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)

List representations are also supported:

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)

Inspect a tree to quickly see its various properties:

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'])

Import the Node class directly to build your own trees:

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)

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 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)

tags: tree,print,result,my,Node,binarytree,self,value,left,trees,list,your,heap

1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。