Sha256: 8d15f272eba5a2891d886daa8384bd6746376e71526a60221c2b2c497f693235
Contents?: true
Size: 676 Bytes
Versions: 69
Compression:
Stored size: 676 Bytes
Contents
defmodule BinarySearchTree do def new(data) do %{data: data, left: nil, right: nil} end def insert(nil, node_value), do: new(node_value) def insert(%{data: data, left: left, right: right}, node_value) do if node_value <= data do %{data: data, left: insert(left, node_value), right: right} else %{data: data, left: left, right: insert(right, node_value)} end end def in_order(tree) do in_order(tree, []) |> Enum.reverse() end def in_order(nil, accum) do accum end def in_order(tree, accum) do left_side = in_order(tree.left, accum) middle = [tree.data | left_side] in_order(tree.right, middle) end end
Version data entries
69 entries across 69 versions & 1 rubygems