+2  A: 

Do you want to to be able match any arbitrary part of the tree or a subtree running upto some leaf node(s)? IIUC, you are looking at suffix matching.

You can also look at Compact Directed Acyclic Word Graph for ideas.

dirkgently
Yes. Upto the leaves.
Elroy
Look up suffix tree construction.
dirkgently
+1  A: 

If you're not looking for high efficiency, you might want to use a very simple depth-first-search algorithm.

"2,7,2,U,6,5,U,11,U,U,U,5,9,4"

As you can see, i added U commands ("up") so as to show where the next child would be created. Of course you can make this more efficient, but i believe that's a start.

Also, you might want to have a look at Boost.Graph (BGL) for implementation.

Benoît
I know that a traversal of the sub-tree would give out a unique string representation. I was actually trying to find out if there exists some other way to do it than creating string representations, something like creation of a data structure representation.
Elroy
+2  A: 

What's wrong with the parentheses notation like you used in your question?

Maurice Perry
As Benoit suggested, I could use a string representation, but, I'm trying to look for a data structure representation to actually depict the same. Any ideas?
Elroy
Well a tree is itself a data structure. Do you mean a binary stream?
Maurice Perry
What's the best way to say (2,7,(5,6,11)) is indicated by id 'x', (5,9,4) is indicated by id 'y' where 'x' and 'y' are unique identifiers?
Elroy
A map having the 'traversal path through the sub-tree' could be considered as key and the PATH ID could be taken as the resultant value. I don't really have any other good way of indicating the traversal path except the string representation. Do you?
Elroy
Well the traversal path can be represented by a string of bits, 0 meaning 'left' and 1 meaning 'right'.
Maurice Perry
I really appreciate your help, but it's not exactly what I'm looking for. Maybe I'm not sounding clear. Anyways.
Elroy
+2  A: 

I would make a hash value (in some Rabin-Karp fashion) based on the nodes' IDs and position in the tree, ie:

long h = 0
for each node in sub tree:
    h ^= node.id << (node.depth % 30)

in pseudo code. The downside is that different subtrees may yield the same hash value. But the advantage is that it is fast to compare hash values, and when match is found you can further investige the actual sub tree for equality.

Cecil Has a Name