Class: Rambling::Trie::Nodes::Node
- Inherits:
-
Object
- Object
- Rambling::Trie::Nodes::Node
- Includes:
- Comparable, Compressible, Enumerable, Inspectable, Stringifyable
- Defined in:
- lib/rambling/trie/nodes/node.rb
Overview
A representation of a node in the trie data structure. :reek:MissingSafeMethod { exclude: [ terminal! ] } :reek:RepeatedConditional { max_ifs: 3 }
Direct Known Subclasses
Constant Summary
Constants included from Enumerable
Instance Attribute Summary collapse
-
#children_tree ⇒ Hash<Symbol, Node>
Child nodes tree.
-
#letter ⇒ Symbol?
The corresponding letter(s).
-
#parent ⇒ Node?
Parent node.
Instance Method Summary collapse
-
#[](letter) ⇒ Node
Get Node corresponding to a given letter.
-
#[]=(letter, node) ⇒ Node
Set the Node that corresponds to a given letter.
-
#children ⇒ Array<Node>
Child nodes.
- #delete(letter) ⇒ Node?
-
#first_child ⇒ Node?
First child node.
-
#initialize(letter = nil, parent = nil, children_tree = {}) ⇒ Node
constructor
Creates a new node.
-
#key?(letter) ⇒ Boolean
(also: #has_key?)
Check if a Node‘s children tree contains a given letter.
-
#match_prefix(chars) {|String| ... } ⇒ Enumerator<String>
Returns all words that match a prefix of any length within chars.
-
#partial_word?(chars) ⇒ Boolean
Checks if a path for a set of characters exists in the trie.
-
#root? ⇒ Boolean
Indicates if the current node is the root node.
-
#scan(chars) ⇒ Node
Returns the node that starts with the specified characters.
-
#terminal! ⇒ self
Mark Node as terminal.
-
#terminal? ⇒ Boolean
Indicates if a Node is terminal or not.
-
#word?(chars = []) ⇒ Boolean
Checks if a path for set of characters represents a word in the trie.
Methods included from Inspectable
Methods included from Stringifyable
Methods included from Comparable
Methods included from Enumerable
Methods included from Compressible
Constructor Details
#initialize(letter = nil, parent = nil, children_tree = {}) ⇒ Node
Creates a new node.
38 39 40 41 42 |
# File 'lib/rambling/trie/nodes/node.rb', line 38 def initialize letter = nil, parent = nil, children_tree = {} @letter = letter @parent = parent @children_tree = children_tree end |
Instance Attribute Details
#children_tree ⇒ Hash<Symbol, Node>
Child nodes tree.
28 29 30 |
# File 'lib/rambling/trie/nodes/node.rb', line 28 def children_tree @children_tree end |
#letter ⇒ Symbol? #letter=(letter) ⇒ Symbol?
Returns the corresponding letter(s).
24 25 26 |
# File 'lib/rambling/trie/nodes/node.rb', line 24 def letter @letter end |
#parent ⇒ Node?
Parent node.
32 33 34 |
# File 'lib/rambling/trie/nodes/node.rb', line 32 def parent @parent end |
Instance Method Details
#[](letter) ⇒ Node
Get Node corresponding to a given letter.
131 132 133 |
# File 'lib/rambling/trie/nodes/node.rb', line 131 def [] letter children_tree[letter] end |
#[]=(letter, node) ⇒ Node
Set the Node that corresponds to a given letter.
140 141 142 |
# File 'lib/rambling/trie/nodes/node.rb', line 140 def []= letter, node children_tree[letter] = node end |
#children ⇒ Array<Node>
Child nodes.
46 47 48 |
# File 'lib/rambling/trie/nodes/node.rb', line 46 def children children_tree.values end |
#delete(letter) ⇒ Node?
157 158 159 |
# File 'lib/rambling/trie/nodes/node.rb', line 157 def delete letter children_tree.delete letter end |
#first_child ⇒ Node?
First child node.
52 53 54 55 56 57 58 59 60 |
# File 'lib/rambling/trie/nodes/node.rb', line 52 def first_child return if children_tree.empty? # rubocop:disable Lint/UnreachableLoop children_tree.each_value { |child| return child } # rubocop:enable Lint/UnreachableLoop nil end |
#key?(letter) ⇒ Boolean Also known as: has_key?
Check if a Node‘s children tree contains a given letter.
148 149 150 |
# File 'lib/rambling/trie/nodes/node.rb', line 148 def key? letter children_tree.key? letter end |
#match_prefix(chars) {|String| ... } ⇒ Enumerator<String>
Returns all words that match a prefix of any length within chars.
117 118 119 120 121 122 123 124 125 |
# File 'lib/rambling/trie/nodes/node.rb', line 117 def match_prefix chars return enum_for :match_prefix, chars unless block_given? yield as_word if terminal? children_match_prefix chars do |word| yield word end end |
#partial_word?(chars) ⇒ Boolean
Checks if a path for a set of characters exists in the trie.
88 89 90 91 92 |
# File 'lib/rambling/trie/nodes/node.rb', line 88 def partial_word? chars return true if chars.empty? partial_word_chars? chars end |
#root? ⇒ Boolean
Indicates if the current node is the root node.
64 65 66 |
# File 'lib/rambling/trie/nodes/node.rb', line 64 def root? !parent end |
#scan(chars) ⇒ Node
Returns the node that starts with the specified characters.
107 108 109 110 111 |
# File 'lib/rambling/trie/nodes/node.rb', line 107 def scan chars return self if chars.empty? closest_node chars end |
#terminal! ⇒ self
Mark Node as terminal.
76 77 78 79 |
# File 'lib/rambling/trie/nodes/node.rb', line 76 def terminal! self.terminal = true self end |
#terminal? ⇒ Boolean
Indicates if a Node is terminal or not.
70 71 72 |
# File 'lib/rambling/trie/nodes/node.rb', line 70 def terminal? !!terminal end |
#word?(chars = []) ⇒ Boolean
Checks if a path for set of characters represents a word in the trie.
97 98 99 100 101 |
# File 'lib/rambling/trie/nodes/node.rb', line 97 def word? chars = [] return terminal? if chars.empty? word_chars? chars end |