Class: BinaryDecisionTree::Node
- Inherits:
-
Object
- Object
- BinaryDecisionTree::Node
- Defined in:
- lib/binary_decision_tree/node.rb
Constant Summary collapse
- LEFT =
0
- RIGHT =
1
Instance Attribute Summary collapse
-
#decision ⇒ Object
nil, 0, or 1.
-
#slot ⇒ Object
readonly
bit position.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Instance Method Summary collapse
- #current_depth ⇒ Object
-
#initialize(tree, slot) ⇒ Node
constructor
A new instance of Node.
- #leaf? ⇒ Boolean
- #left ⇒ Object
- #left_position ⇒ Object
- #parent ⇒ Object
- #parent_position ⇒ Object
- #right ⇒ Object
- #right_position ⇒ Object
- #value ⇒ Object
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
#decision ⇒ Object
nil, 0, or 1
6 7 8 |
# File 'lib/binary_decision_tree/node.rb', line 6 def decision @decision end |
#slot ⇒ Object (readonly)
bit position
8 9 10 |
# File 'lib/binary_decision_tree/node.rb', line 8 def slot @slot end |
#tree ⇒ Object (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_depth ⇒ Object
32 33 34 |
# File 'lib/binary_decision_tree/node.rb', line 32 def current_depth Math.log2(slot).floor + 1 end |
#leaf? ⇒ Boolean
28 29 30 |
# File 'lib/binary_decision_tree/node.rb', line 28 def leaf? left.nil? && right.nil? end |
#left ⇒ Object
52 53 54 |
# File 'lib/binary_decision_tree/node.rb', line 52 def left tree.at(left_position) end |
#left_position ⇒ Object
40 41 42 |
# File 'lib/binary_decision_tree/node.rb', line 40 def left_position slot * 2 end |
#parent ⇒ Object
48 49 50 |
# File 'lib/binary_decision_tree/node.rb', line 48 def parent tree.at(parent_position) end |
#parent_position ⇒ Object
36 37 38 |
# File 'lib/binary_decision_tree/node.rb', line 36 def parent_position (slot % 2 == 0 ? slot + 1 : slot) / 2 end |
#right ⇒ Object
56 57 58 |
# File 'lib/binary_decision_tree/node.rb', line 56 def right tree.at(right_position) end |
#right_position ⇒ Object
44 45 46 |
# File 'lib/binary_decision_tree/node.rb', line 44 def right_position left_position + 1 end |