Suffix tree: Difference between revisions

Content added Content deleted
m (→‎{{header|Racket}}: Added language to template)
(→‎{{header|Racket}}: banana now used, prettier tree output)
Line 108: Line 108:


=={{header|Racket}}==
=={{header|Racket}}==
{{incorrect|Racket|The task has been updated since this code was submitted.}}
See [http://planet.racket-lang.org/package-source/dyoo/suffixtree.plt/1/2/planet-docs/manual/index.html#(def._((planet._main..rkt._(dyoo._suffixtree..plt._1._2))._tree-walk)) Suffix trees with Ukkonen’s algorithm]
See [http://planet.racket-lang.org/package-source/dyoo/suffixtree.plt/1/2/planet-docs/manual/index.html#(def._((planet._main..rkt._(dyoo._suffixtree..plt._1._2))._tree-walk)) Suffix trees with Ukkonen’s algorithm]
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>#lang racket
#lang racket
(require (planet dyoo/suffixtree))
(require (planet dyoo/suffixtree))
(define tree (make-tree))
(define tree (make-tree))
(tree-add! tree (string->label "rosettacode$"))
(tree-add! tree (string->label "banana$"))

(for ([i (in-naturals)]
(define (show-node nd dpth)
[c (node-children (tree-root tree))])
(printf "~a: ~a\n" i (label->string (node-up-label c))))
(define children (node-children nd))
(printf "~a~a ~a~%" (match dpth
</lang>
[(regexp #px"(.*) $" (list _ d)) (string-append d "`")]
Output:
[else else]) (if (null? children) "--" "-+") (label->string (node-up-label nd)))
<lang racket>
(let l ((children children))
0: $
(match children
1: e
((list) (void))
2: de$
((list c) (show-node c (string-append dpth " ")))
3: o
((list c ct ...) (show-node c (string-append dpth " |")) (l ct)))))
4: code$

5: acode$
(show-node (tree-root tree) "")</lang>
6: t

7: settacode$
{{out}}
8: rosettacode$
<pre>-+
</lang>
|-- $
|-+ a
| |-- $
| `-+ na
| |-- $
| `-- na$
|-+ na
| |-- $
| `-- na$
`-- banana$</pre>