fix get_distance() for topology_only=True #742
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In
tree.get_distance(target, target2, topology_only=True)
, the number of nodes betweentarget
andtarget2
was not calculated correctly.In the while loop starting in line 1024, we add +1 for the parent of
current
This means that when reaching the children ofancestor
, we countancestor
twice. The only exception to this is if one of the two nodes, saytarget
is theancestor
-- but in this case we still do countancestor
when going through the while loop fortarget2
, even thoughancestor
istarget
and should therefore not be counted.So in both cases, we need to subtract one from the count of nodes between
target
andtarget2
. In the previous version this has been done with the if condition in line 1026, which simply skips counting the parent oftarget
, which does not work iftarget
=ancestor
.I therefore deleted this if condition and instead added an if after the while loop in line
1030
to subtract one from the distance computed in the while loop.We need to check that
target != target2
, as in this case the ancestor is the node that's given, so we would not go into the while loop and therefore don't need to subtract one from the distance computed.This addresses issue #740.