Class: Xunch::RBTree::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/xunch/utils/rb_tree_node.rb

Overview

A node in the red-black tree.

Nodes should only be manipulated directly by the RedBlackTree class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value, parent) ⇒ Node

Creates a new node.

New tree nodes are red by default.



20
21
22
23
24
25
# File 'lib/xunch/utils/rb_tree_node.rb', line 20

def initialize(key, value, parent)
  @color = :black
  @key = key
  @value = value
  @parent = parent
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



12
13
14
# File 'lib/xunch/utils/rb_tree_node.rb', line 12

def color
  @color
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/xunch/utils/rb_tree_node.rb', line 9

def key
  @key
end

#leftObject

Returns the value of attribute left.



13
14
15
# File 'lib/xunch/utils/rb_tree_node.rb', line 13

def left
  @left
end

#parentObject

Returns the value of attribute parent.



15
16
17
# File 'lib/xunch/utils/rb_tree_node.rb', line 15

def parent
  @parent
end

#rightObject

Returns the value of attribute right.



14
15
16
# File 'lib/xunch/utils/rb_tree_node.rb', line 14

def right
  @right
end

#valueObject

Returns the value of attribute value.



10
11
12
# File 'lib/xunch/utils/rb_tree_node.rb', line 10

def value
  @value
end

Instance Method Details

#black?Boolean

True for black nodes.

Returns:

  • (Boolean)


37
38
39
# File 'lib/xunch/utils/rb_tree_node.rb', line 37

def black?
  @color == :black
end

#inspectObject



32
33
34
# File 'lib/xunch/utils/rb_tree_node.rb', line 32

def inspect
  "#{@key}=#{@value},#{@color}"
end

#red?Boolean

True for red nodes.

Returns:

  • (Boolean)


42
43
44
# File 'lib/xunch/utils/rb_tree_node.rb', line 42

def red?
  @color == :red
end

#to_sObject

node to string



28
29
30
# File 'lib/xunch/utils/rb_tree_node.rb', line 28

def to_s
  "#{@key}=#{@value},#{@color}"
end