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

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/binary-search-tree/example.exs