Skip to content

Commit 20d807a

Browse files
committed
Update trie docs
1 parent d3884d1 commit 20d807a

File tree

1 file changed

+62
-17
lines changed

1 file changed

+62
-17
lines changed

docs/src/trie.md

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,85 @@
1+
```@meta
2+
DocTestSetup = :(using DataStructures)
3+
```
4+
15
# Trie
26

37
An implementation of the Trie data structure. This is an associative
4-
structure, with AbstractString keys:
8+
structure, with iterable keys:
59

6-
```julia
7-
t = Trie{Int}()
8-
t["Rob"] = 42
9-
t["Roger"] = 24
10-
haskey(t, "Rob") # true
11-
get(t, "Rob", nothing) # 42
12-
keys(t) # "Rob", "Roger"
13-
keys(subtrie(t, "Ro")) # "b", "ger"
10+
```jldoctest
11+
julia> t = Trie{Char,Int}();
12+
13+
14+
julia> t["Rob"] = 42;
15+
16+
17+
julia> t["Roger"] = 24;
18+
19+
20+
julia> haskey(t, "Rob")
21+
true
22+
23+
julia> get(t, "Rob", nothing)
24+
42
25+
26+
julia> keys(t)
27+
2-element Vector{String}:
28+
"Roger"
29+
"Rob"
30+
31+
julia> keys(subtrie(t, "Ro"))
32+
2-element Vector{String}:
33+
"ger"
34+
"b"
35+
```
36+
37+
Note that the keys don't need to be `String`s:
38+
39+
```jldoctest
40+
julia> t = Trie{Int,Char}();
41+
42+
julia> t[1:3] = 'a';
43+
44+
julia> t[[2,3,5]] = 'b';
45+
46+
julia> keys(t)
47+
2-element Vector{Vector{Int64}}:
48+
[2, 3, 5]
49+
[1, 2, 3]
1450
```
1551

1652
Constructors:
1753

1854
```julia
1955
Trie(keys, values) # construct a Trie with the given keys and values
20-
Trie(keys) # construct a Trie{Void} with the given keys and with values = nothing
56+
Trie(keys) # construct a Trie{K,Nothing} with the given keys and with values = nothing
2157
Trie(kvs::AbstractVector{(K, V)}) # construct a Trie from the given vector of (key, value) pairs
2258
Trie(kvs::AbstractDict{K, V}) # construct a Trie from the given associative structure
2359
```
2460

25-
This package also provides an iterator `partial_path(t::Trie, str)` for looping
26-
over all the nodes encountered in searching for the given string `str`.
61+
This package also provides an iterator `partial_path(t::Trie, prefix)` for looping
62+
over all the nodes encountered in searching for the given `prefix`.
2763
This obviates much of the boilerplate code needed in writing many trie
2864
algorithms. For example, to test whether a trie contains any prefix of a
29-
given string, use:
65+
given string `str`, use:
3066

3167
```julia
3268
seen_prefix(t::Trie, str) = any(v -> v.is_key, partial_path(t, str))
3369
```
3470

3571
`find_prefixes` can be used to find all keys which are prefixes of the given string.
3672

37-
```julia
38-
t = Trie(["A", "ABC", "ABCD", "BCE"])
39-
find_prefixes(t, "ABCDE") # "A", "ABC", "ABCD"
40-
```
73+
```jldoctest
74+
julia> t = Trie(["A", "ABC", "ABCD", "BCE"]);
75+
76+
julia> find_prefixes(t, "ABCDE")
77+
3-element Vector{String}:
78+
"A"
79+
"ABC"
80+
"ABCD"
81+
```
82+
83+
```@meta
84+
DocTestSetup = nothing
85+
```

0 commit comments

Comments
 (0)