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.



145
146
147
148
# File 'lib/chess_openings/search_tree.rb', line 145

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

Instance Attribute Details

#nodesObject

Returns the value of attribute nodes.



143
144
145
# File 'lib/chess_openings/search_tree.rb', line 143

def nodes
  @nodes
end

#valueObject

Returns the value of attribute value.



143
144
145
# File 'lib/chess_openings/search_tree.rb', line 143

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/chess_openings/search_tree.rb', line 170

def ==(other)
  return false if size != other.size || @value != other.value
  @nodes.keys.each do |key|
    return false unless other.keys.include?(key)
    return false if @nodes[key] != other.nodes[key]
  end
  true
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @value.nil?
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#keysObject



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

def keys
  @nodes.keys
end

#leaf?Boolean

Returns:

  • (Boolean)


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

def leaf?
  @nodes.empty?
end

#sizeObject



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

def size
  @nodes.size
end