Suffix tree: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
m (syntax highlighting fixup automation)
Line 23: Line 23:
{{trans|Python}}
{{trans|Python}}


<lang 11l>T Node
<syntaxhighlight lang="11l">T Node
String sub
String sub
[Int] ch
[Int] ch
Line 86: Line 86:
f(0, ‘’)
f(0, ‘’)


SuffixTree(‘banana$’).visualize()</lang>
SuffixTree(‘banana$’).visualize()</syntaxhighlight>


{{out}}
{{out}}
Line 105: Line 105:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|C++}}
{{trans|C++}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 214: Line 214:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>+
<pre>+
Line 230: Line 230:
=={{header|C++}}==
=={{header|C++}}==
{{trans|D}}
{{trans|D}}
<lang cpp>#include <functional>
<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();
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>+
<pre>+
Line 350: Line 350:
=={{header|D}}==
=={{header|D}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang D>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


struct Node {
struct Node {
Line 442: Line 442:
void main() {
void main() {
SuffixTree("banana$").visualize();
SuffixTree("banana$").visualize();
}</lang>
}</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]].
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 543: Line 543:
}
}
f(0, "")
f(0, "")
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 563: Line 563:
Implementation:
Implementation:


<lang J>classify=: {.@> </. ]
<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
)</lang>
)</syntaxhighlight>


Task example:
Task example:


<lang J> suffix_tree 'banana$'
<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$│$│$│
└──┴───────┴─┴──┴───┴─┴─┴──┴───┴─┴─┘</lang>
└──┴───────┴─┴──┴───┴─┴─┴──┴───┴─┴─┘</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):


<lang J>fmttree=: ;@(1&{) showtree~ {: (,~ }.`('[','] ',~":)@.(_>|))each {.
<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}}
<lang Java>import java.util.ArrayList;
<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();
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>┐
<pre>┐
Line 733: Line 733:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
{{trans|Java}}
{{trans|Java}}
<lang JavaScript>class Node {
<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());</lang>
console.log(st.toString());</syntaxhighlight>


{{out}}
{{out}}
Line 834: Line 834:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Go}}
{{trans|Go}}
<lang julia>import Base.print
<syntaxhighlight lang="julia">import Base.print


mutable struct Node
mutable struct Node
Line 907: Line 907:


println(SuffixTree("banana\$"))
println(SuffixTree("banana\$"))
</lang> {{out}}
</syntaxhighlight> {{out}}
<pre>
<pre>
Line 924: Line 924:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Go}}
{{trans|Go}}
<lang scala>// version 1.1.3
<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()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,028: Line 1,028:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Go}}
{{trans|Go}}
<lang Nim>type
<syntaxhighlight lang="nim">type


Tree = seq[Node]
Tree = seq[Node]
Line 1,098: Line 1,098:




newTree("banana$").vis()</lang>
newTree("banana$").vis()</syntaxhighlight>


{{out}}
{{out}}
Line 1,115: Line 1,115:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang Perl>use strict;
<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$';</lang>
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}}
<!--<lang Phix>(phixonline)-->
<!--<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,256: Line 1,256:
=={{header|Python}}==
=={{header|Python}}==
{{trans|D}}
{{trans|D}}
<lang python>class Node:
<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()</lang>
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.


<lang racket>#lang 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) "")</lang>
(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 perl6>multi suffix-tree(Str $str) { suffix-tree flat map &flip, [\~] $str.flip.comb }
<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);
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,432: Line 1,432:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>func suffix_tree(Str t) {
<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$')</lang>
say suffix_tree('banana$')</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,478: Line 1,478:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang ecmascript>class Node {
<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()</lang>
SuffixTree.new("banana$").visualize()</syntaxhighlight>


{{out}}
{{out}}