Class: NodeOfTree::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#leftObject

Returns the value of attribute left.



3
4
5
# File 'lib/BinarySearchk/node.rb', line 3

def left
  @left
end

#rightObject

Returns the value of attribute right.



3
4
5
# File 'lib/BinarySearchk/node.rb', line 3

def right
  @right
end

#valueObject

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