🌸 BinaryNode
bigtree.node.binarynode
BinaryNode
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
left
: Get left childrenright
: Get right children
parent
property
writable
Get parent node.
Returns:
Type | Description |
---|---|
Optional[T]
|
Parent node, none if the node is root |
children
deletable
property
writable
Get child nodes.
Returns:
Type | Description |
---|---|
Tuple[T, ...]
|
Child nodes |
is_leaf
property
Get indicator if self is leaf node.
Returns:
Type | Description |
---|---|
bool
|
Indicator if node is leaf node |
parents
property
writable
Do not allow parents
attribute to be accessed.
Raises:
Type | Description |
---|---|
AttributeError
|
No such attribute |
ancestors
property
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
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
Get iterator to yield all leaf nodes from self.
Returns:
Type | Description |
---|---|
Iterable[T]
|
Leaf node(s) of node |
siblings
property
Get siblings of self.
Returns:
Type | Description |
---|---|
Iterable[T]
|
Sibling(s) of node |
left_sibling
property
Get sibling left of self.
Returns:
Type | Description |
---|---|
Optional[T]
|
Left sibling of node |
right_sibling
property
Get sibling right of self.
Returns:
Type | Description |
---|---|
Optional[T]
|
Right sibling of node |
node_path
property
Get tuple of nodes starting from root.
Returns:
Type | Description |
---|---|
Iterable[T]
|
Node path from root to itself |
is_root
property
Get indicator if self is root node.
Returns:
Type | Description |
---|---|
bool
|
Indicator if node is root node |
diameter
property
Get diameter of tree or subtree, the length of longest path between any two nodes.
Returns:
Type | Description |
---|---|
int
|
Diameter of node |
depth
property
Get depth of self, indexing starts from 1.
Returns:
Type | Description |
---|---|
int
|
Depth of node |
max_depth
property
Get maximum depth from root to leaf node.
Returns:
Type | Description |
---|---|
int
|
Maximum depth of tree |
sep
property
writable
Get separator, gets from root node.
Returns:
Type | Description |
---|---|
str
|
Seperator |
path_name
property
Get path name, separated by self.sep.
Returns:
Type | Description |
---|---|
str
|
Path name |
sort
Sort children, possible keyword arguments include key=lambda node: node.val
, reverse=True
.
Examples:
from_dict
classmethod
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:
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
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 value of node attribute. Returns default value if attribute name does not exist.
Examples:
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
go_to
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
Add other as child of self. Can be chained.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
T
|
other node, child to be added |
required |
extend
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
plot
query
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 |
hshow
Print tree in horizontal orientation to console, takes in same keyword arguments as hprint_tree
function.