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.119 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.118 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.117 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.116 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.115 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.114 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.113 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.111 tracks/elixir/exercises/binary-search-tree/example.exs
trackler-2.2.1.110 tracks/elixir/exercises/binary-search-tree/example.exs