Class: RubyAnagrams::Node

Inherits:
Object
  • Object
show all
Includes:
Anagrams, Enumerable, Subtrees
Defined in:
lib/anagrams/node.rb

Overview

A representation of a Node in the trie data structure.

Direct Known Subclasses

Root

Constant Summary

Constants included from Anagrams

Anagrams::SYM_PRIMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Anagrams

#anagrams_by_product, #search_for_anagrams, sym_a_permutations, sym_a_to_product

Methods included from Enumerable

#each

Methods included from Subtrees

#descend, #node_count

Constructor Details

#initialize(symbol = nil, parent = nil) ⇒ Node

Creates a new, non-terminal node with no children.

Parameters:

  • symbol (Symbol, nil) (defaults to: nil)

    the symbol the node represents.

  • parent (Node, nil) (defaults to: nil)

    the parent of the node.



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

#childrenObject (readonly)

Returns the value of attribute children.



10
11
12
# File 'lib/anagrams/node.rb', line 10

def children
  @children
end

#parentObject (readonly)

Returns the value of attribute parent.



10
11
12
# File 'lib/anagrams/node.rb', line 10

def parent
  @parent
end

#symbolObject (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.

Parameters:

  • symbol (Symbol)

Returns:

  • (Node)

    the child node associated with the 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.

Parameters:

  • symbol (Symbol)

    the symbol the node represents.

  • child (Node)

    the node to add as a child.

Returns:

  • (Node)

    the node just added.



27
28
29
# File 'lib/anagrams/node.rb', line 27

def []= symbol, child
  @children[symbol] = child
end

#inspectObject



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?

Returns:

  • (Boolean)

    true if the node is a terminal, false otherwise



45
46
47
# File 'lib/anagrams/node.rb', line 45

def terminal?
  @terminal
end

#wordString

Returns the word the node represents based on its symbol and the symbols of its parents.

Examples:

called on the node representing :e

:a
  :p
    :p
      :l
        :e
#word #=> "apple"

Returns:

  • (String)

    the word the node represents.



59
60
61
# File 'lib/anagrams/node.rb', line 59

def word
  @parent ? "#{@parent.word}#{@symbol}" : "#{@symbol}"
end