Closed crazywkb closed 3 years ago
Hi @crazywkb,
Your cycle
function seems to have a bug (it will never terminate if the input is not falsy), but I get your point. I prefer option 2 as it doesn't try to hide anything from the users. The earlier the users discover that their tree has a problem the better. I will look into adding a circular dependency check in the next release. Thanks for bringing this up!
There is already a way to validate the tree (including circular dependency checks) via binarytree.Node.validate. After some thought, I decided to leave it up to the user to call this validate method explicitly rather than calling it in all methods like len
.
The reasoning is as follows:
I met a problem when implementing the morris traversal algorithm using this package, the simplified code is as follows:
the
while
statement will call theroot.__len__()
, which will get size by level traversal. But if there is a circular references like above, level traversal will get into infinite loop without any message about it. Changing the while statement towhile root is not None
can solve this, but I think it would be better to raise some message if this happened.Suggestions:
len(node)
can get correct size of node even though there is circular references.you can use
set
to record the visited node while counting size of node using level traversal. Besides,while node, if node
looks more graceful thanwhile node is not None, if node is not None
.if the first proposal is rejected, please raise some error message or warning while get into
len(node)
infinite loop.