Suffix tree: Difference between revisions
→{{header|Racket}}: banana now used, prettier tree output
m (→{{header|Racket}}: Added language to template) |
(→{{header|Racket}}: banana now used, prettier tree output) |
||
Line 108:
=={{header|Racket}}==
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.
<lang racket>#lang racket
(require (planet dyoo/suffixtree))
(define tree (make-tree))
(tree-add! tree (string->label "
(define (show-node nd dpth)
[c (node-children (tree-root tree))])▼
(
(printf "~a~a ~a~%" (match dpth
[(regexp #px"(.*) $" (list _ d)) (string-append d "`")]
[else else]) (if (null? children) "--" "-+") (label->string (node-up-label nd)))
(let l ((children children))
(match children
((list) (void))
((list c) (show-node c (string-append dpth " ")))
((list c ct ...) (show-node c (string-append dpth " |")) (l ct)))))
{{out}}
<pre>-+
|-- $
|-+ a
| |-- $
| `-+ na
| |-- $
| `-- na$
|-+ na
| |-- $
| `-- na$
`-- banana$</pre>
|