Class: Trees::Binary::Node

Inherits:
Object
  • Object
show all
Includes:
NodeHelpers, SearchHelpers
Defined in:
lib/trees/binary/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SearchHelpers

#include?

Methods included from NodeHelpers

#inspect

Constructor Details

#initialize(data, parent: nil) ⇒ Node

Returns a new instance of Node.



13
14
15
16
# File 'lib/trees/binary/node.rb', line 13

def initialize(data, parent: nil)
  self.parent = parent
  self.data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



11
12
13
# File 'lib/trees/binary/node.rb', line 11

def data
  @data
end

#leftObject

Returns the value of attribute left.



10
11
12
# File 'lib/trees/binary/node.rb', line 10

def left
  @left
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/trees/binary/node.rb', line 10

def parent
  @parent
end

#rightObject

Returns the value of attribute right.



10
11
12
# File 'lib/trees/binary/node.rb', line 10

def right
  @right
end

Instance Method Details

#insert(value) ⇒ Object Also known as: <<



22
23
24
25
26
27
28
# File 'lib/trees/binary/node.rb', line 22

def insert(value)
  case data <=> value
    when 1 then insert_left(value)
    when -1 then insert_right(value)
    when 0 then false
  end
end

#root?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/trees/binary/node.rb', line 18

def root?
  parent.nil? ? true : false
end