Path between nodes of a binary search tree

Asked

Viewed 205 times

3

inserir a descrição da imagem aqui

I need to create an algorithm that will define if a path between the value a and b are valid, my doubt is in relation to these rules, for example between node 3 and 7 there is a valid path, but between nodes 3 and 10 I would need to perform a turn, that still makes it valid?! and between nodes 1 and 7 there is also a valid path?!

1 answer

3


It’s probably an exercise, right? It’s complicated to say if something is valid without the context and the rules that can be applied in this case.

The first question you should ask is whether this tree supports navigation from a "child" (descending) element to your "parent (ascending). Some data structures store only descendants and there would be no viable path between 3 and 10.

However, if the search starts at the root of the tree, you can go through the tree twice, once searching for the 3 and another seeking 10. The path will be the combination of the two paths from the root.

Anyway, that’s why I hate abstract college exercises.

  • 2

    Hello @utluiz, because it is in my structure today the "son" does not know who is his "father", reevaluated the issue with their views I ended up realizing that the goal of the exercise is to be simple even. In the case the examples cited above would exist only the paths 3-7, ie there is no ( at least in this proposal ) that return to the parent node.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.