Class: BinarySearch::RedBlackTree::Node

Inherits:
Struct
  • Object
show all
Defined in:
lib/binary_search/red_black_tree/node.rb

Overview

Represents a node in the Red-Black Tree

A node contains a key, color, references to its left and right children, and a reference to its parent. The color is used to maintain the balance properties of the Red-Black Tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, color = :red, left = nil, right = nil, parent = nil) ⇒ Node

Creates a new Node

Parameters:

  • key (Comparable)

    The key stored in the node

  • color (Symbol) (defaults to: :red)

    The color of the node (:red or :black)

  • left (Node, nil) (defaults to: nil)

    The left child of the node

  • right (Node, nil) (defaults to: nil)

    The right child of the node

  • parent (Node, nil) (defaults to: nil)

    The parent of the node



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

def initialize(key, color = :red, left = nil, right = nil, parent = nil)
  super(key, color, left, right, parent)
end

Instance Attribute Details

#colorObject

Returns the value of attribute color

Returns:

  • (Object)

    the current value of color



10
11
12
# File 'lib/binary_search/red_black_tree/node.rb', line 10

def color
  @color
end

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



10
11
12
# File 'lib/binary_search/red_black_tree/node.rb', line 10

def key
  @key
end

#leftObject

Returns the value of attribute left

Returns:

  • (Object)

    the current value of left



10
11
12
# File 'lib/binary_search/red_black_tree/node.rb', line 10

def left
  @left
end

#parentObject

Returns the value of attribute parent

Returns:

  • (Object)

    the current value of parent



10
11
12
# File 'lib/binary_search/red_black_tree/node.rb', line 10

def parent
  @parent
end

#rightObject

Returns the value of attribute right

Returns:

  • (Object)

    the current value of right



10
11
12
# File 'lib/binary_search/red_black_tree/node.rb', line 10

def right
  @right
end

Instance Method Details

#black?Boolean

Checks if the node is black

Returns:

  • (Boolean)

    true if the node is black, false otherwise



25
26
27
# File 'lib/binary_search/red_black_tree/node.rb', line 25

def black?
  color == :black
end

#red?Boolean

Checks if the node is red

Returns:

  • (Boolean)

    true if the node is red, false otherwise



32
33
34
# File 'lib/binary_search/red_black_tree/node.rb', line 32

def red?
  color == :red
end