Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement exercise binary-search-tree #412

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,18 @@
"topics": [
"lists"
]
},
{
"slug": "binary-search-tree",
"uuid": "a8f7f17e-567b-43ec-9ee5-6288367f0ae4",
"core": false,
"unlocked_by": null,
"difficulty": 3,
"topics": [
"recursion",
"searching",
"trees"
]
}
]
}
60 changes: 60 additions & 0 deletions exercises/binary-search-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Binary Search Tree

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sections "Running tests" and "Questions?" are missing, please use confliglet generate to generate the README.md.


Josh Cheek [https://twitter.com/josh_cheek](https://twitter.com/josh_cheek)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
30 changes: 30 additions & 0 deletions exercises/binary-search-tree/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%% Erlang compiler options
{erl_opts, [debug_info, warnings_as_errors]}.

{deps, [{erl_exercism, "0.1.2"}]}.

{dialyzer, [
{warnings, [underspecs, no_return]},
{get_warnings, true},
{plt_apps, top_level_deps}, % top_level_deps | all_deps
{plt_extra_apps, []},
{plt_location, local}, % local | "/my/file/name"
{plt_prefix, "rebar3"},
{base_plt_apps, [stdlib, kernel, crypto]},
{base_plt_location, global}, % global | "/my/file/name"
{base_plt_prefix, "rebar3"}
]}.

%% eunit:test(Tests)
{eunit_tests, []}.
%% Options for eunit:test(Tests, Opts)
{eunit_opts, [verbose]}.

%% == xref ==

{xref_warnings, true}.

%% xref checks to run
{xref_checks, [undefined_function_calls, undefined_functions,
locals_not_used, exports_not_used,
deprecated_function_calls, deprecated_functions]}.
9 changes: 9 additions & 0 deletions exercises/binary-search-tree/src/binary_search_tree.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{application, binary_search_tree,
[{description, "exercism.io - binary-search-tree"},
{vsn, "0.0.1"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Canonical data is at version 1.0.0.

{modules, []},
{registered, []},
{applications, [kernel,
stdlib]},
{env, []}
]}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline please.

21 changes: 21 additions & 0 deletions exercises/binary-search-tree/src/binary_search_tree.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-module(binary_search_tree).

-export([empty/0, value/1, left/1, right/1, insert/2, to_list/1]).

%% empty tree
empty() -> undefined.

%% value at the top of _BST
value(_BST) -> undefined.

%% left subtree
left(_BST) -> undefined.

%% right subtree
right(_BST) -> undefined.

%% insert _Value into _BST
insert(_BST, _Value) -> undefined.

%% convert _BST to a sorted list
to_list(_BST) -> undefined.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline please.

Also this should really be sorted_data/1 as in the problem specification.

18 changes: 18 additions & 0 deletions exercises/binary-search-tree/src/example.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-module(example).

-export([empty/0, value/1, left/1, right/1, insert/2, to_list/1]).

empty() -> empty.

value({V,_,_}) -> V.

left({_, L,_}) -> L.

right({_, _, R}) -> R.

insert(empty, V) -> {V, empty, empty};
insert({V1, L, R}, V2) when V1 < V2 -> {V1, L, insert(R, V2)};
insert({V1, L, R}, V2) when V1 >= V2 -> {V1, insert(L, V2), R}.

to_list(empty) -> [];
to_list({V, L, R}) -> to_list(L) ++ [V] ++ to_list(R).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline please

112 changes: 112 additions & 0 deletions exercises/binary-search-tree/test/binary_search_tree_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
%% Based on canonical data version 1.4.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/bob/canonical-data.json
%% This file is automatically generated from the exercises canonical data.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the test suite is created manually (ie, there is no test generator), this line is wrong...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It were even wrong if it were from the generator, as it claims to be generated from bobs canonical data…


-module(binary_search_tree_tests).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a comment, explaining that this was manually created from canonical data version 1.0.0 and a link to the specific commit (https://github.com/exercism/problem-specifications/blob/d5a4db7d282b16110e376f0ccfa91c80967b8b40/exercises/binary-search-tree/canonical-data.json)

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'1_data_is_retained_test'() ->
T = binary_search_tree:insert(binary_search_tree:empty(), 4),
?assertMatch(4, binary_search_tree:value(T)).

'2_smaller_number_at_left_node_test'() ->
T = binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:empty(), 4),
2),
?assertMatch(4, binary_search_tree:value(T)),
?assertMatch(2, binary_search_tree:value(binary_search_tree:left(T))).

'3_same number_at_left_node_test'() ->
T = binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:empty(), 4),
4),
?assertMatch(4, binary_search_tree:value(T)),
?assertMatch(4, binary_search_tree:value(binary_search_tree:left(T))).

'4_greater_number_at_right_node_test'() ->
T = binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:empty(), 4),
5),
?assertMatch(4, binary_search_tree:value(T)),
?assertMatch(5, binary_search_tree:value(binary_search_tree:right(T))).

'5_can_create_complex_tree_test'() ->
T = binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:empty(),
4),
2),
6),
1),
3),
5),
7),
?assertMatch(4, binary_search_tree:value(T)),
?assertMatch(2, binary_search_tree:value(binary_search_tree:left(T))),
?assertMatch(1, binary_search_tree:value(
binary_search_tree:left(
binary_search_tree:left(T)))),
?assertMatch(3, binary_search_tree:value(
binary_search_tree:right(
binary_search_tree:left(T)))),
?assertMatch(6, binary_search_tree:value(binary_search_tree:right(T))),
?assertMatch(5, binary_search_tree:value(
binary_search_tree:left(
binary_search_tree:right(T)))),
?assertMatch(7, binary_search_tree:value(
binary_search_tree:right(
binary_search_tree:right(T)))).

'6_can_sort_single_number_test'() ->
T = binary_search_tree:insert(binary_search_tree:empty(), 2),
?assertMatch([2], binary_search_tree:to_list(T)).

'7_can_sort_if_second_number_is_smaller_than_first_test'() ->
T = binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:empty(),
2),
1),
?assertMatch([1, 2], binary_search_tree:to_list(T)).

'8_can_sort_if_second_number_is_same_as_first_test'() ->
T = binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:empty(),
2),
2),
?assertMatch([2, 2], binary_search_tree:to_list(T)).

'9_can_sort_if_second_number_is_greater_than_first_test'() ->
T = binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:empty(),
2),
3),
?assertMatch([2, 3], binary_search_tree:to_list(T)).

'10_can_sort_complex_tree_test'() ->
T = binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:insert(
binary_search_tree:empty(),
2),
1),
3),
6),
7),
5),
?assertMatch([1, 2, 3, 5, 6, 7], binary_search_tree:to_list(T)).