Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements of Trie Structure #756

Merged
merged 7 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/src/trie.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ given string, use:
```julia
seen_prefix(t::Trie, str) = any(v -> v.is_key, partial_path(t, str))
```

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

```julia
t = Trie(["A", "ABC", "ABCD", "BCE"])
find_prefixes(t, "ABCDE") # "A", "ABC", "ABCD"
```
2 changes: 1 addition & 1 deletion src/DataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module DataStructures
export heapify!, heapify, heappop!, heappush!, isheap
export BinaryMinMaxHeap, popmin!, popmax!, popall!

export Trie, subtrie, keys_with_prefix, partial_path
export Trie, subtrie, keys_with_prefix, partial_path, find_prefixes

export LinkedList, Nil, Cons, nil, cons, head, tail, list, filter, cat,
reverse
Expand Down
35 changes: 32 additions & 3 deletions src/trie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,43 @@ end
# since the root of the trie corresponds to a length 0 prefix of str.
function Base.iterate(it::TrieIterator, (t, i) = (it.t, 0))
if i == 0
return it.t, (it.t, 1)
elseif i == length(it.str) + 1 || !(it.str[i] in keys(t.children))
return it.t, (it.t, firstindex(it.str))
elseif i > lastindex(it.str) || !(it.str[i] in keys(t.children))
return nothing
else
t = t.children[it.str[i]]
return (t, (t, i + 1))
return (t, (t, nextind(it.str, i)))
end
end

partial_path(t::Trie, str::AbstractString) = TrieIterator(t, str)
Base.IteratorSize(::Type{TrieIterator}) = Base.SizeUnknown()

"""
find_prefixes(t::Trie, str::AbstractString)

Find all keys from the `Trie` that are prefix of the given string

# Examples
```julia-repl
julia> t = Trie(["A", "ABC", "ABCD", "BCE"])

julia> find_prefixes(t, "ABCDE")
3-element Vector{AbstractString}:
"A"
"ABC"
"ABCD"
```
"""
function find_prefixes(t::Trie, str::AbstractString)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this requires a docstring

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in the latest commit

prefixes = AbstractString[]
it = partial_path(t, str)
idx = 0
for t in it
if t.is_key
push!(prefixes, str[firstindex(str):idx])
end
idx = nextind(str, idx)
end
return prefixes
end
26 changes: 26 additions & 0 deletions test/test_trie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,30 @@
@test collect(partial_path(t, "ro")) == [t0, t1, t2]
@test collect(partial_path(t, "roa")) == [t0, t1, t2]
end

@testset "partial_path iterator non-ascii" begin
t = Trie(["東京都"])
t0 = t
t1 = t0.children['東']
t2 = t1.children['京']
t3 = t2.children['都']
@test collect(partial_path(t, "西")) == [t0]
@test collect(partial_path(t, "東京都")) == [t0, t1, t2, t3]
@test collect(partial_path(t, "東京都渋谷区")) == [t0, t1, t2, t3]
@test collect(partial_path(t, "東京")) == [t0, t1, t2]
@test collect(partial_path(t, "東京スカイツリー")) == [t0, t1, t2]
end

@testset "find_prefixes" begin
t = Trie(["A", "ABC", "ABD", "BCD"])
prefixes = find_prefixes(t, "ABCDE")
@test prefixes == ["A", "ABC"]
end

@testset "find_prefixes non-ascii" begin
t = Trie(["東京都", "東京都渋谷区", "東京都新宿区"])
prefixes = find_prefixes(t, "東京都渋谷区東")
@test prefixes == ["東京都", "東京都渋谷区"]
end

end # @testset Trie