Given a search tree, e.g.
"1"
└ "2"
├ "2.1"
┊ └ "3"
┊
└ "2.2"
└ "2.2.1"
└ "3"
As well as two nodes, a
and b
, that belong on that tree, e.g. "2.1" and "3". How can we check whether a
and b
are parent-child (or child-parent) related / connected?
For the first example, True should be yielded. Here are some more:
a="3" b="1" -> False
a="3" b="2" -> False
a="2.2.1" b="2.2" -> True
a="2.2.1" b="3" -> True
I'm currently using the anytree
library, with which I am struggling to implement this solution. The above graph is a structural simplification. What I have currently tried implementing is outlined here: https://pastebin.com/Mjk7gyqH
If the answer could be given with either pure python or anytree, that would be fantastic, but any answer is better than none.