From e0d8231e91f5e05d1fdd70ff22980e25ac8f0048 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Mon, 1 Jul 2024 13:56:40 -0400 Subject: [PATCH] binary_search_tree --- .../binary-search-tree/.exercism/config.json | 25 ++++ .../.exercism/metadata.json | 1 + elixir/binary-search-tree/.formatter.exs | 4 + elixir/binary-search-tree/.gitignore | 24 ++++ elixir/binary-search-tree/HELP.md | 75 +++++++++++ elixir/binary-search-tree/README.md | 70 +++++++++++ .../lib/binary_search_tree.ex | 26 ++++ elixir/binary-search-tree/mix.exs | 28 +++++ .../test/binary_search_tree_test.exs | 119 ++++++++++++++++++ .../binary-search-tree/test/test_helper.exs | 2 + 10 files changed, 374 insertions(+) create mode 100644 elixir/binary-search-tree/.exercism/config.json create mode 100644 elixir/binary-search-tree/.exercism/metadata.json create mode 100644 elixir/binary-search-tree/.formatter.exs create mode 100644 elixir/binary-search-tree/.gitignore create mode 100644 elixir/binary-search-tree/HELP.md create mode 100644 elixir/binary-search-tree/README.md create mode 100644 elixir/binary-search-tree/lib/binary_search_tree.ex create mode 100644 elixir/binary-search-tree/mix.exs create mode 100644 elixir/binary-search-tree/test/binary_search_tree_test.exs create mode 100644 elixir/binary-search-tree/test/test_helper.exs diff --git a/elixir/binary-search-tree/.exercism/config.json b/elixir/binary-search-tree/.exercism/config.json new file mode 100644 index 0000000..912427a --- /dev/null +++ b/elixir/binary-search-tree/.exercism/config.json @@ -0,0 +1,25 @@ +{ + "authors": [ + "sngeth" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "neenjaw", + "sotojuan" + ], + "files": { + "solution": [ + "lib/binary_search_tree.ex" + ], + "test": [ + "test/binary_search_tree_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Insert and search for numbers in a binary tree.", + "source": "Josh Cheek" +} diff --git a/elixir/binary-search-tree/.exercism/metadata.json b/elixir/binary-search-tree/.exercism/metadata.json new file mode 100644 index 0000000..bf250ce --- /dev/null +++ b/elixir/binary-search-tree/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"binary-search-tree","id":"fc6ed3ee9f82408eb78d0f9df5e506bf","url":"https://exercism.org/tracks/elixir/exercises/binary-search-tree","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/binary-search-tree/.formatter.exs b/elixir/binary-search-tree/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/binary-search-tree/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/binary-search-tree/.gitignore b/elixir/binary-search-tree/.gitignore new file mode 100644 index 0000000..258a530 --- /dev/null +++ b/elixir/binary-search-tree/.gitignore @@ -0,0 +1,24 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +binary_search_tree-*.tar + diff --git a/elixir/binary-search-tree/HELP.md b/elixir/binary-search-tree/HELP.md new file mode 100644 index 0000000..7f639ae --- /dev/null +++ b/elixir/binary-search-tree/HELP.md @@ -0,0 +1,75 @@ +# Help + +## Running the tests + +From the terminal, change to the base directory of the exercise then execute the tests with: + +```bash +$ mix test +``` + +This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs` + +Documentation: + +* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html) +* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html) + +## Pending tests + +In test suites of practice exercises, all but the first test have been tagged to be skipped. + +Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol. + +For example: + +```elixir +# @tag :pending +test "shouting" do + assert Bob.hey("WATCH OUT!") == "Whoa, chill out!" +end +``` + +If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command: + +```bash +$ mix test --include pending +``` + +Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`. + +```elixir +# ExUnit.configure(exclude: :pending, trace: true) +``` + +## Useful `mix test` options + +* `test/.exs:LINENUM` - runs only a single test, the test from `.exs` whose definition is on line `LINENUM` +* `--failed` - runs only tests that failed the last time they ran +* `--max-failures` - the suite stops evaluating tests when this number of test failures +is reached +* `--seed 0` - disables randomization so the tests in a single file will always be ran +in the same order they were defined in + +## Submitting your solution + +You can submit your solution using the `exercism submit lib/binary_search_tree.ex` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir) +- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir) +- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found. \ No newline at end of file diff --git a/elixir/binary-search-tree/README.md b/elixir/binary-search-tree/README.md new file mode 100644 index 0000000..2d87cbd --- /dev/null +++ b/elixir/binary-search-tree/README.md @@ -0,0 +1,70 @@ +# Binary Search Tree + +Welcome to Binary Search Tree on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Insert and search for numbers in a binary tree. + +When we need to represent sorted data, an array does not make a good data structure. + +Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes `[1, 3, 4, 5, 2]`. +Now we must sort the entire array again! +We can improve on this by realizing that we only need to make space for the new item `[1, nil, 3, 4, 5]`, and then adding the item in the space we added. +But this still requires us to shift many elements down by one. + +Binary Search Trees, however, can operate on sorted data much more efficiently. + +A binary search tree consists of a series of connected nodes. +Each node contains a piece of data (e.g. the number 3), a variable named `left`, and a variable named `right`. +The `left` and `right` variables point at `nil`, or other nodes. +Since these other nodes in turn have other nodes beneath them, we say that the left and right variables are pointing at subtrees. +All data in the left subtree is less than or equal to the current node's data, and all data in the right subtree is greater than the current node's data. + +For example, if we had a node containing the data 4, and we added the data 2, our tree would look like this: + + 4 + / + 2 + +If we then added 6, it would look like this: + + 4 + / \ + 2 6 + +If we then added 3, it would look like this + + 4 + / \ + 2 6 + \ + 3 + +And if we then added 1, 5, and 7, it would look like this + + 4 + / \ + / \ + 2 6 + / \ / \ + 1 3 5 7 + +## Source + +### Created by + +- @sngeth + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @neenjaw +- @sotojuan + +### Based on + +Josh Cheek \ No newline at end of file diff --git a/elixir/binary-search-tree/lib/binary_search_tree.ex b/elixir/binary-search-tree/lib/binary_search_tree.ex new file mode 100644 index 0000000..9379680 --- /dev/null +++ b/elixir/binary-search-tree/lib/binary_search_tree.ex @@ -0,0 +1,26 @@ +defmodule BinarySearchTree do + @type bst_node :: %{data: any, left: bst_node | nil, right: bst_node | nil} + + @doc """ + Create a new Binary Search Tree with root's value as the given 'data' + """ + @spec new(any) :: bst_node + def new(data), do: %{data: data, left: nil, right: nil} + + @doc """ + Creates and inserts a node with its value as 'data' into the tree. + """ + @spec insert(bst_node, any) :: bst_node + def insert(nil, data), do: new(data) + def insert(tree, data) when data <= tree.data, do: + %{tree | left: insert(tree.left, data)} + def insert(tree, data), do: + %{tree | right: insert(tree.right, data)} + + @doc """ + Traverses the Binary Search Tree in order and returns a list of each node's data. + """ + @spec in_order(bst_node) :: [any] + def in_order(nil), do: [] + def in_order(tree), do: in_order(tree.left) ++ [tree.data | in_order(tree.right)] +end diff --git a/elixir/binary-search-tree/mix.exs b/elixir/binary-search-tree/mix.exs new file mode 100644 index 0000000..a223062 --- /dev/null +++ b/elixir/binary-search-tree/mix.exs @@ -0,0 +1,28 @@ +defmodule BinarySearchTree.MixProject do + use Mix.Project + + def project do + [ + app: :binary_search_tree, + version: "0.1.0", + # elixir: "~> 1.8", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/elixir/binary-search-tree/test/binary_search_tree_test.exs b/elixir/binary-search-tree/test/binary_search_tree_test.exs new file mode 100644 index 0000000..e5867d9 --- /dev/null +++ b/elixir/binary-search-tree/test/binary_search_tree_test.exs @@ -0,0 +1,119 @@ +defmodule BinarySearchTreeTest do + use ExUnit.Case + + test "data is retained" do + root = BinarySearchTree.new(4) + assert root.data == 4 + assert root.left == nil + assert root.right == nil + end + + describe "insert data at proper node" do + test "smaller number at left node" do + root = + BinarySearchTree.new(4) + |> BinarySearchTree.insert(2) + + assert root.data == 4 + assert root.left.data == 2 + assert root.left.left == nil + assert root.left.right == nil + assert root.right == nil + end + + test "same number at left node" do + root = + BinarySearchTree.new(4) + |> BinarySearchTree.insert(4) + + assert root.data == 4 + assert root.left.data == 4 + assert root.left.left == nil + assert root.left.right == nil + assert root.right == nil + end + + test "greater number at right node" do + root = + BinarySearchTree.new(4) + |> BinarySearchTree.insert(5) + + assert root.data == 4 + assert root.left == nil + assert root.right.data == 5 + assert root.right.left == nil + assert root.right.right == nil + end + end + + test "can create complex tree" do + root = + BinarySearchTree.new(4) + |> BinarySearchTree.insert(2) + |> BinarySearchTree.insert(6) + |> BinarySearchTree.insert(1) + |> BinarySearchTree.insert(3) + |> BinarySearchTree.insert(5) + |> BinarySearchTree.insert(7) + + assert root.data == 4 + assert root.left.data == 2 + assert root.left.left.data == 1 + assert root.left.left.left == nil + assert root.left.left.right == nil + assert root.left.right.data == 3 + assert root.left.right.left == nil + assert root.left.right.right == nil + assert root.right.data == 6 + assert root.right.left.data == 5 + assert root.right.left.left == nil + assert root.right.left.right == nil + assert root.right.right.data == 7 + assert root.right.right.left == nil + assert root.right.right.right == nil + end + + describe "can sort data" do + test "can sort single number" do + root = BinarySearchTree.new(2) + + assert [2] == BinarySearchTree.in_order(root) + end + + test "can sort if second number is smaller than first" do + root = + BinarySearchTree.new(2) + |> BinarySearchTree.insert(1) + + assert [1, 2] == BinarySearchTree.in_order(root) + end + + test "can sort if second number is the same as the first" do + root = + BinarySearchTree.new(2) + |> BinarySearchTree.insert(2) + + assert [2, 2] == BinarySearchTree.in_order(root) + end + + test "can sort if second number is greater than the first" do + root = + BinarySearchTree.new(2) + |> BinarySearchTree.insert(3) + + assert [2, 3] == BinarySearchTree.in_order(root) + end + + test "can sort complex tree" do + root = + BinarySearchTree.new(2) + |> BinarySearchTree.insert(1) + |> BinarySearchTree.insert(3) + |> BinarySearchTree.insert(6) + |> BinarySearchTree.insert(7) + |> BinarySearchTree.insert(5) + + assert [1, 2, 3, 5, 6, 7] == BinarySearchTree.in_order(root) + end + end +end diff --git a/elixir/binary-search-tree/test/test_helper.exs b/elixir/binary-search-tree/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/binary-search-tree/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)