Skip to content

Commit

Permalink
make tree link dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
Licini committed Oct 6, 2023
1 parent 7666de4 commit 9928394
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/compas/datastructures/tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class TreeNode(Data):
The parent node of the tree node.
children : set[:class:`~compas.datastructures.TreeNode`]
The children of the tree node.
tree : :class:`~compas.datastructures.Tree`
The tree to which the node belongs.
is_root : bool
True if the node is the root node of the tree.
is_leaf : bool
Expand Down Expand Up @@ -55,6 +57,7 @@ def __init__(self, name=None, attributes=None):
self.attributes = attributes or {}
self._parent = None
self._children = set()
self._tree = None

def __repr__(self):
return "<TreeNode {}>".format(self.name)
Expand All @@ -79,6 +82,13 @@ def parent(self):
def children(self):
return self._children

@property
def tree(self):
if self.is_root:
return self._tree
else:
return self.parent.tree

@property
def data(self):
return {
Expand Down Expand Up @@ -303,6 +313,7 @@ def add(self, node, parent=None):
raise ValueError("The tree already has a root node, remove it first.")

self._root = node
node._tree = self

else:
# add the node as a child of the parent node
Expand Down Expand Up @@ -338,6 +349,7 @@ def remove(self, node):
"""
if node == self.root:
self._root = None
node._tree = None
else:
node.parent.remove(node)

Expand Down

0 comments on commit 9928394

Please sign in to comment.