Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

up vote 1 down vote accepted

There is a LinkedList implementation in .net, which allows you to pass next LinkedListNode into method.

Other than that, approach with indexes is also fine - at least it is not consuming extra memory.

There is also a way to pass pointer to array element, like in C. But this would force you to compile program in unsafe mode, which is often undesirable.

share|improve this answer
Huh. I had no idea there was a LinkedList class in addition to the List class. For the present purpose, this answer absolutely works, thank you. But I'm going to wait a little bit before marking this as the answer, to see if anyone else suggests any good way of creating a sublist or subarray. – Edward Ned Harvey Apr 10 at 23:14
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.