Skip to content

Commit

Permalink
#71: support node instance for parent param of add_node
Browse files Browse the repository at this point in the history
  • Loading branch information
caesar0301 committed Apr 7, 2017
1 parent 2f12c40 commit 9433a1f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions treelib/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,20 @@ def add_node(self, node, parent=None):
raise DuplicatedNodeIdError("Can't create node "
"with ID '%s'" % node.identifier)

if parent is None:
pid = parent.identifier if isinstance(parent, Node) else parent

if pid is None:
if self.root is not None:
raise MultipleRootError("A tree takes one root merely.")
else:
self.root = node.identifier
elif not self.contains(parent):
elif not self.contains(pid):
raise NodeIDAbsentError("Parent node '%s' "
"is not in the tree" % parent)
"is not in the tree" % pid)

self._nodes.update({node.identifier: node})
self.__update_fpointer(parent, node.identifier, Node.ADD)
self.__update_bpointer(node.identifier, parent)
self.__update_fpointer(pid, node.identifier, Node.ADD)
self.__update_bpointer(node.identifier, pid)

def all_nodes(self):
"""Return all nodes in a list"""
Expand Down

0 comments on commit 9433a1f

Please sign in to comment.