Recursive map datastructure/crate

I'm looking for a datastructure (and hopefully a crate) which implements something like a recursive HashMap - where the key of the hashmap can either contain a value, or contain another key, to be considered as it's ancestor. The root key has no ancestor. All keys must have a singe ancestor, so the keys form a tree like structure.

A search for key would be recursive till we find a value, or return a None.

I've working on a naive implementation wrapping indextree (for recursive key lookup) and HashMap (for value lookup) into a utility class.

Please let me know if you can suggest anything. My background is not in computer science, so apologies if this is not clear.

thanks!

I would just use a HashMap with an enum as value.

enum MyValue<K, V> {
    NextKey(K),
    Value(V),
}
2 Likes

Ohhh .. beautiful! When efficiency is simple and elegant... thank you, I've just got a little satori! :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.