-
-
Notifications
You must be signed in to change notification settings - Fork 89
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
73ac5d1
63c61b1
28905b8
432e2bd
5f2f4a3
d7e79b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sections "Running tests" and "Questions?" are missing, please use |
||
|
||
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. |
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]}. |
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"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, []} | ||
]}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newline please. |
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newline please. Also this should really be |
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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newline please |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
-module(binary_search_tree_tests). | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please unnest function calls and use numbered variables?
This is usually much easier to follow. Alternatively using a list and folding over it is fine as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you meant |
||
?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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please unnest function calls and use numbered variables? (see above) |
||
?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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please unnest function calls and use numbered variables? (see above) |
||
?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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please unnest function calls and use numbered variables? (see above) |
||
?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)))). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not really like the look of the multi line matches, but aside from introducing further intermediate variables that destructure the tree again, I have no idea how to clean that up… |
||
|
||
'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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please unnest function calls and use numbered variables? (see above) |
||
?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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please unnest function calls and use numbered variables? (see above) |
||
?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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please unnest function calls and use numbered variables? (see above) |
||
?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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please unnest function calls and use numbered variables? (see above) |
||
?assertMatch([1, 2, 3, 5, 6, 7], binary_search_tree:to_list(T)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a newline here!