Class: SearchTree
- Inherits:
-
Object
- Object
- SearchTree
- Defined in:
- lib/chess_openings/search_tree.rb
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ SearchTree
constructor
A new instance of SearchTree.
- #insert(moves, value) ⇒ Object
- #search(moves) ⇒ Object
- #search_all_with_moves(moves) ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ SearchTree
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
#root ⇒ Object
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
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 |
#size ⇒ Object
13 14 15 |
# File 'lib/chess_openings/search_tree.rb', line 13 def size size_helper(@root) end |
#to_s ⇒ Object
17 18 19 |
# File 'lib/chess_openings/search_tree.rb', line 17 def to_s @root.to_s end |