Class: NodeOfTree::Node
- Inherits:
-
Object
- Object
- NodeOfTree::Node
- Defined in:
- lib/BinarySearchk/node.rb
Instance Attribute Summary collapse
-
#left ⇒ Object
Returns the value of attribute left.
-
#right ⇒ Object
Returns the value of attribute right.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(v) ⇒ Node
constructor
A new instance of Node.
- #insert(v) ⇒ Object
- #insert_left(v) ⇒ Object
- #insert_right(v) ⇒ Object
Constructor Details
#initialize(v) ⇒ Node
Returns a new instance of Node.
5 6 7 8 9 |
# File 'lib/BinarySearchk/node.rb', line 5 def initialize(v) @value = v @left = nil @right = nil end |
Instance Attribute Details
#left ⇒ Object
Returns the value of attribute left.
3 4 5 |
# File 'lib/BinarySearchk/node.rb', line 3 def left @left end |
#right ⇒ Object
Returns the value of attribute right.
3 4 5 |
# File 'lib/BinarySearchk/node.rb', line 3 def right @right end |
#value ⇒ Object
Returns the value of attribute value.
3 4 5 |
# File 'lib/BinarySearchk/node.rb', line 3 def value @value end |
Instance Method Details
#insert(v) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/BinarySearchk/node.rb', line 11 def insert(v) case value <=> v when 1 then insert_left(v) when -1 then insert_right(v) when 0 then end end |
#insert_left(v) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/BinarySearchk/node.rb', line 19 def insert_left(v) if left.nil? self.left = Node.new(v) else left.insert(v) end end |
#insert_right(v) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/BinarySearchk/node.rb', line 27 def insert_right(v) if right.nil? self.right = Node.new(v) else right.insert(v) end end |