Here’s a restatement of the rather cryptic title question:
Suppose we have a Prototype tree that has been built, that contains all the info on the structure of the tree and the generic description of each node. Now we want to create instances of this tree with elements that contain extra unique data. Let’s call these Concrete trees.
The only difference between Concrete and Prototype trees is the extra data in the nodes of the Concrete tree. Supposing each node of a Concrete tree has a pointer/link to the corresponding element in the Prototype tree for generic information about the node, but no parent/child information of its own:
Is it possible to traverse the Concrete tree?
In particular, given a starting node in the Concrete tree, and a path through the Prototype tree, is it possible to efficiently get the corresponding node in the Concrete tree? There can be many Concrete trees, so a link back from Prototype tree is not possible.
Even though I might not need to optimize things to such an extent in my code, this is still an interesting problem!
Thanks in advance!
NOTE: There are no restrictions on the branching factor of the tree- a node can have between one and hundreds of children.
Extra ramblings/ideas:
The reason I ask, is that it seems like it would be a waste to copy parent/child information each time a new instance of a Concrete tree is created, since this structure is identical to the Prototype tree. In my particular case, children are identified by string names, so I have to store a string-to-pointer hash at each node. There can be many instances of Concrete trees, and duplicating this hash seems like a huge waste of space.
As a first idea, perhaps the path could be somehow hashed into an int or something that compactly identifies an element (not a string, since that’s too big), which is then used to look up concrete elements in hashes for each Concrete tree?
Once created, will the prototype tree ever change (i.e. will nodes ever be inserted or removed)?
If not, you could consider array-backed trees (i.e. child/parent links are represented by array indices, not raw pointers), and use consistent indexing for your concrete trees. That way, it’s trivial to map from concrete to prototype, and vice versa.