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