From 665644d139a15cb0eab343c890898e52aacbe431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Thu, 9 Jul 2026 20:04:10 -0700 Subject: [PATCH] add `binary-search-tree` --- config.json | 8 ++ .../binary-search-tree/.docs/instructions.md | 70 ++++++++++++++++++ .../practice/binary-search-tree/.gitignore | 3 + .../binary-search-tree/.meta/config.json | 18 +++++ .../binary-search-tree/.meta/example.arr | 30 ++++++++ .../binary-search-tree/.meta/tests.toml | 40 ++++++++++ .../binary-search-tree-test.arr | 73 +++++++++++++++++++ .../binary-search-tree/binary-search-tree.arr | 26 +++++++ .../practice/binary-search-tree/package.json | 21 ++++++ 9 files changed, 289 insertions(+) create mode 100644 exercises/practice/binary-search-tree/.docs/instructions.md create mode 100644 exercises/practice/binary-search-tree/.gitignore create mode 100644 exercises/practice/binary-search-tree/.meta/config.json create mode 100644 exercises/practice/binary-search-tree/.meta/example.arr create mode 100644 exercises/practice/binary-search-tree/.meta/tests.toml create mode 100644 exercises/practice/binary-search-tree/binary-search-tree-test.arr create mode 100644 exercises/practice/binary-search-tree/binary-search-tree.arr create mode 100644 exercises/practice/binary-search-tree/package.json diff --git a/config.json b/config.json index f4b9a22..bea9f77 100644 --- a/config.json +++ b/config.json @@ -569,6 +569,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "binary-search-tree", + "name": "Binary Search Tree", + "uuid": "4caa6234-719f-4548-b4ee-521407248751", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "camicia", "name": "Camicia", diff --git a/exercises/practice/binary-search-tree/.docs/instructions.md b/exercises/practice/binary-search-tree/.docs/instructions.md new file mode 100644 index 0000000..7625220 --- /dev/null +++ b/exercises/practice/binary-search-tree/.docs/instructions.md @@ -0,0 +1,70 @@ +# 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: + +![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) + +```text + 4 + / + 2 +``` + +If we then added 6, it would look like this: + +![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) + +```text + 4 + / \ + 2 6 +``` + +If we then added 3, it would look like this + +![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) + +```text + 4 + / \ + 2 6 + \ + 3 +``` + +And if we then added 1, 5, and 7, it would look like this + +![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) + +```text + 4 + / \ + / \ + 2 6 + / \ / \ + 1 3 5 7 +``` + +## Credit + +The images were created by [habere-et-dispertire][habere-et-dispertire] using [PGF/TikZ][pgf-tikz] by Till Tantau. + +[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire +[pgf-tikz]: https://en.wikipedia.org/wiki/PGF/TikZ diff --git a/exercises/practice/binary-search-tree/.gitignore b/exercises/practice/binary-search-tree/.gitignore new file mode 100644 index 0000000..3cd67f4 --- /dev/null +++ b/exercises/practice/binary-search-tree/.gitignore @@ -0,0 +1,3 @@ +.pyret/ +*.jarr +node_modules diff --git a/exercises/practice/binary-search-tree/.meta/config.json b/exercises/practice/binary-search-tree/.meta/config.json new file mode 100644 index 0000000..7648877 --- /dev/null +++ b/exercises/practice/binary-search-tree/.meta/config.json @@ -0,0 +1,18 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "binary-search-tree.arr" + ], + "test": [ + "binary-search-tree-test.arr" + ], + "example": [ + ".meta/example.arr" + ] + }, + "blurb": "Insert and search for numbers in a binary tree.", + "source": "Josh Cheek" +} diff --git a/exercises/practice/binary-search-tree/.meta/example.arr b/exercises/practice/binary-search-tree/.meta/example.arr new file mode 100644 index 0000000..a6a00a7 --- /dev/null +++ b/exercises/practice/binary-search-tree/.meta/example.arr @@ -0,0 +1,30 @@ +use context starter2024 + +provide-types * + +provide: binary-search-tree end + +data BinarySearchTree: + | empty-tree with: + method insert(_, new-value): + tree-node(new-value, empty-tree, empty-tree) + end, + method sorted-data(_): + [list: ] + end + | tree-node(value, left, right) with: + method insert(self, new-value): + if new-value <= self.value: + tree-node(self.value, self.left.insert(new-value), self.right) + else: + tree-node(self.value, self.left, self.right.insert(new-value)) + end + end, + method sorted-data(self): + self.left.sorted-data() + [list: self.value] + self.right.sorted-data() + end +end + +fun binary-search-tree(values): + values.foldl(lam(value, tree): tree.insert(value) end, empty-tree) +end diff --git a/exercises/practice/binary-search-tree/.meta/tests.toml b/exercises/practice/binary-search-tree/.meta/tests.toml new file mode 100644 index 0000000..c7d3202 --- /dev/null +++ b/exercises/practice/binary-search-tree/.meta/tests.toml @@ -0,0 +1,40 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[e9c93a78-c536-4750-a336-94583d23fafa] +description = "data is retained" + +[7a95c9e8-69f6-476a-b0c4-4170cb3f7c91] +description = "insert data at proper node -> smaller number at left node" + +[22b89499-9805-4703-a159-1a6e434c1585] +description = "insert data at proper node -> same number at left node" + +[2e85fdde-77b1-41ed-b6ac-26ce6b663e34] +description = "insert data at proper node -> greater number at right node" + +[dd898658-40ab-41d0-965e-7f145bf66e0b] +description = "can create complex tree" + +[9e0c06ef-aeca-4202-b8e4-97f1ed057d56] +description = "can sort data -> can sort single number" + +[425e6d07-fceb-4681-a4f4-e46920e380bb] +description = "can sort data -> can sort if second number is smaller than first" + +[bd7532cc-6988-4259-bac8-1d50140079ab] +description = "can sort data -> can sort if second number is same as first" + +[b6d1b3a5-9d79-44fd-9013-c83ca92ddd36] +description = "can sort data -> can sort if second number is greater than first" + +[d00ec9bd-1288-4171-b968-d44d0808c1c8] +description = "can sort data -> can sort complex tree" diff --git a/exercises/practice/binary-search-tree/binary-search-tree-test.arr b/exercises/practice/binary-search-tree/binary-search-tree-test.arr new file mode 100644 index 0000000..baf8206 --- /dev/null +++ b/exercises/practice/binary-search-tree/binary-search-tree-test.arr @@ -0,0 +1,73 @@ +use context starter2024 + +include file("binary-search-tree.arr") + +check "data is retained": + binary-search-tree([list: 4]) is tree-node(4, empty-tree, empty-tree) +end + +check "insert data at proper node -> smaller number at left node": + expected = tree-node(4, + tree-node(2, empty-tree, empty-tree), + empty-tree) + + binary-search-tree([list: 4, 2]) is expected +end + +check "insert data at proper node -> same number at left node": + expected = tree-node(4, + tree-node(4, empty-tree, empty-tree), + empty-tree) + + binary-search-tree([list: 4, 4]) is expected +end + +check "insert data at proper node -> greater number at right node": + expected = tree-node(4, + empty-tree, + tree-node(5, empty-tree, empty-tree)) + + binary-search-tree([list: 4, 5]) is expected +end + +check "can create complex tree": + expected = tree-node(4, + tree-node(2, + tree-node(1, empty-tree, empty-tree), + tree-node(3, empty-tree, empty-tree)), + tree-node(6, + tree-node(5, empty-tree, empty-tree), + tree-node(7, empty-tree, empty-tree))) + + binary-search-tree([list: 4, 2, 6, 1, 3, 5, 7]) is expected +end + +check "can sort data -> can sort single number": + tree = binary-search-tree([list: 2]) + + tree.sorted-data() is [list: 2] +end + +check "can sort data -> can sort if second number is smaller than first": + tree = binary-search-tree([list: 2, 1]) + + tree.sorted-data() is [list: 1, 2] +end + +check "can sort data -> can sort if second number is same as first": + tree = binary-search-tree([list: 2, 2]) + + tree.sorted-data() is [list: 2, 2] +end + +check "can sort data -> can sort if second number is greater than first": + tree = binary-search-tree([list: 2, 3]) + + tree.sorted-data() is [list: 2, 3] +end + +check "can sort data -> can sort complex tree": + tree = binary-search-tree([list: 2, 1, 3, 6, 7, 5]) + + tree.sorted-data() is [list: 1, 2, 3, 5, 6, 7] +end diff --git a/exercises/practice/binary-search-tree/binary-search-tree.arr b/exercises/practice/binary-search-tree/binary-search-tree.arr new file mode 100644 index 0000000..bfad15d --- /dev/null +++ b/exercises/practice/binary-search-tree/binary-search-tree.arr @@ -0,0 +1,26 @@ +use context starter2024 + +provide-types * + +provide: binary-search-tree end + +data BinarySearchTree: + | empty-tree with: + method insert(_, new-value): + raise("please implement the insert method") + end, + method sorted-data(_): + raise("please implement the sorted-data method") + end + | tree-node(value, left, right) with: + method insert(_, new-value): + raise("please implement the insert method") + end, + method sorted-data(_): + raise("please implement the sorted-data method") + end +end + +fun binary-search-tree(values): + raise("please implement the binary-search-tree function") +end diff --git a/exercises/practice/binary-search-tree/package.json b/exercises/practice/binary-search-tree/package.json new file mode 100644 index 0000000..a619ffd --- /dev/null +++ b/exercises/practice/binary-search-tree/package.json @@ -0,0 +1,21 @@ +{ + "name": "@exercism/pyret", + "description": "Exercism exercises in Pyret.", + "author": "Katrina Owen", + "contributors": [ + "BNAndras" + ], + "private": true, + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/exercism/pyret" + }, + "scripts": { + "test": "pyret *-test.arr" + }, + "dependencies": { + "pyret-npm": "0.0.90" + }, + "packageManager": "npm@11.13.0" +}