Class: Kotoshu::Core::Trie::Node
- Inherits:
-
Object
- Object
- Kotoshu::Core::Trie::Node
- Defined in:
- lib/kotoshu/core/trie/node.rb
Overview
Node in the Trie data structure. Each node represents a character and its children.
Instance Attribute Summary collapse
-
#character ⇒ Object
readonly
Returns the value of attribute character.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#terminal ⇒ Object
readonly
Returns the value of attribute terminal.
Instance Method Summary collapse
-
#add_child(character) ⇒ Node
Add a child node for the given character.
-
#all_children ⇒ Hash
Get all children of this node.
-
#child(character) ⇒ Node?
Get child node for the given character.
-
#child_count ⇒ Integer
Get the number of children.
-
#has_child?(character) ⇒ Boolean
Check if this node has a child for the given character.
-
#has_children? ⇒ Boolean
Check if this node has any children.
-
#initialize(character = "") ⇒ Node
constructor
A new instance of Node.
-
#inspect ⇒ String
Inspect the node.
-
#mark_terminal(payload = nil) ⇒ Object
Mark this node as terminal (end of a word).
-
#terminal? ⇒ Boolean
Check if this node is terminal.
-
#to_s ⇒ String
Convert node to string representation.
Constructor Details
#initialize(character = "") ⇒ Node
Returns a new instance of Node.
11 12 13 14 15 16 |
# File 'lib/kotoshu/core/trie/node.rb', line 11 def initialize(character = "") @character = character @children = {} @terminal = false @payload = nil end |
Instance Attribute Details
#character ⇒ Object (readonly)
Returns the value of attribute character.
9 10 11 |
# File 'lib/kotoshu/core/trie/node.rb', line 9 def character @character end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
9 10 11 |
# File 'lib/kotoshu/core/trie/node.rb', line 9 def children @children end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
9 10 11 |
# File 'lib/kotoshu/core/trie/node.rb', line 9 def payload @payload end |
#terminal ⇒ Object (readonly)
Returns the value of attribute terminal.
9 10 11 |
# File 'lib/kotoshu/core/trie/node.rb', line 9 def terminal @terminal end |
Instance Method Details
#add_child(character) ⇒ Node
Add a child node for the given character.
22 23 24 |
# File 'lib/kotoshu/core/trie/node.rb', line 22 def add_child(character) @children[character] ||= Node.new(character) end |
#all_children ⇒ Hash
Get all children of this node.
60 61 62 |
# File 'lib/kotoshu/core/trie/node.rb', line 60 def all_children @children end |
#child(character) ⇒ Node?
Get child node for the given character.
30 31 32 |
# File 'lib/kotoshu/core/trie/node.rb', line 30 def child(character) @children[character] end |
#child_count ⇒ Integer
Get the number of children.
74 75 76 |
# File 'lib/kotoshu/core/trie/node.rb', line 74 def child_count @children.size end |
#has_child?(character) ⇒ Boolean
Check if this node has a child for the given character.
38 39 40 |
# File 'lib/kotoshu/core/trie/node.rb', line 38 def has_child?(character) @children.key?(character) end |
#has_children? ⇒ Boolean
Check if this node has any children.
67 68 69 |
# File 'lib/kotoshu/core/trie/node.rb', line 67 def has_children? !@children.empty? end |
#inspect ⇒ String
Inspect the node.
88 89 90 |
# File 'lib/kotoshu/core/trie/node.rb', line 88 def inspect to_s end |
#mark_terminal(payload = nil) ⇒ Object
Mark this node as terminal (end of a word).
45 46 47 48 |
# File 'lib/kotoshu/core/trie/node.rb', line 45 def mark_terminal(payload = nil) @terminal = true @payload = payload end |
#terminal? ⇒ Boolean
Check if this node is terminal.
53 54 55 |
# File 'lib/kotoshu/core/trie/node.rb', line 53 def terminal? @terminal end |
#to_s ⇒ String
Convert node to string representation.
81 82 83 |
# File 'lib/kotoshu/core/trie/node.rb', line 81 def to_s "Node('#{@character}', terminal: #{@terminal}, children: #{@children.keys})" end |