Skip to content

🎄 Binary Tree

Conceptually, binary trees are made up of binary nodes, and they are synonymous; a tree is a node. In bigtree implementation, node refers to the BinaryNode class, whereas tree refers to the BinaryTree class. BinaryTree is implemented as a wrapper around a BinaryNode to implement tree-level methods for a more intuitive API.

Construct, export, helper, query, search, and iterator methods encapsulated in BinaryTree class. Binary Tree is a type of Tree and inherits from Tree, hence all Tree methods are also available in BinaryTree class.

Binary Tree Construct Methods

Construct Binary Tree from Using heapq structure Add node attributes
List BinaryTree.from_heapq_list No

Iterator Methods

Data Structure Algorithm Description
Binary Tree inorder_iter Depth-First Search, LNR

bigtree.binarytree.binarytree

BinaryTree

BinaryTree(root)

Bases: Tree

BinaryTree wraps around BinaryNode class to provide a quick, intuitive, Pythonic API for

  • Construction with dataframe, dictionary, list, or string
  • Export to dataframe, dictionary, list, string, or images
  • Query and Search methods to find one or more Nodes
  • Helper methods for cloning, pruning, getting tree diff

Do refer to the various modules respectively on the keyword parameters.

diameter property

diameter

Get diameter of tree, the length of longest path between any two nodes.

Returns:

Type Description
int

Diameter of tree

depth property

depth

Get depth of tree, indexing starts from 1.

Returns:

Type Description
int

Depth of tree

plot

plot(*args, **kwargs)

Plot tree in line form. Accepts args and kwargs for matplotlib.pyplot.plot() function.

Examples:

>>> from bigtree import Tree
>>> path_list = ["a/b/d", "a/b/e/g", "a/b/e/h", "a/c/f"]
>>> tree = Tree.from_list(path_list)
>>> tree.plot("-ok")
<Figure size 1280x960 with 1 Axes>

copy

copy()

Deep copy self; clone Tree.

Returns:

Type Description
T

Cloned copy of Tree