Class: RubyTrie::TrieNode
- Inherits:
-
Tree::TreeNode
- Object
- Tree::TreeNode
- RubyTrie::TrieNode
- Defined in:
- lib/ruby_trie.rb
Overview
A TrieNode is a node in the Trie derives from the TreeNode class You should only need this class if you use Tree methods to walk the tree and such-like
Instance Method Summary collapse
-
#add_node(string, value) ⇒ Object
adds adds a node at a particular point if the node already exists, then it will overwrite the value :call-seq: add_node(string, value) -> TrieContent.
-
#get_node(string) ⇒ Object
return the TreeNode corresponding to a given string :call-seq: get_node(string) -> TrieNode.
-
#get_node_value(string) ⇒ Object
gets the value at a node returns nil if the node does not exist :call-seq: get_node_value(string) -> Object.
Instance Method Details
#add_node(string, value) ⇒ Object
adds adds a node at a particular point if the node already exists, then it will overwrite the value :call-seq: add_node(string, value) -> TrieContent
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ruby_trie.rb', line 52 def add_node(string, value) current_node = self.root current_string = '' chars = string.split('') chars.each do |c| current_string << c child = current_node[c] unless child child = TrieNode.new(c) current_node.add(child) end current_node = child end current_node.content = TrieContent.new(string, value) end |
#get_node(string) ⇒ Object
return the TreeNode corresponding to a given string :call-seq: get_node(string) -> TrieNode
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ruby_trie.rb', line 74 def get_node(string) current_node = self.root current_string = '' chars = string.split('') chars.each do |c| current_string << c current_node = current_node[c] break unless current_node end current_node end |
#get_node_value(string) ⇒ Object
gets the value at a node returns nil if the node does not exist :call-seq: get_node_value(string) -> Object
92 93 94 95 |
# File 'lib/ruby_trie.rb', line 92 def get_node_value(string) node = get_node(string) node&.content&.value end |