class BinaryDecisionTree::Node

Constants

LEFT

Attributes

decision[RW]
slot[R]
tree[R]

Public Class Methods

new(tree, slot) click to toggle source
# File lib/binary_decision_tree/node.rb, line 11
def initialize(tree, slot)
  @tree = tree
  @slot = slot
  @decision = nil
end

Public Instance Methods

current_depth() click to toggle source
# File lib/binary_decision_tree/node.rb, line 32
def current_depth
  Math.log2(slot).floor + 1
end
leaf?() click to toggle source
# File lib/binary_decision_tree/node.rb, line 28
def leaf?
  left.nil? && right.nil?
end
left() click to toggle source
# File lib/binary_decision_tree/node.rb, line 52
def left
  tree.at(left_position)
end
left_position() click to toggle source
# File lib/binary_decision_tree/node.rb, line 40
def left_position
  slot * 2
end
parent() click to toggle source
# File lib/binary_decision_tree/node.rb, line 48
def parent
  tree.at(parent_position)
end
parent_position() click to toggle source
# File lib/binary_decision_tree/node.rb, line 36
def parent_position
  (slot % 2 == 0 ? slot + 1 : slot) / 2
end
right() click to toggle source
# File lib/binary_decision_tree/node.rb, line 56
def right
  tree.at(right_position)
end
right_position() click to toggle source
# File lib/binary_decision_tree/node.rb, line 44
def right_position
  left_position + 1
end
value() click to toggle source
# File lib/binary_decision_tree/node.rb, line 17
def value
  case decision
    when LEFT
      left.nil? ? left_position : left.value
    when RIGHT
      right.nil? ? right_position : right.value
    else
      nil
  end
end