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, 1 branches, and 2 leaves>
>>> tree.print()
<TreeNode root>
    <TreeNode branch>
        <TreeNode leaf2>
        <TreeNode leaf1>
Attributes:
rootcompas.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.

Methods

Constructors

from_json

Construct an object of this type from a JSON file.

Conversions

to_json

Convert an object to its native data representation and save it to a JSON file.

Builders and Modifiers

Accessors

Attributes

Topology

Geometry

Paths

Matrices

Mappings

Utilities

Other