Last active
January 11, 2020 13:17
-
-
Save holsee/0bdfce307616e9a1bee3 to your computer and use it in GitHub Desktop.
BTree in Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule BTree do | |
defstruct tree: nil | |
def new(e), do: %BTree{tree: {e, nil, nil}} | |
def insert(%BTree{tree: root}, element) do | |
%BTree{tree: do_insert(root, element)} | |
end | |
defp do_insert(nil, element) do | |
{element, nil, nil} | |
end | |
defp do_insert({e, l, r}, element) when e > element do | |
{e, do_insert(l, element), r} | |
end | |
defp do_insert({e, l, r}, element) when e < element do | |
{e, l, do_insert(r, element)} | |
end | |
defp do_insert({_, _, _} = tuple, _element) do | |
tuple | |
end | |
def search(%BTree{tree: root}, element) do | |
do_search(root, element) | |
end | |
defp do_search(nil, element) do | |
false | |
end | |
defp do_search({e, l, _r}, element) when e > element do | |
do_search(l, element) | |
end | |
defp do_search({e, _l, r}, element) when e < element do | |
do_search(r, element) | |
end | |
defp do_search({_, _, _}, _element) do | |
true | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule TreesTest do | |
use ExUnit.Case | |
test "new" do | |
assert BTree.new(10) | |
== %BTree{tree: {10, nil, nil}} | |
end | |
test "insert same" do | |
assert BTree.new(10) |> BTree.insert(10) | |
== %BTree{tree: {10, nil, nil}} | |
end | |
test "insert lower" do | |
assert BTree.new(10) |> BTree.insert(5) | |
== %BTree{tree: {10, {5, nil, nil}, nil}} | |
end | |
test "insert higher" do | |
assert BTree.new(10) |> BTree.insert(15) | |
== %BTree{tree: {10, nil, {15, nil, nil}}} | |
end | |
test "insert lower then higher" do | |
assert BTree.new(10) |> BTree.insert(5) |> BTree.insert(15) | |
== %BTree{tree: {10, {5, nil, nil}, {15, nil, nil}}} | |
end | |
test "insert higher then lower" do | |
assert BTree.new(10) |> BTree.insert(15) |> BTree.insert(5) | |
== %BTree{tree: {10, {5, nil, nil}, {15, nil, nil}}} | |
end | |
test "search empty" do | |
assert BTree.search(%BTree{tree: nil}, 1) == false | |
end | |
test "search found" do | |
assert BTree.new(10) |> BTree.search(10) | |
== true | |
end | |
test "search not found" do | |
assert BTree.new(10) |> BTree.search(1) | |
== false | |
end | |
test "search higher found" do | |
assert BTree.new(10) |> BTree.insert(15) |> BTree.search(15) | |
== true | |
end | |
test "search lower found" do | |
assert BTree.new(10) |> BTree.insert(5) |> BTree.search(5) | |
== true | |
end | |
test "search higher not found" do | |
assert BTree.new(10) |> BTree.insert(15) |> BTree.search(20) | |
== false | |
end | |
test "search lower not found" do | |
assert BTree.new(10) |> BTree.insert(5) |> BTree.search(1) | |
== false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment