|
| 1 | +```@meta |
| 2 | +DocTestSetup = :(using DataStructures) |
| 3 | +``` |
| 4 | + |
1 | 5 | # Trie
|
2 | 6 |
|
3 | 7 | An implementation of the Trie data structure. This is an associative
|
4 |
| -structure, with AbstractString keys: |
| 8 | +structure, with iterable keys: |
5 | 9 |
|
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] |
14 | 50 | ```
|
15 | 51 |
|
16 | 52 | Constructors:
|
17 | 53 |
|
18 | 54 | ```julia
|
19 | 55 | 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 |
21 | 57 | Trie(kvs::AbstractVector{(K, V)}) # construct a Trie from the given vector of (key, value) pairs
|
22 | 58 | Trie(kvs::AbstractDict{K, V}) # construct a Trie from the given associative structure
|
23 | 59 | ```
|
24 | 60 |
|
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`. |
27 | 63 | This obviates much of the boilerplate code needed in writing many trie
|
28 | 64 | algorithms. For example, to test whether a trie contains any prefix of a
|
29 |
| -given string, use: |
| 65 | +given string `str`, use: |
30 | 66 |
|
31 | 67 | ```julia
|
32 | 68 | seen_prefix(t::Trie, str) = any(v -> v.is_key, partial_path(t, str))
|
33 | 69 | ```
|
34 | 70 |
|
35 | 71 | `find_prefixes` can be used to find all keys which are prefixes of the given string.
|
36 | 72 |
|
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