Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Should prevent loop when creating tree and provide proper error message #5
Just try this code by mistake:
node = Tree::TreeNode.new("Root") node << node
And it throw following exception:
As it try to output node in irb.
It maybe better to check the input data and prevent user to change node if they add children node that may cause loop.
Thanks for highlighting the error.
I have added a check to prevent the scenario pointed out by your sample code, and the fix will be available in 0.8.3.
However, the possibility exists that a node which has already been added to the tree can get added again further down the hierarchy, and this is a somewhat different problem to solve. We could check every single addition against the tree, but this will be a big performance hit for large trees.
The question is really about performance, and since RubyTree is meant for generic usage, it needs to be balanced appropriately.