Class: Collatz::TreeGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/collatz/tree_graph.rb

Overview

Contains the results of computing the Tree Graph via tree_graph(~). Contains the root node of a tree of TreeGraphNode’s.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_value, max_orbit_distance, p, a, b, create_raw: false, root: nil) ⇒ TreeGraph

Create a new TreeGraph with the root node defined by the inputs.

Parameters:

  • +Integer+

    node_value: The value for which to find the tree graph node reversal.

  • +Integer+

    max_orbit_distance: The maximum distance/orbit/branch length to travel.

  • +Integer+

    p: Modulus used to devide n, iff n is equivalent to (0 mod p).

  • +Integer+

    a: Factor by which to multiply n.

  • +Integer+

    b: Value to add to the scaled value of n.

  • +Boolean+

    create_raw: Used to instruct the initialiser method to take 1:1 inputs, used in testing.

  • TreeGraphNode

    root: A node that will be set to the root of this tree

Raises:

  • FailedSaneParameterCheck If p or a are 0.



134
135
136
137
138
139
140
# File 'lib/collatz/tree_graph.rb', line 134

def initialize(node_value, max_orbit_distance, p, a, b, create_raw: false, root: nil)
  if create_raw && !root.nil?
    @root = root
  else
    @root = TreeGraphNode.new(node_value, max_orbit_distance, p, a, b)
  end
end

Instance Attribute Details

#rootObject (readonly)

The root node of the tree of TreeGraphNode’s.



113
114
115
# File 'lib/collatz/tree_graph.rb', line 113

def root
  @root
end

Instance Method Details

#==(other) ⇒ Object

The equality between TreeGraph’s is determined by the equality check on subtrees. A subtree check will be done on both TreeGraph’s root nodes.

Parameters:

  • TreeGraph

Returns:

  • Boolean true, if both are TreeGraph, with the entire root’s sub-trees being equal.



148
149
150
151
152
153
154
# File 'lib/collatz/tree_graph.rb', line 148

def ==(other)
  # Generic checks
  return false if other.nil?
  return false unless other.is_a?(self.class)
  # Actual check
  self.root.sub_tree_equals(other.root)
end