caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
812 stars 186 forks source link

treelib.tree.NodeIDAbsentError: Parent node '...' is not in the tree #71

Closed evandrocoan closed 7 years ago

evandrocoan commented 7 years ago

treelib.tree.NodeIDAbsentError: Parent node '...' is not in the tree

Running this code:


from treelib import Node, Tree

class Apple():
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return self.name

harryApple = Apple("Harry")
addonApple = Apple("Addons")

tree      = Tree()
harryNode = Node( harryApple )  # root node
addonNode = Node( addonApple )

tree.add_node( harryNode )
tree.add_node( addonNode, parent=harryNode )
tree.show()

Results in:

Traceback (most recent call last):
  File "D:\User\Dropbox\Applications\SoftwareVersioning\ComputerScienceGraduation\Semester20171\treelib\test.py", line 19, in <module>
    tree.add_node( addonNode, parent=harryNode )
  File "D:\User\Dropbox\Applications\SoftwareVersioning\ComputerScienceGraduation\Semester20171\treelib\treelib\tree.py", line 279, in add_node
    "is not in the tree" % parent)
treelib.tree.NodeIDAbsentError: Parent node 'Node(tag=<__main__.Apple instance at 0x0250DF58>, identifier='7b806e1e-17c5-11e7-95e9-6c626d784ee9', data=None)' is not in the tree

I added the node harry, but why it is not in the tree?

caesar0301 commented 7 years ago

Actually, it should be tree.add_node( addonNode, parent=harryNode.identifier)

But now I improved the add_node function to support Node instance as well. Thanks

evandrocoan commented 7 years ago

Good work!