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.
7 8 9 |
# File 'lib/chess_openings/search_tree.rb', line 7 def initialize @root = Node.new(nil) end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
5 6 7 |
# File 'lib/chess_openings/search_tree.rb', line 5 def root @root end |
Instance Method Details
#==(other) ⇒ Boolean
Compares two trees
36 37 38 |
# File 'lib/chess_openings/search_tree.rb', line 36 def ==(other) @root == other.root end |
#empty? ⇒ Boolean
Check if tree doesnt have child Nodes and root is empty
14 15 16 |
# File 'lib/chess_openings/search_tree.rb', line 14 def empty? @root.empty? && @root.leaf? end |
#get_moves_in_depth(num) ⇒ Array
Get all values at a certain depth
72 73 74 |
# File 'lib/chess_openings/search_tree.rb', line 72 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
44 45 46 47 |
# File 'lib/chess_openings/search_tree.rb', line 44 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
53 54 55 56 |
# File 'lib/chess_openings/search_tree.rb', line 53 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
62 63 64 65 66 |
# File 'lib/chess_openings/search_tree.rb', line 62 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
21 22 23 |
# File 'lib/chess_openings/search_tree.rb', line 21 def size size_helper(@root) end |
#to_s ⇒ String
String representation of the tree
28 29 30 |
# File 'lib/chess_openings/search_tree.rb', line 28 def to_s @root.to_s end |