⚠️ Warning: This is a draft ⚠️

This means it might contain formatting issues, incorrect code, conceptual problems, or other severe issues.

If you want to help to improve and eventually enable this page, please fork RosettaGit's repository and open a merge request on GitHub.

==Different test case?== Can we use maybe "banana" from the WP page instead? "rosettacode" simply doesn't have enough interesting repetitions. --[[User:Ledrug|Ledrug]] ([[User talk:Ledrug|talk]]) 17:58, 17 May 2013 (UTC) :Ok --[[User:Grondilu|Grondilu]] ([[User talk:Grondilu|talk]]) 21:11, 25 May 2013 (UTC)

== definition? ==

The wikipedia definition for a suffix tree currently looks like this:

:The suffix tree for the string $S$ of length $n$ is defined as a tree such that:Gusfield, 1999, p.90. :* the paths from the root to the leaves have a one-to-one relationship with the suffixes of $S$, :* edges spell non-empty strings, :* and all internal nodes (except perhaps the root) have at least two children.

: *{{citation | last1 = Barsky | first1 = Marina | last2 = Stege | first2 = Ulrike | last3 = Thomo | first3 = Alex | last4 = Upton | first4 = Chris | contribution = A new method for indexing genomes using on-disk suffix trees | location = New York, NY, USA | pages = 649–658 | publisher = ACM | title = CIKM '08: Proceedings of the 17th ACM Conference on Information and Knowledge Management | year = 2008}}.

But this can be satisfied by a tree with only a root where each node is a unique suffix. Something is missing from this definition, and that something seems to have something to do with substrings which appear in multiple locations in the string.

What is the missing part of this definition? --[[User:Rdm|Rdm]] ([[User talk:Rdm|talk]]) 16:33, 23 May 2013 (UTC) : On every node, each edge leading away from it must start with a unique letter. So the suffix tree of string "aa\$" can't have two edges "a\$" and "aa\$" leading away from the root node. Instead it must have one edge "a" pointing to a second node, which in turn has two outgoing edges "a\$" and "\$". The reason for unique leading letters is that, otherwise when matching substrings, one can't decide which edge to follow at each node in O(1) time. --[[User:Ledrug|Ledrug]] ([[User talk:Ledrug|talk]]) 13:02, 24 May 2013 (UTC) :: I'm still not seeing how to make this work in O(1) time (interpreting the big-O notation as representing worst case behavior). Let's say our string is an arbitrary length sequence of a single letter (followed by the terminating character). Now we have a single node and the number of edges we have to pick between is O(n) and we need something like an oracle (or luck or a complete scan) to tell us which of them we need to follow. --[[User:Rdm|Rdm]] ([[User talk:Rdm|talk]]) 19:39, 24 May 2013 (UTC)

::: Eh, either an oracle, or a hash table maybe? If you have a single node with n edges going out, but each edge begins with a unique letter, then map these letters to the edges. During a string match, just see what next letter is, and do an O(1) lookup to get the corresponding edge. --[[User:Ledrug|Ledrug]] ([[User talk:Ledrug|talk]]) 15:27, 25 May 2013 (UTC)

:::: If by "each edge begins with a unique letter" you mean "each edge begins with a uniquely different letter" then I agree. However, the example I proposed had each edge begin with a letter which is the same as every other edge (except for the final edge). This letter is in a sense unique (it's the only letter in the example before we decorate it with the final character) but it's probably better to say that we cannot be guaranteed that each edge begins with a different unique letter. Or, more concisely: did you read what I wrote? --[[User:Rdm|Rdm]] ([[User talk:Rdm|talk]]) 15:33, 25 May 2013 (UTC)

::::: Yes, I misread your example. But then, your example is just like the one I provided earlier: "aa\$", only with more "a"s. If you can work out how to find the substring "a\$" and "aa\$" in that example, extending it to arbitrary length is a no brainer. --[[User:Ledrug|Ledrug]] ([[User talk:Ledrug|talk]]) 01:54, 26 May 2013 (UTC)

:::::: I am not sure I can work that out. Let's take aaa\$. The root node can have only one edge leading away from it, which has the label a. I would expect that this node is similar to the tree for 'aa\$' but the wikipedia page claims that except for the root node all nodes must have at least two children, and you have stated that only one edge leading from the aa\$ node is allowed. I cannot think of any implementation which can satisfy both of these constraints. Perhaps because I am stuck in my thinking about this structure, I also cannot think of any useful algorithms that would use it. --[[User:Rdm|Rdm]] ([[User talk:Rdm|talk]]) 05:09, 26 May 2013 (UTC)

:::::: Actually, that banana\$ example - enumerating the edges there, combined with a re-read of the definition and realizing that it's not making any O(1) guarantees at the nodes -- I now think that the suffix tree representation of 'aaaaaaa\$' winds up enumerating all the edges which start with 'a' at a single node -- helps quite a lot. Thank you. --[[User:Rdm|Rdm]] ([[User talk:Rdm|talk]]) 14:08, 26 May 2013 (UTC)

::::::: No. It's not "only one edge leading out is allowed", but "only one edge starting with each letter is allowed". For "aaaa\$", you have: ::::::: 1. from root node, edge '\$' pointing to a leaf node, and edge 'a' pointing to internal node 1; ::::::: 2. from node 1, edge '\$' pointing to a leaf node, and 'a' pointing to internal node 2; ::::::: 3. from node 2, edge '\$' pointing to a leaf node, and 'a' pointing to internal node 3; ::::::: 4. from node 3, edge '\$' pointing to a leaf node, and 'a\$' pointing to a leaf node. ::::::: You can just add more nodes like 2 and 3 if you insert more 'a's to the string. String matching works exactly like a trie lookup (actually, it ''is'' exactly a trie lookup, and it really only takes O(1) time to decide which edge to follow at each node ''unless you don't want to'' (one C implementation referrenced by the WP article stores edges in a linked list, but it could easily have used a hash table or a dynamic array.)

:::::::: Ok, this helped greatly, because it conveyed to me the definition of "edge" (or an important part of that definition). A study of the patricia tree link from the wikipedia page also helped. --[[User:Rdm|Rdm]] ([[User talk:Rdm|talk]]) 13:05, 27 May 2013 (UTC)

== definition (take 2) ==

I am still dissatisfied with the definition of suffix trees. This page refers to wikipedia and the wikipedia page's definition is quoted above.

My current dissatisfaction is that I see no reason, based on the wikipedia definition, to exclude an implementation, for banana, which looks like this:

b-> 'banana\$' a-> 'anana\$', 'ana\$', 'a\$' n-> 'nana\$', 'na\$' \$-> '\$'

But, of course, this differs from the required result for this task. I can probably extract the definition from the example, given enough thought (and perhaps some or all of the implementations suggested on the wikipedia page can be made to match this example), but I would prefer a real definition for this task.

(A perhaps related issue is that the required result suggests that this structure is not a "tree" but a "directed acyclic graph" during construction, though of course that information can be discarded.)