Suffix tree: Difference between revisions

update task description.
m (→‎{{header|Perl}}: updating output as well)
(update task description.)
Line 1:
{{draft task}}
A [[wp:suffix tree|suffix tree]] is a data structure commonly used in [[wp:string algorithms|string algorithms]]. Basically, for any string, its suffix tree is a [[wp:rooted tree|rooted tree]] where each edge is labelled, and where the concatenation of all the labels from the root to a leaf uniquely identifies a suffix of the string.
 
Given a string S of length n, its suffix tree is a tree T such that:
<!-- TODO: more detailed explanation, + maybe a graphical example -->
 
* T has exactly n leaves numbered from 1 to n.
For this task, build the suffix tree of the string "banana$", and show that its edges are:
* Except for the root, every internal node has at least two children.
* Each edge of T is labelled with a non-empty substring of S.
* No two edges starting out of a node can have string labels beginning with the same character.
* The string obtained by concatenating all the string labels found on the path from the root to leaf i spells out suffix S[i..n], for i from 1 to n.
 
Such a tree does not exist for any string. To ensure existence, a character that is not found in S must be appended at its end. The character '$' is traditionally used for this purpose.
<pre>$ $ $ $ a banana$ na na na$ na$</pre>
 
AddingFor athis $task, signbuild atand display the endsuffix tree of the string is"banana$". a commonDisplaying practice.the tree Herecan webe shalldone notusing trythe tocode explainfrom the rationale[[visualize behinda ittree]] task, thoughbut any other convenient method is accepted.
 
There are several ways to implement the tree data structure, for instance how edges labels should be encoded. Latitude is given in this matter.
Extra-credit: use the [[visualize a tree]] task in order to show the whole tree.
 
The computation time for an efficient algorithm should be <math>O(n)</math>, but such an algorithm might be difficult to implement. An easier, <math>O(n^2)</math> algorithm is accepted.
 
=={{header|Perl}}==
1,935

edits