Class: BinaryDecisionTree::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/binary_decision_tree/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, slot, decision: nil) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
# File 'lib/binary_decision_tree/node.rb', line 7

def initialize(tree, slot, decision: nil)
  @tree = tree
  @slot = slot
  @decision = decision
end

Instance Attribute Details

#decisionObject

nil, 0, or 1



2
3
4
# File 'lib/binary_decision_tree/node.rb', line 2

def decision
  @decision
end

#slotObject (readonly)

bit position



4
5
6
# File 'lib/binary_decision_tree/node.rb', line 4

def slot
  @slot
end

#treeObject (readonly)

Returns the value of attribute tree.



5
6
7
# File 'lib/binary_decision_tree/node.rb', line 5

def tree
  @tree
end

Instance Method Details

#leaf?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/binary_decision_tree/node.rb', line 24

def leaf?
  left.nil? && right.nil?
end

#leftObject



36
37
38
# File 'lib/binary_decision_tree/node.rb', line 36

def left
  tree.at(left_position)
end

#left_positionObject



28
29
30
# File 'lib/binary_decision_tree/node.rb', line 28

def left_position
  slot * 2
end

#rightObject



40
41
42
# File 'lib/binary_decision_tree/node.rb', line 40

def right
  tree.at(right_position)
end

#right_positionObject



32
33
34
# File 'lib/binary_decision_tree/node.rb', line 32

def right_position
  left_position + 1
end

#valueObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/binary_decision_tree/node.rb', line 13

def value
  case decision
    when 0
      left.nil? ? left_position : left.value
    when 1
      right.nil? ? right_position : right.value
    else
      nil
  end
end