Class: BinaryDecisionTree::Node

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

Constant Summary collapse

LEFT =
0
RIGHT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, slot) ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
# File 'lib/binary_decision_tree/node.rb', line 11

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

Instance Attribute Details

#decisionObject

nil, 0, or 1



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

def decision
  @decision
end

#slotObject (readonly)

bit position



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

def slot
  @slot
end

#treeObject (readonly)

Returns the value of attribute tree.



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

def tree
  @tree
end

Instance Method Details

#current_depthObject



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

def current_depth
  Math.log2(slot).floor + 1
end

#leaf?Boolean

Returns:

  • (Boolean)


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

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

#leftObject



52
53
54
# File 'lib/binary_decision_tree/node.rb', line 52

def left
  tree.at(left_position)
end

#left_positionObject



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

def left_position
  slot * 2
end

#parentObject



48
49
50
# File 'lib/binary_decision_tree/node.rb', line 48

def parent
  tree.at(parent_position)
end

#parent_positionObject



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

def parent_position
  (slot % 2 == 0 ? slot + 1 : slot) / 2
end

#rightObject



56
57
58
# File 'lib/binary_decision_tree/node.rb', line 56

def right
  tree.at(right_position)
end

#right_positionObject



44
45
46
# File 'lib/binary_decision_tree/node.rb', line 44

def right_position
  left_position + 1
end

#valueObject



17
18
19
20
21
22
23
24
25
26
# 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