mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
Merge pull request #69 from vbrazo/add-trie-data-structure-example
Trie data structure: Search string
This commit is contained in:
commit
4e1438728d
2 changed files with 77 additions and 0 deletions
|
@ -13,6 +13,8 @@
|
|||
* Linked Lists
|
||||
* [Double List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/double_list.rb)
|
||||
* [Single List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/single_list.rb)
|
||||
* Tries
|
||||
* [Trie](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/tries/trie.rb)
|
||||
|
||||
## Discrete Mathematics
|
||||
* [Euclidean Gcd](https://github.com/TheAlgorithms/Ruby/blob/master/discrete_mathematics/euclidean_gcd.rb)
|
||||
|
|
75
data_structures/tries/trie.rb
Normal file
75
data_structures/tries/trie.rb
Normal file
|
@ -0,0 +1,75 @@
|
|||
# A Trie (prefix tree) is a kind of search tree used to provide quick lookup
|
||||
# of words/patterns in a set of words. A basic Trie however has O(n^2)
|
||||
# space complexity making it impractical in practice.
|
||||
# It however provides O(max(search_string, length of longest word))
|
||||
# lookup time making it an optimal approach when space is not an issue.
|
||||
|
||||
class Node
|
||||
attr_reader :value, :next
|
||||
attr_accessor :word
|
||||
|
||||
def initialize(value)
|
||||
@value = value
|
||||
@word = false
|
||||
@next = []
|
||||
end
|
||||
end
|
||||
|
||||
class Trie
|
||||
def initialize
|
||||
@root = Node.new('*')
|
||||
end
|
||||
|
||||
def insert_many(word)
|
||||
letters = word.chars
|
||||
base = @root
|
||||
|
||||
letters.each do |letter|
|
||||
base = insert(letter, base.next)
|
||||
end
|
||||
end
|
||||
|
||||
def include?(word)
|
||||
letters = word.chars
|
||||
base = @root
|
||||
|
||||
letters.all? do |letter|
|
||||
base = find(letter, base.next)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# check if it already exists
|
||||
# if not add character to node
|
||||
def insert(character, trie)
|
||||
found = trie.find do |n|
|
||||
n.value == character
|
||||
end
|
||||
|
||||
add_node(character, trie) unless found
|
||||
end
|
||||
|
||||
def add_node(character, trie)
|
||||
Node.new(character).tap do |new_node|
|
||||
trie << new_node
|
||||
end
|
||||
end
|
||||
|
||||
def find(character, trie)
|
||||
trie.find do |n|
|
||||
n.value == character
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
trie = Trie.new
|
||||
trie.insert_many('Dogs')
|
||||
trie.insert_many('South')
|
||||
trie.insert_many('Cape Town')
|
||||
|
||||
puts trie.include?('Cape Town')
|
||||
# => true
|
||||
|
||||
puts trie.include?('not presented')
|
||||
# => false
|
Loading…
Reference in a new issue