bigtree

๐ŸŒฟ BinaryNodeยถ

Classes:

BinaryNode([name,ย left,ย right,ย parent,ย children])

BinaryNode is an extension of Node, and is able to extend to any Python class for Binary Tree implementation.

class bigtree.node.binarynode.BinaryNode(name: str | int = '', left: T | None = None, right: T | None = None, parent: T | None = None, children: List[T | None] | None = None, **kwargs: Any)ยถ

Bases: Node

BinaryNode is an extension of Node, and is able to extend to any Python class for Binary Tree implementation. Nodes can have attributes if they are initialized from BinaryNode, dictionary, or pandas DataFrame.

BinaryNode can be linked to each other with children, left, or right setter methods. If initialized with children, it must be length 2, denoting left and right child.

>>> from bigtree import BinaryNode, print_tree
>>> a = BinaryNode(1)
>>> b = BinaryNode(2)
>>> c = BinaryNode(3)
>>> d = BinaryNode(4)
>>> a.children = [b, c]
>>> b.right = d
>>> print_tree(a)
1
โ”œโ”€โ”€ 2
โ”‚   โ””โ”€โ”€ 4
โ””โ”€โ”€ 3

Directly passing left, right, or children argument.

>>> from bigtree import BinaryNode
>>> d = BinaryNode(4)
>>> c = BinaryNode(3)
>>> b = BinaryNode(2, right=d)
>>> a = BinaryNode(1, children=[b, c])

BinaryNode Creation

Node can be created by instantiating a BinaryNode class or by using a dictionary. If node is created with dictionary, all keys of dictionary will be stored as class attributes.

>>> from bigtree import BinaryNode
>>> a = BinaryNode.from_dict({"name": "1"})
>>> a
BinaryNode(name=1, val=1)

BinaryNode Attributes

These are node attributes that have getter and/or setter methods.

Get BinaryNode configuration

  1. left: Get left children

  2. right: Get right children


Attributes:

children

Get child nodes

is_leaf

Get indicator if self is leaf node

left

Get left children

parent

Get parent node

right

Get right children

Methods:

sort(**kwargs)

Sort children, possible keyword arguments include key=lambda node: node.val, reverse=True

property children: Tuple[T, ...]ยถ

Get child nodes

Returns:

(Tuple[Optional[Self]])

property is_leaf: boolยถ

Get indicator if self is leaf node

Returns:

(bool)

property left: Tยถ

Get left children

Returns:

(Self)

property parent: T | Noneยถ

Get parent node

Returns:

(Optional[Self])

property right: Tยถ

Get right children

Returns:

(Self)

sort(**kwargs: Any) Noneยถ

Sort children, possible keyword arguments include key=lambda node: node.val, reverse=True

>>> from bigtree import BinaryNode, print_tree
>>> a = BinaryNode(1)
>>> c = BinaryNode(3, parent=a)
>>> b = BinaryNode(2, parent=a)
>>> print_tree(a)
1
โ”œโ”€โ”€ 3
โ””โ”€โ”€ 2
>>> a.sort(key=lambda node: node.val)
>>> print_tree(a)
1
โ”œโ”€โ”€ 2
โ””โ”€โ”€ 3