Tree
- class compas.datastructures.Tree[source]
Bases:
Datastructure
A hierarchical data structure that organizes elements into parent-child relationships. The tree starts from a unique root node, and every node (excluding the root) has exactly one parent.
- Parameters:
- namestr, optional
The name of the tree.
- **kwargsdict, optional
Additional keyword arguments, which are stored in the attributes dict.
Examples
>>> from compas.datastructures import Tree, TreeNode >>> tree = Tree() >>> root = TreeNode("root") >>> branch = TreeNode("branch") >>> leaf1 = TreeNode("leaf1") >>> leaf2 = TreeNode("leaf2") >>> tree.add(root) >>> root.add(branch) >>> branch.add(leaf1) >>> branch.add(leaf2) >>> print(tree) <Tree with 4 nodes> |--<TreeNode: root> |-- <TreeNode: branch> |-- <TreeNode: leaf1> |-- <TreeNode: leaf2>
- Attributes:
- root
compas.datastructures.TreeNode
The root node of the tree.
- nodesgenerator[
compas.datastructures.TreeNode
] The nodes of the tree.
- leavesgenerator[
compas.datastructures.TreeNode
] A generator of the leaves of the tree.
- root
Methods
Constructors
Construct an object of this type from a JSON file. |
Conversions
Convert an object to its native data representation and save it to a JSON file. |