Skip to content

✨ Construct

Construct Binary Tree from a heapq list. Binary Tree inherits from Tree, so it is able to support construction using string, list, dictionary, and pandas and polars DataFrame.

Binary Tree Construct Methods

Construct Binary Tree from Using heapq structure Add node attributes
List list_to_binarytree No

bigtree.binarytree.construct

list_to_binarytree

list_to_binarytree(heapq_list, node_type=BinaryNode)

Construct tree from a list of numbers (int or float) in heapq format.

Examples:

>>> from bigtree import BinaryTree
>>> nums_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> tree = BinaryTree.from_heapq_list(nums_list)
>>> tree.show()
1
β”œβ”€β”€ 2
β”‚   β”œβ”€β”€ 4
β”‚   β”‚   β”œβ”€β”€ 8
β”‚   β”‚   └── 9
β”‚   └── 5
β”‚       └── 10
└── 3
    β”œβ”€β”€ 6
    └── 7
>>> graph = tree.to_dot(node_colour="gold")
>>> graph.write_png("assets/construct_binarytree.png")

Sample Binary Tree

Parameters:

Name Type Description Default
heapq_list Sequence[int]

list containing integer node names, ordered in heapq fashion

required
node_type type[T]

node type of tree to be created

BinaryNode

Returns:

Type Description
T

Binary node