Suffix tree: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: syntax coloured, made p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 23: | Line 23: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">T Node |
||
String sub |
String sub |
||
[Int] ch |
[Int] ch |
||
Line 86: | Line 86: | ||
f(0, ‘’) |
f(0, ‘’) |
||
SuffixTree(‘banana$’).visualize()</ |
SuffixTree(‘banana$’).visualize()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 105: | Line 105: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 214: | Line 214: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>+ |
<pre>+ |
||
Line 230: | Line 230: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="cpp">#include <functional> |
||
#include <iostream> |
#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 334: | Line 334: | ||
int main() { |
int main() { |
||
SuffixTree("banana$").visualize(); |
SuffixTree("banana$").visualize(); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>+ |
<pre>+ |
||
Line 350: | Line 350: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="d">import std.stdio; |
||
struct Node { |
struct Node { |
||
Line 442: | Line 442: | ||
void main() { |
void main() { |
||
SuffixTree("banana$").visualize(); |
SuffixTree("banana$").visualize(); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>┐ |
<pre>┐ |
||
Line 458: | Line 458: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
Vis function from [[Visualize_a_tree#Unicode]]. |
Vis function from [[Visualize_a_tree#Unicode]]. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 543: | Line 543: | ||
} |
} |
||
f(0, "") |
f(0, "") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 563: | Line 563: | ||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang="j">classify=: {.@> </. ] |
||
build_tree=:3 :0 |
build_tree=:3 :0 |
||
Line 589: | Line 589: | ||
tree=. B=:|:build_tree <\. y |
tree=. B=:|:build_tree <\. y |
||
((1+#y)-each {.tree),}.tree |
((1+#y)-each {.tree),}.tree |
||
)</ |
)</syntaxhighlight> |
||
Task example: |
Task example: |
||
< |
<syntaxhighlight lang="j"> suffix_tree 'banana$' |
||
┌──┬───────┬─┬──┬───┬─┬─┬──┬───┬─┬─┐ |
┌──┬───────┬─┬──┬───┬─┬─┬──┬───┬─┬─┐ |
||
│__│1 │_│_ │2 │4│6│_ │3 │5│7│ |
│__│1 │_│_ │2 │4│6│_ │3 │5│7│ |
||
Line 600: | Line 600: | ||
├──┼───────┼─┼──┼───┼─┼─┼──┼───┼─┼─┤ |
├──┼───────┼─┼──┼───┼─┼─┼──┼───┼─┼─┤ |
||
│ │banana$│a│na│na$│$│$│na│na$│$│$│ |
│ │banana$│a│na│na$│$│$│na│na$│$│$│ |
||
└──┴───────┴─┴──┴───┴─┴─┴──┴───┴─┴─┘</ |
└──┴───────┴─┴──┴───┴─┴─┴──┴───┴─┴─┘</syntaxhighlight> |
||
The first row is the leaf number (_ for internal nodes). |
The first row is the leaf number (_ for internal nodes). |
||
Line 610: | Line 610: | ||
Visualizing, using [[Visualize_a_tree#J|showtree]] and prefixing the substring leading to each leaf with the leaf number (in brackets): |
Visualizing, using [[Visualize_a_tree#J|showtree]] and prefixing the substring leading to each leaf with the leaf number (in brackets): |
||
< |
<syntaxhighlight lang="j">fmttree=: ;@(1&{) showtree~ {: (,~ }.`('[','] ',~":)@.(_>|))each {. |
||
fmttree suffix_tree 'banana$' |
fmttree suffix_tree 'banana$' |
||
Line 620: | Line 620: | ||
├─ na ────────┴─ [5] $ |
├─ na ────────┴─ [5] $ |
||
└─ [7] $ |
└─ [7] $ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="java">import java.util.ArrayList; |
||
import java.util.List; |
import java.util.List; |
||
Line 717: | Line 717: | ||
new SuffixTree("banana$").visualize(); |
new SuffixTree("banana$").visualize(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>┐ |
<pre>┐ |
||
Line 733: | Line 733: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="javascript">class Node { |
||
sub = ''; // a substring of the input string |
sub = ''; // a substring of the input string |
||
children = []; // list of child nodes |
children = []; // list of child nodes |
||
Line 819: | Line 819: | ||
const st = new SuffixTree('banana'); |
const st = new SuffixTree('banana'); |
||
console.log(st.toString());</ |
console.log(st.toString());</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 834: | Line 834: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="julia">import Base.print |
||
mutable struct Node |
mutable struct Node |
||
Line 907: | Line 907: | ||
println(SuffixTree("banana\$")) |
println(SuffixTree("banana\$")) |
||
</ |
</syntaxhighlight> {{out}} |
||
<pre> |
<pre> |
||
┐ |
┐ |
||
Line 924: | Line 924: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.3 |
||
class Node { |
class Node { |
||
Line 1,009: | Line 1,009: | ||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
SuffixTree("banana$").visualize() |
SuffixTree("banana$").visualize() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,028: | Line 1,028: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="nim">type |
||
Tree = seq[Node] |
Tree = seq[Node] |
||
Line 1,098: | Line 1,098: | ||
newTree("banana$").vis()</ |
newTree("banana$").vis()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,115: | Line 1,115: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use Data::Dumper; |
use Data::Dumper; |
||
Line 1,145: | Line 1,145: | ||
return $h; |
return $h; |
||
} |
} |
||
print +Dumper suffix_tree suffixes 'banana$';</ |
print +Dumper suffix_tree suffixes 'banana$';</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$VAR1 = { |
<pre>$VAR1 = { |
||
Line 1,165: | Line 1,165: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|D}} |
{{trans|D}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #000080;font-style:italic;">-- tree nodes are simply {string substr, sequence children_idx}</span> |
<span style="color: #000080;font-style:italic;">-- tree nodes are simply {string substr, sequence children_idx}</span> |
||
Line 1,238: | Line 1,238: | ||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">SuffixTree</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"banana$"</span><span style="color: #0000FF;">)</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">SuffixTree</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"banana$"</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #000000;">visualize</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">visualize</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,256: | Line 1,256: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="python">class Node: |
||
def __init__(self, sub="", children=None): |
def __init__(self, sub="", children=None): |
||
self.sub = sub |
self.sub = sub |
||
Line 1,322: | Line 1,322: | ||
f(0, "") |
f(0, "") |
||
SuffixTree("banana$").visualize()</ |
SuffixTree("banana$").visualize()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>+- |
<pre>+- |
||
Line 1,340: | Line 1,340: | ||
by Danny Yoo for more information on how to use suffix trees in Racket. |
by Danny Yoo for more information on how to use suffix trees in Racket. |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(require (planet dyoo/suffixtree)) |
(require (planet dyoo/suffixtree)) |
||
(define tree (make-tree)) |
(define tree (make-tree)) |
||
Line 1,356: | Line 1,356: | ||
((list c ct ...) (show-node c (string-append dpth " |")) (l ct))))) |
((list c ct ...) (show-node c (string-append dpth " |")) (l ct))))) |
||
(show-node (tree-root tree) "")</ |
(show-node (tree-root tree) "")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,378: | Line 1,378: | ||
The display code is a variant of the [[visualize_a_tree#Raku|visualize a tree]] task code. |
The display code is a variant of the [[visualize_a_tree#Raku|visualize a tree]] task code. |
||
<lang |
<syntaxhighlight lang="raku" line>multi suffix-tree(Str $str) { suffix-tree flat map &flip, [\~] $str.flip.comb } |
||
multi suffix-tree(@a) { |
multi suffix-tree(@a) { |
||
hash |
hash |
||
Line 1,415: | Line 1,415: | ||
} |
} |
||
flat visit($tree, $indent xx 2); |
flat visit($tree, $indent xx 2); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,432: | Line 1,432: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">func suffix_tree(Str t) { |
||
suffix_tree(^t.len -> map { t.substr(_) }) |
suffix_tree(^t.len -> map { t.substr(_) }) |
||
} |
} |
||
Line 1,456: | Line 1,456: | ||
} |
} |
||
say suffix_tree('banana$')</ |
say suffix_tree('banana$')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,478: | Line 1,478: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="ecmascript">class Node { |
||
construct new() { |
construct new() { |
||
_sub = "" // a substring of the input string |
_sub = "" // a substring of the input string |
||
Line 1,568: | Line 1,568: | ||
} |
} |
||
SuffixTree.new("banana$").visualize()</ |
SuffixTree.new("banana$").visualize()</syntaxhighlight> |
||
{{out}} |
{{out}} |