views:

78

answers:

4

Apart from recursively querying each node, are there any other options for searching for a node given some identifier?

A: 

Only if you implement it. :-(

(The pattern itself says very little about its implementation, different tools/platforms make a big difference.)

Richard
A: 

You can use any search you want.

The only thing the composite pattern says is that 1 object, or a collection of those objects should implement the same interface.

What happens inside that implementation is not relevant to the composite pattern.

Brian R. Bondy
+1  A: 

Visitor is always fun for examining nodes within a composite object.

If they have unique id's you might want to accumulate an index with direct references to each piece of the Composite structure.

If, on the other hand, identifiers can be reused down within the composite structure, you may have to resort to XPath-like searches.

S.Lott
+1  A: 

Well, recursion is the most natural thing if you're using a tree. Other than that, you could maintain a list or a collection outside of the tree, which seems superfluous, or you could simply linearly iterate over the list of tree nodes, which is probably going to be slower, but at least removes any traversal-specific code from the nodes, if that's of any value.

I can't see how any kind of specific tree query (e.g. xpath) that's looking for all nodes of the form a.b.c could be doing anything other than just traversing at level a for b's, and traversing the b's for c's, in other words, some sort of filtered recursive tree traversal.

Steve B.