Class: SearchTree
- Inherits:
-
Object
- Object
- SearchTree
- Defined in:
- lib/chess_openings/search_tree.rb
Overview
Class that is a Tree-like data structure to hold all Openings
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares two trees.
-
#empty? ⇒ Boolean
Check if tree doesnt have child Nodes and root is empty.
-
#get_moves_in_depth(num) ⇒ Array
Get all values at a certain depth.
-
#initialize ⇒ SearchTree
constructor
A new instance of SearchTree.
-
#insert(moves, value) ⇒ Object
Insert new value in SearchTree at the depth of the moves.
-
#search(moves) ⇒ Opening
Search in the tree with the path moves.
-
#search_all_with_moves(moves) ⇒ Array
Search the tree for all the values from the path and values of its children.
-
#size ⇒ int
Number of not empty Nodes.
-
#to_s ⇒ String
String representation of the tree.
Constructor Details
#initialize ⇒ SearchTree
Returns a new instance of SearchTree
8 9 10 |
# File 'lib/chess_openings/search_tree.rb', line 8 def initialize @root = Node.new(nil) end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root
6 7 8 |
# File 'lib/chess_openings/search_tree.rb', line 6 def root @root end |
Instance Method Details
#==(other) ⇒ Boolean
Compares two trees
37 38 39 |
# File 'lib/chess_openings/search_tree.rb', line 37 def ==(other) @root == other.root end |
#empty? ⇒ Boolean
Check if tree doesnt have child Nodes and root is empty
15 16 17 |
# File 'lib/chess_openings/search_tree.rb', line 15 def empty? @root.is_empty? && @root.is_leaf? end |
#get_moves_in_depth(num) ⇒ Array
Get all values at a certain depth
73 74 75 |
# File 'lib/chess_openings/search_tree.rb', line 73 def get_moves_in_depth(num) get_moves_in_depth_helper(num, @root, 0).flatten end |
#insert(moves, value) ⇒ Object
Insert new value in SearchTree at the depth of the moves
45 46 47 48 |
# File 'lib/chess_openings/search_tree.rb', line 45 def insert(moves, value) moves = ChessOpeningsHelper.moves_as_symbols(moves) insert_helper(moves, value, @root) end |
#search(moves) ⇒ Opening
Search in the tree with the path moves
54 55 56 57 |
# File 'lib/chess_openings/search_tree.rb', line 54 def search(moves) moves = ChessOpeningsHelper.moves_as_symbols(moves) search_helper(moves, @root) end |
#search_all_with_moves(moves) ⇒ Array
Search the tree for all the values from the path and values of its children
63 64 65 66 67 |
# File 'lib/chess_openings/search_tree.rb', line 63 def search_all_with_moves(moves) moves = ChessOpeningsHelper.moves_as_symbols(moves) node = find_node(moves, @root) get_all_from_node(node).flatten end |
#size ⇒ int
Number of not empty Nodes
22 23 24 |
# File 'lib/chess_openings/search_tree.rb', line 22 def size size_helper(@root) end |
#to_s ⇒ String
String representation of the tree
29 30 31 |
# File 'lib/chess_openings/search_tree.rb', line 29 def to_s @root.to_s end |