Class: RedBlackTree::Node

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/red_black_tree/node.rb,
lib/red_black_tree/node/left_right_element_referencers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Node

Returns a new instance of Node.

Parameters:

  • data (any)

    a non-nil data/value representing the node

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
# File 'lib/red_black_tree/node.rb', line 22

def initialize data
  raise ArgumentError, "data cannot be nil" if data.nil? && !(instance_of? RedBlackTree::LeafNode)

  @data = data
  @colour = nil
  @parent = @left = @right = nil

  validate_free!
end

Instance Attribute Details

#dataany (readonly)

Returns the data/value representing the node.

Returns:

  • (any)

    the data/value representing the node



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

def data
  @data
end

Instance Method Details

#<=>(other) ⇒ Object

Needs to be implemented in a subclass of RedBlackTree::Node. Will be used to calculate the ideal position of this node when adding it to a tree.

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/red_black_tree/node.rb', line 35

def <=> other
  raise NotImplementedError, "Comparable operator <=> must be implemented in subclass"
end