Class: RubyAnagrams::Node
- Inherits:
-
Object
- Object
- RubyAnagrams::Node
- Includes:
- Anagrams, Enumerable, Subtrees
- Defined in:
- lib/anagrams/node.rb
Overview
A representation of a Node in the trie data structure.
Direct Known Subclasses
Constant Summary
Constants included from Anagrams
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Instance Method Summary collapse
-
#[](symbol) ⇒ Node
Returns the child node associated with a symbol.
-
#[]=(symbol, child) ⇒ Node
Adds a new child node representing a symbol to this node's children.
-
#initialize(symbol = nil, parent = nil) ⇒ Node
constructor
Creates a new, non-terminal node with no children.
- #inspect ⇒ Object
-
#terminal! ⇒ Object
Sets the node as a terminal.
-
#terminal? ⇒ Boolean
Is the node a terminal?.
-
#word ⇒ String
Returns the word the node represents based on its symbol and the symbols of its parents.
Methods included from Anagrams
#anagrams_by_product, #search_for_anagrams, sym_a_permutations, sym_a_to_product
Methods included from Enumerable
Methods included from Subtrees
Constructor Details
#initialize(symbol = nil, parent = nil) ⇒ Node
Creates a new, non-terminal node with no children.
16 17 18 19 20 21 |
# File 'lib/anagrams/node.rb', line 16 def initialize symbol = nil, parent = nil @symbol = symbol @parent = parent @children = {} @terminal = false end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
10 11 12 |
# File 'lib/anagrams/node.rb', line 10 def children @children end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
10 11 12 |
# File 'lib/anagrams/node.rb', line 10 def parent @parent end |
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
10 11 12 |
# File 'lib/anagrams/node.rb', line 10 def symbol @symbol end |
Instance Method Details
#[](symbol) ⇒ Node
Returns the child node associated with a symbol.
34 35 36 |
# File 'lib/anagrams/node.rb', line 34 def [] symbol @children[symbol] end |
#[]=(symbol, child) ⇒ Node
Adds a new child node representing a symbol to this node's children.
27 28 29 |
# File 'lib/anagrams/node.rb', line 27 def []= symbol, child @children[symbol] = child end |
#inspect ⇒ Object
63 64 65 66 67 68 |
# File 'lib/anagrams/node.rb', line 63 def inspect "<#{self.class}:#{self.object_id}, "\ "@symbol=#{@symbol ? ":#{@symbol}" : "nil"}, "\ "@word=#{terminal? ? word : "nil"}, "\ "@children=#{@children.each_key.to_a}>" end |
#terminal! ⇒ Object
Sets the node as a terminal.
39 40 41 |
# File 'lib/anagrams/node.rb', line 39 def terminal! @terminal = true end |
#terminal? ⇒ Boolean
Is the node a terminal?
45 46 47 |
# File 'lib/anagrams/node.rb', line 45 def terminal? @terminal end |
#word ⇒ String
Returns the word the node represents based on its symbol and the symbols of its parents.
59 60 61 |
# File 'lib/anagrams/node.rb', line 59 def word @parent ? "#{@parent.word}#{@symbol}" : "#{@symbol}" end |