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:
- **kwargsdict[str, Any], optional
User-defined attributes of the tree.
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, 1 branches, and 2 leaves> >>> tree.print() <TreeNode root> <TreeNode branch> <TreeNode leaf2> <TreeNode leaf1>
- 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
Add a node to the tree.
Construct an object of this type from the provided data.
Get a node by its name.
Get all nodes by their name.
Print the spatial hierarchy of the tree.
Remove a node from the tree.
Traverse the tree from the root node.
Inherited Methods
Converts the instance to a string.
Make an independent copy of the data object.
Construct an object of this type from a JSON file.
Construct an object of this type from a JSON string.
Compute a hash of the data for comparison during version control using the sha256 algorithm.
Convert an object to its native data representation.
Convert an object to its native data representation and save it to a JSON file.
Convert an object to its native data representation and save it to a JSON string.
Validate the data against the object's data schema.