Given a (m-way) tree T:
A
/ \
B C
/ \ \
D* E* F
/ \ \ \
G H* I J*
With the marked nodes D*, E* H* and J*, is there a fast way to retrieve all marked children under a given node, apart from walking all subtrees or storing all marked children for every node? I.e:
B -> D*, E*, H*
C -> J*
A -> D*, E*, H*, J*
In principle you have a tradeoff between walking the tree and storing lists of your marked values. You mentioned the two extremes, I will give you an example below that sits somewhere in the middle between them.
One idea that comes to mind is storing the next “layer” of marked children at each marked node, that should give a pretty balanced mix between storage and time, so in your example it wouldn’t save much, but for example if you have
You would store
D,IinBandHinDand “empty” markers inH,I,J.To get the list for a node you only have to walk until every branch hits a marked node, so for example, to get the list for
A, you would have to walk fromA->BandA->C->F->J, thenBwould give youI,D,Dwould give youH.You can also think of it as storing trees of marked nodes only alongside with the original tree, in this case, the two trees
Depending on the distribution of marked nodes, you might be able to optimize this idea for your application.