I have the following tree structure holding sets of values in it:
Node ({a, b})
/ \
Node ({c}) Node ({d, e})
/ \
Node ({f, g}) Node({h})
I want to check if e
is in the tree - I would need to traverse from parent to children and find the node. I don't necessarily start from the root. I could start from Node({d, e})
and see if there is i
in that subtree.
One thing I could do is to change the data structure for every Node to have values from all descendants, so the top one would be:
Node({a, b, c, d, e, f, g, h})
and the right child would be Node({d, e, f, g, h})
I would be trading memory for speed.
Another alternative is I could have a reverse index from value to Nodes that have it or descendants.
Each option has a trade-offs.
My question is: is there some specialized data structures that I am not aware of, that help with this kind of problem? Something dedicated to efficiently answering the query I am looking for.