Method: BiDimensionalTree#new_node

Defined in:
lib/bi-dimensional-access.rb

#new_node(node, x, y, *args) ⇒ Object

Create a new branch after locating it’s place



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bi-dimensional-access.rb', line 10

def new_node(node, x, y, *args)
  if @root.nil?
    puts "Adding the node (#{x}, #{y}) in the root"
    return @root = BiNode.new(x, y, *args)
  elsif node.nil?
    return node = BiNode.new(x, y, *args)
  elsif node.x == x
    if node.y == y
      return node
    elsif y < node.y
      if node.south.nil?
        puts "Adding the node (#{x}, #{y}) in the south of (#{node.x}, #{node.y})"
        node.south = new_node(node.south, x, y, *args)
      else
        new_node(node.south, x, y, *args)
      end
    elsif y > node.y
      if node.north.nil?
        puts "Adding the node (#{x}, #{y}) in the north of (#{node.x}, #{node.y})"
        node.north = new_node(node.north, x, y, *args)
      else
        new_node(node.north, x, y, *args)
      end
    end
  elsif x < node.x
    if node.west.nil?
      puts "Adding the node (#{x}, #{y}) in the west of (#{node.x}, #{node.y})"
      node.west = new_node(node.west, x, y, *args)
    else
      new_node(node.west, x, y, *args)
    end
  elsif x > node.x
    if node.east.nil?
      puts "Adding the node (#{x}, #{y}) in the east of (#{node.x}, #{node.y})"
      node.east = new_node(node.east, x, y, *args)
    else
      new_node(node.east, x, y, *args)
    end
  end
end