Skip to content

🌸 BinaryNode

bigtree.node.binarynode

BinaryNode

BinaryNode(
    name="",
    left=None,
    right=None,
    parent=None,
    children=None,
    **kwargs
)

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.

Examples:

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

parents property writable

parents

Do not allow parents attribute to be accessed

Raises:

Type Description
AttributeError

No such attribute

ancestors property

ancestors

Get iterator to yield all ancestors of self, does not include self

Returns:

Type Description
Iterable[T]

(Iterable[Self])

descendants property

descendants

Get iterator to yield all descendants of self, does not include self

Returns:

Type Description
Iterable[T]

(Iterable[Self])

leaves property

leaves

Get iterator to yield all leaf nodes from self

Returns:

Type Description
Iterable[T]

(Iterable[Self])

siblings property

siblings

Get siblings of self

Returns:

Type Description
Iterable[T]

(Iterable[Self])

left_sibling property

left_sibling

Get sibling left of self

Returns:

Type Description
T

(Self)

right_sibling property

right_sibling

Get sibling right of self

Returns:

Type Description
T

(Self)

node_path property

node_path

Get tuple of nodes starting from root

Returns:

Type Description
Iterable[T]

(Iterable[Self])

is_root property

is_root

Get indicator if self is root node

Returns:

Type Description
bool

(bool)

root property

root

Get root node of tree

Returns:

Type Description
T

(Self)

diameter property

diameter

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

Returns:

Type Description
int

(int)

depth property

depth

Get depth of self, indexing starts from 1

Returns:

Type Description
int

(int)

max_depth property

max_depth

Get maximum depth from root to leaf node

Returns:

Type Description
int

(int)

sep property writable

sep

Get separator, gets from root node

Returns:

Type Description
str

(str)

node_name property

node_name

Get node name

Returns:

Type Description
str

(str)

path_name property

path_name

Get path name, separated by self.sep

Returns:

Type Description
str

(str)

left property writable

left

Get left children

Returns:

Type Description
T

(Self)

right property writable

right

Get right children

Returns:

Type Description
T

(Self)

parent property writable

parent

Get parent node

Returns:

Type Description
Optional[T]

(Optional[Self])

children deletable property writable

children

Get child nodes

Returns:

Type Description
Tuple[T, ...]

(Tuple[Optional[Self]])

is_leaf property

is_leaf

Get indicator if self is leaf node

Returns:

Type Description
bool

(bool)

from_dict classmethod

from_dict(input_dict)

Construct node from dictionary, all keys of dictionary will be stored as class attributes Input dictionary must have key name if not Node will not have any name

Examples:

>>> from bigtree import Node
>>> a = Node.from_dict({"name": "a", "age": 90})

Parameters:

Name Type Description Default
input_dict Dict[str, Any]

dictionary with node information, key: attribute name, value: attribute value

required

Returns:

Type Description
BaseNode

(BaseNode)

describe

describe(exclude_attributes=[], exclude_prefix='')

Get node information sorted by attribute name, returns list of tuples

Examples:

>>> from bigtree.node.node import Node
>>> a = Node('a', age=90)
>>> a.describe()
[('_BaseNode__children', []), ('_BaseNode__parent', None), ('_sep', '/'), ('age', 90), ('name', 'a')]
>>> a.describe(exclude_prefix="_")
[('age', 90), ('name', 'a')]
>>> a.describe(exclude_prefix="_", exclude_attributes=["name"])
[('age', 90)]

Parameters:

Name Type Description Default
exclude_attributes List[str]

list of attributes to exclude

[]
exclude_prefix str

prefix of attributes to exclude

''

Returns:

Type Description
List[Tuple[str, Any]]

(List[Tuple[str, Any]])

get_attr

get_attr(attr_name, default_value=None)

Get value of node attribute Returns default value if attribute name does not exist

Examples:

>>> from bigtree.node.node import Node
>>> a = Node('a', age=90)
>>> a.get_attr("age")
90

Parameters:

Name Type Description Default
attr_name str

attribute name

required
default_value Any

default value if attribute does not exist, defaults to None

None

Returns:

Type Description
Any

(Any)

set_attrs

set_attrs(attrs)

Set node attributes

Examples:

>>> from bigtree.node.node import Node
>>> a = Node('a')
>>> a.set_attrs({"age": 90})
>>> a
Node(/a, age=90)

Parameters:

Name Type Description Default
attrs Dict[str, Any]

attribute dictionary, key: attribute name, value: attribute value

required

go_to

go_to(node)

Get path from current node to specified node from same tree

Examples:

>>> from bigtree import Node, print_tree
>>> a = Node(name="a")
>>> b = Node(name="b", parent=a)
>>> c = Node(name="c", parent=a)
>>> d = Node(name="d", parent=b)
>>> e = Node(name="e", parent=b)
>>> f = Node(name="f", parent=c)
>>> g = Node(name="g", parent=e)
>>> h = Node(name="h", parent=e)
>>> print_tree(a)
a
├── b
│   ├── d
│   └── e
│       ├── g
│       └── h
└── c
    └── f
>>> d.go_to(d)
[Node(/a/b/d, )]
>>> d.go_to(g)
[Node(/a/b/d, ), Node(/a/b, ), Node(/a/b/e, ), Node(/a/b/e/g, )]
>>> d.go_to(f)
[Node(/a/b/d, ), Node(/a/b, ), Node(/a, ), Node(/a/c, ), Node(/a/c/f, )]

Parameters:

Name Type Description Default
node Self

node to travel to from current node, inclusive of start and end node

required

Returns:

Type Description
Iterable[T]

(Iterable[Self])

append

append(other)

Add other as child of self

Parameters:

Name Type Description Default
other Self

other node, child to be added

required

extend

extend(others)

Add others as children of self

Parameters:

Name Type Description Default
others Self

other nodes, children to be added

required

copy

copy()

Deep copy self; clone self

Examples:

>>> from bigtree.node.node import Node
>>> a = Node('a')
>>> a_copy = a.copy()

Returns:

Type Description
T

(Self)

show

show(**kwargs)

Print tree to console, takes in same keyword arguments as print_tree function

hshow

hshow(**kwargs)

Print tree in horizontal orientation to console, takes in same keyword arguments as hprint_tree function

sort

sort(**kwargs)

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

Examples:

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