Class: SearchTree::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/chess_openings/search_tree.rb

Overview

Class that represents a node of a tree data structure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Node

Returns a new instance of Node



153
154
155
156
# File 'lib/chess_openings/search_tree.rb', line 153

def initialize(value)
  @value = value
  @nodes = {}
end

Instance Attribute Details

#nodesObject

Returns the value of attribute nodes



151
152
153
# File 'lib/chess_openings/search_tree.rb', line 151

def nodes
  @nodes
end

#valueObject

Returns the value of attribute value



151
152
153
# File 'lib/chess_openings/search_tree.rb', line 151

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/chess_openings/search_tree.rb', line 178

def ==(other)
  return false if self.size != other.size || @value != other.value

  @nodes.keys.each do |key|
    return false unless other.keys.include?(key)
  end

  @nodes.keys.each do |key|
    return false if @nodes[key] != other.nodes[key]
  end

  true
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/chess_openings/search_tree.rb', line 174

def include?(key)
  @nodes.keys.include?(key)
end

#is_empty?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/chess_openings/search_tree.rb', line 158

def is_empty?
  @value.nil?
end

#is_leaf?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/chess_openings/search_tree.rb', line 162

def is_leaf?
  @nodes.empty?
end

#keysObject



170
171
172
# File 'lib/chess_openings/search_tree.rb', line 170

def keys
  @nodes.keys
end

#sizeObject



166
167
168
# File 'lib/chess_openings/search_tree.rb', line 166

def size
  @nodes.size
end