Module: TreeHandler::Comparable

Included in:
Tree
Defined in:
lib/tree_handler/comparable.rb

Instance Method Summary collapse

Instance Method Details

#compare(node_A, node_B) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/tree_handler/comparable.rb', line 3

def compare(node_A, node_B)
  if node_B.value < node_A.value
    if node_A.left_child.nil?
      node_A.left_child = node_B
    else
      compare(node_A.left_child, node_B)
    end
  else
    if node_A.right_child.nil?
      node_A.right_child = node_B
    else
      compare(node_A.right_child, node_B)
    end
  end
end