Class: SearchTree

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

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSearchTree

Returns a new instance of SearchTree.



5
6
7
# File 'lib/chess_openings/search_tree.rb', line 5

def initialize
  @root = Node.new(nil)
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



3
4
5
# File 'lib/chess_openings/search_tree.rb', line 3

def root
  @root
end

Instance Method Details

#==(other) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chess_openings/search_tree.rb', line 21

def ==(other)
  return false unless @root.size == other.root.size
  
  @root.keys.each do |key|
    return false unless other.root.keys.include?(key)
  end

  @root.keys.each do |key|
    return false unless @root.nodes[key] == other.root.nodes[key]
  end

  true
end

#empty?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/chess_openings/search_tree.rb', line 9

def empty?
  @root.is_leaf?
end

#insert(moves, value) ⇒ Object



35
36
37
38
# File 'lib/chess_openings/search_tree.rb', line 35

def insert(moves, value)
  moves = moves_in_symbols(moves)
  insert_helper(moves, value, @root)
end

#search(moves) ⇒ Object



40
41
42
43
# File 'lib/chess_openings/search_tree.rb', line 40

def search(moves)
  moves = moves_in_symbols(moves)
  search_helper(moves, @root)
end

#search_all_with_moves(moves) ⇒ Object



45
46
47
48
49
# File 'lib/chess_openings/search_tree.rb', line 45

def search_all_with_moves(moves)
  moves = moves_in_symbols(moves)
  node = find_node(moves, @root)
  get_all_from_node(node).flatten
end

#sizeObject



13
14
15
# File 'lib/chess_openings/search_tree.rb', line 13

def size
  size_helper(@root)
end

#to_sObject



17
18
19
# File 'lib/chess_openings/search_tree.rb', line 17

def to_s
  @root.to_s
end