I have an in-memory tree structure, resembling a directory tree. That is: each node has a dictionary of named subnodes. I want an efficient method of traversing the tree, from a list or array of names.
If I start at the root node, with a list of subnodes I want to traverse, {"organisms","primates","human","male","John Smith"} and I recursively process one step and pass the remaining sub-list to the subnode, return this.subNodes[myList[0]].GetSubNode(myList.GetRange(1,myList.Count-1)) ... Even though List.GetRange() is a shallow copy, it's still going to create a new list for every level of recursion. The whole operation seems very time and space inefficient.
Or if I try to use an array, then the best method I can find to create a sub-array would be Array.Copy, which is again, a shallow copy. Same problem.
I'm thinking in terms of C, where the head of a list is just a pointer to an object that has another pointer to another object, so getting a sub-list is as simple as following one pointer. Or an array is just a pointer to some memory, so getting a sub-array is as simple as incrementing the pointer. Very time and space efficient. Is there any way to do this in C#?
At present, in C#, I'm thinking I just need to forget the recursion and do some sort of iteration from the top level...
Or I can pass the unmodified array as an argument recursively, along with an int index which I'll increment at each level deeper. Which is fine, except that I'll need to pass another argument to the recursive method call, whose sole purpose is to communicate to the nth recursive method call, "ignore the first n items in the array"... This is fine, it just seems silly if this is the only possible solution (or best possible solution).
Is there a better way?