๐ฟ BinaryNodeยถ
Classes:
|
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:
NodeBinaryNode 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
left: Get left childrenright: Get right children
Attributes:
Get child nodes
Get indicator if self is leaf node
Get left children
Get parent node
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