Class: SearchTree

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeSearchTree

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

#rootObject

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

Parameters:

  • other (SearchTree)

    SearchTree to be compared with

Returns:

  • (Boolean)

    True if both trees have the same children with the same values



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

Returns:

  • (Boolean)

    True if the tree doesnt have childs and root value is nil, false otherwise



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

Parameters:

  • num (int)

    Depth to be used to find values

Returns:

  • (Array)

    Array with all the values found at depth num



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

Parameters:

  • moves (Array)

    Path to the place to insert the value

  • value (Opening)

    Value to be inserted in the tree



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

Parameters:

  • moves (Array)

    Array with Strings or symbols that represent the path

Returns:

  • (Opening)

    Opening that was found in the tree, Nil otherwise



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

Parameters:

  • moves (Array)

    Array with Strings or symbols that represent the path

Returns:

  • (Array)

    Array with values found



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

#sizeint

Number of not empty Nodes

Returns:

  • (int)

    Total of not empty Nodes in the tree



21
22
23
# File 'lib/chess_openings/search_tree.rb', line 21

def size
  size_helper(@root)
end

#to_sString

String representation of the tree

Returns:

  • (String)

    Representation of the Nodes in the tree



28
29
30
# File 'lib/chess_openings/search_tree.rb', line 28

def to_s
  @root.to_s
end