programming-examples/ruby/Algorithms/binary_search_tree_from_array.rb
2019-11-15 12:59:38 +01:00

11 lines
275 B
Ruby

def create_binary_tree(arr, arr_start, arr_end)
return if arr_start > arr_end
mid = arr_start + arr_end / 2
root = Node.new(arr[mid])
root.left = create_binary_tree(arr, arr_start, mid - 1)
root.right = create_binary_tree(arr, mid + 1, arr_end)
root
end