Class: Trees::Binary::Node
- Inherits:
-
Object
- Object
- Trees::Binary::Node
- Includes:
- NodeHelpers, SearchHelpers
- Defined in:
- lib/trees/binary/node.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#left ⇒ Object
Returns the value of attribute left.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#right ⇒ Object
Returns the value of attribute right.
Instance Method Summary collapse
-
#initialize(data, parent: nil) ⇒ Node
constructor
A new instance of Node.
- #insert(value) ⇒ Object (also: #<<)
- #root? ⇒ Boolean
Methods included from SearchHelpers
Methods included from NodeHelpers
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
#data ⇒ Object
Returns the value of attribute data.
11 12 13 |
# File 'lib/trees/binary/node.rb', line 11 def data @data end |
#left ⇒ Object
Returns the value of attribute left.
10 11 12 |
# File 'lib/trees/binary/node.rb', line 10 def left @left end |
#parent ⇒ Object
Returns the value of attribute parent.
10 11 12 |
# File 'lib/trees/binary/node.rb', line 10 def parent @parent end |
#right ⇒ Object
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
18 19 20 |
# File 'lib/trees/binary/node.rb', line 18 def root? parent.nil? ? true : false end |