Skip to content

🌺 Node

bigtree.node.node

Node

Node(name, sep='/', **kwargs)

Bases: BaseNode

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

Note

Node names cannot contain separator symbol! This will not throw error, but you might run into issues when performing certain functions such as export-then-import of tree.

Nodes can be linked to each other with parent and children setter methods.

Examples:

>>> from bigtree import Node
>>> a = Node("a")
>>> b = Node("b")
>>> c = Node("c")
>>> d = Node("d")
>>> b.parent = a
>>> b.children = [c, d]

Directly passing parent argument.

>>> from bigtree import Node
>>> a = Node("a")
>>> b = Node("b", parent=a)
>>> c = Node("c", parent=b)
>>> d = Node("d", parent=b)

Directly passing children argument.

>>> from bigtree import Node
>>> d = Node("d")
>>> c = Node("c")
>>> b = Node("b", children=[c, d])
>>> a = Node("a", children=[b])

Node Creation

Node can be created by instantiating a Node 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 Node
>>> a = Node.from_dict({"name": "a", "age": 90})

Node Attributes

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

Get and set Node configuration

  1. sep: Get/set separator for path name

Get Node configuration

  1. node_name: Get node name, without accessing name directly. This is the preferred way to access node name as node_name is immutable, whereas name is mutable
  2. path_name: Get path name from root, separated by sep

Node Methods

These are methods available to be performed on Node.

Node methods

  1. show(): Print tree to console
  2. hshow(): Print tree in horizontal orientation to console
  3. vshow(): Print tree in vertical orientation to console

sep property writable

sep

Get separator, gets from root node.

Returns:

Type Description
str

Seperator

node_name property

node_name

Get node name.

Returns:

Type Description
str

Node name

path_name property

path_name

Get path name, separated by self.sep.

Returns:

Type Description
str

Path name

parent property writable

parent

Get parent node.

Returns:

Type Description
Optional[T]

Parent node, none if the node is root

children deletable property writable

children

Get child nodes.

Returns:

Type Description
Tuple[T, ...]

Child node(s)

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]

Ancestor(s) of node excluding itself

descendants property

descendants

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

Returns:

Type Description
Iterable[T]

Descendant(s) of node excluding itself

leaves property

leaves

Get iterator to yield all leaf nodes from self.

Returns:

Type Description
Iterable[T]

Leaf node(s) of node

siblings property

siblings

Get siblings of self.

Returns:

Type Description
Iterable[T]

Sibling(s) of node

left_sibling property

left_sibling

Get sibling left of self.

Returns:

Type Description
Optional[T]

Left sibling of node

right_sibling property

right_sibling

Get sibling right of self.

Returns:

Type Description
Optional[T]

Right sibling of node

node_path property

node_path

Get tuple of nodes starting from root.

Returns:

Type Description
Iterable[T]

Node path from root to itself

is_root property

is_root

Get indicator if self is root node.

Returns:

Type Description
bool

Indicator if node is root node

is_leaf property

is_leaf

Get indicator if self is leaf node.

Returns:

Type Description
bool

Indicator if node is leaf node

root property

root

Get root node of tree.

Returns:

Type Description
T

Root node

diameter property

diameter

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

Returns:

Type Description
int

Diameter of node

depth property

depth

Get depth of self, indexing starts from 1.

Returns:

Type Description
int

Depth of node

max_depth property

max_depth

Get maximum depth from root to leaf node.

Returns:

Type Description
int

Maximum depth of tree

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.

vshow

vshow(**kwargs)

Print tree in vertical orientation to console, takes in same keyword arguments as vprint_tree function.

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 Mapping[str, Any]

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

required

Returns:

Type Description
BaseNode

Base node

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 Iterable[str]

attributes to exclude

()
exclude_prefix str

prefix of attributes to exclude

''

Returns:

Type Description
List[Tuple[str, Any]]

List of attribute name and attribute value pairs

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

None

Returns:

Type Description
Any

Attribute value of node

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 Mapping[str, Any]

attribute information, key: attribute name, value: attribute value

required

go_to

go_to(node)

Get path from current node to specified node from same tree, uses get_path function.

Parameters:

Name Type Description Default
node T

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

required

Returns:

Type Description
Iterable[T]

Path from current node to destination node

append

append(other)

Add other as child of self. Can be chained.

Parameters:

Name Type Description Default
other T

other node, child to be added

required

extend

extend(others)

Add others as children of self. Can be chained.

Parameters:

Name Type Description Default
others List[T]

other nodes, children to be added

required

copy

copy()

Deep copy self; clone BaseNode.

Examples:

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

Returns:

Type Description
T

Cloned copy of node

sort

sort(**kwargs)

Sort children, possible keyword arguments include key=lambda node: node.node_name, reverse=True. Accepts kwargs for sort() function. Can be chained.

Examples:

>>> from bigtree import Node, print_tree
>>> a = Node('a')
>>> c = Node("c", parent=a)
>>> b = Node("b", parent=a)
>>> print_tree(a)
a
β”œβ”€β”€ c
└── b
>>> a.sort(key=lambda node: node.node_name)
Node(/a, )
>>> print_tree(a)
a
β”œβ”€β”€ b
└── c

plot

plot(*args, **kwargs)

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

Examples:

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

query

query(query, debug=False)

Query tree using Tree Definition Language.

Examples:

>>> from bigtree import list_to_tree
>>> path_list = ["a/b/d", "a/b/e/g", "a/b/e/h", "a/c/f"]
>>> root = list_to_tree(path_list)
>>> root.query("depth == 2")
[Node(/a/b, ), Node(/a/c, )]

Parameters:

Name Type Description Default
query str

query

required
debug bool

if True, will print out the parsed query

False

Returns:

Type Description
List[T]

List of nodes that fulfil the condition of query