Skip to content

Commit b64a090

Browse files
authored
Add binary-search-tree (#131)
1 parent d6a0c46 commit b64a090

9 files changed

Lines changed: 331 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,14 @@
602602
"prerequisites": [],
603603
"difficulty": 4
604604
},
605+
{
606+
"slug": "binary-search-tree",
607+
"name": "Binary Search Tree",
608+
"uuid": "338954f7-9a12-46fc-9c5f-3345a83baa48",
609+
"practices": [],
610+
"prerequisites": [],
611+
"difficulty": 5
612+
},
605613
{
606614
"slug": "camicia",
607615
"name": "Camicia",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Instructions
2+
3+
Insert and search for numbers in a binary tree.
4+
5+
When we need to represent sorted data, an array does not make a good data structure.
6+
7+
Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes `[1, 3, 4, 5, 2]`.
8+
Now we must sort the entire array again!
9+
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.
10+
But this still requires us to shift many elements down by one.
11+
12+
Binary Search Trees, however, can operate on sorted data much more efficiently.
13+
14+
A binary search tree consists of a series of connected nodes.
15+
Each node contains a piece of data (e.g. the number 3), a variable named `left`, and a variable named `right`.
16+
The `left` and `right` variables point at `nil`, or other nodes.
17+
Since these other nodes in turn have other nodes beneath them, we say that the left and right variables are pointing at subtrees.
18+
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.
19+
20+
For example, if we had a node containing the data 4, and we added the data 2, our tree would look like this:
21+
22+
![A graph with root node 4 and a single child node 2.](https://assets.exercism.org/images/exercises/binary-search-tree/tree-4-2.svg)
23+
24+
```text
25+
4
26+
/
27+
2
28+
```
29+
30+
If we then added 6, it would look like this:
31+
32+
![A graph with root node 4 and two child nodes 2 and 6.](https://assets.exercism.org/images/exercises/binary-search-tree/tree-4-2-6.svg)
33+
34+
```text
35+
4
36+
/ \
37+
2 6
38+
```
39+
40+
If we then added 3, it would look like this
41+
42+
![A graph with root node 4, two child nodes 2 and 6, and a grandchild node 3.](https://assets.exercism.org/images/exercises/binary-search-tree/tree-4-2-6-3.svg)
43+
44+
```text
45+
4
46+
/ \
47+
2 6
48+
\
49+
3
50+
```
51+
52+
And if we then added 1, 5, and 7, it would look like this
53+
54+
![A graph with root node 4, two child nodes 2 and 6, and four grandchild nodes 1, 3, 5 and 7.](https://assets.exercism.org/images/exercises/binary-search-tree/tree-4-2-6-1-3-5-7.svg)
55+
56+
```text
57+
4
58+
/ \
59+
/ \
60+
2 6
61+
/ \ / \
62+
1 3 5 7
63+
```
64+
65+
## Credit
66+
67+
The images were created by [habere-et-dispertire][habere-et-dispertire] using [PGF/TikZ][pgf-tikz] by Till Tantau.
68+
69+
[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire
70+
[pgf-tikz]: https://en.wikipedia.org/wiki/PGF/TikZ
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"binary_search_tree.moon"
8+
],
9+
"test": [
10+
"binary_search_tree_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Insert and search for numbers in a binary tree.",
17+
"source": "Josh Cheek"
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class BinarySearchTree
2+
new: (items = {}) =>
3+
@add item for item in *items
4+
5+
add: (item) =>
6+
if not @value
7+
@value = item
8+
elseif item <= @value
9+
if not @left then @left = @@!
10+
@left\add item
11+
else
12+
if not @right then @right = @@!
13+
@right\add item
14+
15+
data: =>
16+
{
17+
data: @value
18+
left: @left and @left\data! or nil
19+
right: @right and @right\data! or nil
20+
}
21+
22+
sorted: (list = {}) =>
23+
if @left then @left\sorted list
24+
table.insert list, @value
25+
if @right then @right\sorted list
26+
list
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
stringx = require 'pl.stringx'
2+
import dump from require 'moon'
3+
4+
bst = (t, level) ->
5+
dumped = dump t
6+
dumped = dumped\gsub('%[(.-)%] =', '%1:')
7+
lines = [indent line, level for line in *(stringx.splitlines dumped)]
8+
table.concat(lines, "\n")\gsub('{%s+}', 'nil')\gsub('^%s+', '')
9+
10+
string_list = (list) ->
11+
"{#{table.concat [quote word for word in *list], ', '}}"
12+
13+
14+
{
15+
module_name: 'BinarySearchTree',
16+
17+
generate_test: (case, level) ->
18+
local lines
19+
switch case.property
20+
when 'data'
21+
lines = {
22+
"tree = BinarySearchTree #{string_list case.input.treeData}"
23+
"result = tree\\data!"
24+
"expected = #{bst case.expected, level}",
25+
"assert.are.same expected, result"
26+
}
27+
when 'sortedData'
28+
lines = {
29+
"tree = BinarySearchTree #{string_list case.input.treeData}"
30+
"result = tree\\sorted!"
31+
"expected = #{string_list case.expected}",
32+
"assert.are.same expected, result"
33+
}
34+
table.concat [indent line, level for line in *lines], '\n'
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[e9c93a78-c536-4750-a336-94583d23fafa]
13+
description = "data is retained"
14+
15+
[7a95c9e8-69f6-476a-b0c4-4170cb3f7c91]
16+
description = "insert data at proper node -> smaller number at left node"
17+
18+
[22b89499-9805-4703-a159-1a6e434c1585]
19+
description = "insert data at proper node -> same number at left node"
20+
21+
[2e85fdde-77b1-41ed-b6ac-26ce6b663e34]
22+
description = "insert data at proper node -> greater number at right node"
23+
24+
[dd898658-40ab-41d0-965e-7f145bf66e0b]
25+
description = "can create complex tree"
26+
27+
[9e0c06ef-aeca-4202-b8e4-97f1ed057d56]
28+
description = "can sort data -> can sort single number"
29+
30+
[425e6d07-fceb-4681-a4f4-e46920e380bb]
31+
description = "can sort data -> can sort if second number is smaller than first"
32+
33+
[bd7532cc-6988-4259-bac8-1d50140079ab]
34+
description = "can sort data -> can sort if second number is same as first"
35+
36+
[b6d1b3a5-9d79-44fd-9013-c83ca92ddd36]
37+
description = "can sort data -> can sort if second number is greater than first"
38+
39+
[d00ec9bd-1288-4171-b968-d44d0808c1c8]
40+
description = "can sort data -> can sort complex tree"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class BinarySearchTree
2+
new: (items) =>
3+
error 'Implement the constructor'
4+
5+
data: =>
6+
error 'Implement the data method'
7+
8+
sorted: =>
9+
error 'Implement the sorted method'
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
BinarySearchTree = require 'binary_search_tree'
2+
3+
describe 'binary-search-tree', ->
4+
it 'data is retained', ->
5+
tree = BinarySearchTree {'4'}
6+
result = tree\data!
7+
expected = {
8+
right: nil
9+
data: "4"
10+
left: nil
11+
}
12+
assert.are.same expected, result
13+
14+
describe 'insert data at proper node', ->
15+
pending 'smaller number at left node', ->
16+
tree = BinarySearchTree {'4', '2'}
17+
result = tree\data!
18+
expected = {
19+
right: nil
20+
data: "4"
21+
left: {
22+
right: nil
23+
data: "2"
24+
left: nil
25+
}
26+
}
27+
assert.are.same expected, result
28+
29+
pending 'same number at left node', ->
30+
tree = BinarySearchTree {'4', '4'}
31+
result = tree\data!
32+
expected = {
33+
right: nil
34+
data: "4"
35+
left: {
36+
right: nil
37+
data: "4"
38+
left: nil
39+
}
40+
}
41+
assert.are.same expected, result
42+
43+
pending 'greater number at right node', ->
44+
tree = BinarySearchTree {'4', '5'}
45+
result = tree\data!
46+
expected = {
47+
right: {
48+
right: nil
49+
data: "5"
50+
left: nil
51+
}
52+
data: "4"
53+
left: nil
54+
}
55+
assert.are.same expected, result
56+
57+
pending 'can create complex tree', ->
58+
tree = BinarySearchTree {'4', '2', '6', '1', '3', '5', '7'}
59+
result = tree\data!
60+
expected = {
61+
right: {
62+
right: {
63+
right: nil
64+
data: "7"
65+
left: nil
66+
}
67+
data: "6"
68+
left: {
69+
right: nil
70+
data: "5"
71+
left: nil
72+
}
73+
}
74+
data: "4"
75+
left: {
76+
right: {
77+
right: nil
78+
data: "3"
79+
left: nil
80+
}
81+
data: "2"
82+
left: {
83+
right: nil
84+
data: "1"
85+
left: nil
86+
}
87+
}
88+
}
89+
assert.are.same expected, result
90+
91+
describe 'can sort data', ->
92+
pending 'can sort single number', ->
93+
tree = BinarySearchTree {'2'}
94+
result = tree\sorted!
95+
expected = {'2'}
96+
assert.are.same expected, result
97+
98+
pending 'can sort if second number is smaller than first', ->
99+
tree = BinarySearchTree {'2', '1'}
100+
result = tree\sorted!
101+
expected = {'1', '2'}
102+
assert.are.same expected, result
103+
104+
pending 'can sort if second number is same as first', ->
105+
tree = BinarySearchTree {'2', '2'}
106+
result = tree\sorted!
107+
expected = {'2', '2'}
108+
assert.are.same expected, result
109+
110+
pending 'can sort if second number is greater than first', ->
111+
tree = BinarySearchTree {'2', '3'}
112+
result = tree\sorted!
113+
expected = {'2', '3'}
114+
assert.are.same expected, result
115+
116+
pending 'can sort complex tree', ->
117+
tree = BinarySearchTree {'2', '1', '3', '6', '7', '5'}
118+
result = tree\sorted!
119+
expected = {'1', '2', '3', '5', '6', '7'}
120+
assert.are.same expected, result

0 commit comments

Comments
 (0)