Class: Algorithms::Containers::RubyRBTreeMap::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/containers/rb_tree_map.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value) ⇒ Node

Returns a new instance of Node.



207
208
209
210
211
212
213
214
215
# File 'lib/containers/rb_tree_map.rb', line 207

def initialize(key, value)
  @key = key
  @value = value
  @color = :red
  @left = nil
  @right = nil
  @size = 1
  @height = 1
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



206
207
208
# File 'lib/containers/rb_tree_map.rb', line 206

def color
  @color
end

#heightObject

Returns the value of attribute height.



206
207
208
# File 'lib/containers/rb_tree_map.rb', line 206

def height
  @height
end

#keyObject

Returns the value of attribute key.



206
207
208
# File 'lib/containers/rb_tree_map.rb', line 206

def key
  @key
end

#leftObject

Returns the value of attribute left.



206
207
208
# File 'lib/containers/rb_tree_map.rb', line 206

def left
  @left
end

#rightObject

Returns the value of attribute right.



206
207
208
# File 'lib/containers/rb_tree_map.rb', line 206

def right
  @right
end

#sizeObject

Returns the value of attribute size.



206
207
208
# File 'lib/containers/rb_tree_map.rb', line 206

def size
  @size
end

#valueObject

Returns the value of attribute value.



206
207
208
# File 'lib/containers/rb_tree_map.rb', line 206

def value
  @value
end

Instance Method Details

#colorflipObject



221
222
223
224
225
# File 'lib/containers/rb_tree_map.rb', line 221

def colorflip
  @color       = @color == :red       ? :black : :red
  @left.color  = @left.color == :red  ? :black : :red
  @right.color = @right.color == :red ? :black : :red
end

#fixupObject



286
287
288
289
290
291
292
# File 'lib/containers/rb_tree_map.rb', line 286

def fixup
  rotate_left if @right && @right.red?
  rotate_right if (@left && @left.red?) && (@left.left && @left.left.red?)
  colorflip if (@left && @left.red?) && (@right && @right.red?)

  update_size
end

#move_red_leftObject



267
268
269
270
271
272
273
274
275
# File 'lib/containers/rb_tree_map.rb', line 267

def move_red_left
  colorflip
  if (@right.left && @right.left.red?)
    @right.rotate_right
    rotate_left
    colorflip
  end
  self
end

#move_red_rightObject



277
278
279
280
281
282
283
284
# File 'lib/containers/rb_tree_map.rb', line 277

def move_red_right
  colorflip
  if (@left.left && @left.left.red?)
    rotate_right
    colorflip
  end
  self
end

#red?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/containers/rb_tree_map.rb', line 217

def red?
  @color == :red
end

#rotate_leftObject



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/containers/rb_tree_map.rb', line 239

def rotate_left
  r = @right
  r_key, r_value, r_color = r.key, r.value, r.color
  b = r.left
  r.left = @left
  @left = r
  @right = r.right
  r.right = b
  r.color, r.key, r.value = :red, @key, @value
  @key, @value = r_key, r_value
  r.update_size
  update_size
end

#rotate_rightObject



253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/containers/rb_tree_map.rb', line 253

def rotate_right
  l = @left
  l_key, l_value, l_color = l.key, l.value, l.color
  b = l.right
  l.right = @right
  @right = l
  @left = l.left
  l.left = b
  l.color, l.key, l.value = :red, @key, @value
  @key, @value = l_key, l_value
  l.update_size
  update_size
end

#update_sizeObject



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/containers/rb_tree_map.rb', line 227

def update_size
  @size = (@left ? @left.size : 0) + (@right ? @right.size : 0) + 1
  left_height = (@left ? @left.height : 0)
  right_height = (@right ? @right.height : 0)
  if left_height > right_height
    @height = left_height + 1
  else
    @height = right_height + 1
  end
  self
end