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

Binary search tree #739

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "binary-search-tree",
"name": "Binary Search Tree",
"uuid": "39998558-213a-4f92-953e-5764d15ddd7c",
"practices": [],
"prerequisites": [],
"difficulty": 6
}
]
},
Expand Down
47 changes: 47 additions & 0 deletions exercises/practice/binary-search-tree/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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
16 changes: 16 additions & 0 deletions exercises/practice/binary-search-tree/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"authors": [],
ErikSchierboom marked this conversation as resolved.
Show resolved Hide resolved
"files": {
"solution": [
"binary-search-tree.jl"
],
"test": [
"runtests.jl"
],
"example": [
".meta/example.jl"
]
},
"blurb": "Insert and search for numbers in a binary tree.",
"source": "Josh Cheek"
}
50 changes: 50 additions & 0 deletions exercises/practice/binary-search-tree/.meta/example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
mutable struct BinarySearchTree
data
left
right
BinarySearchTree() = new(nothing, nothing, nothing)
BinarySearchTree(node::T) where T<:Real = new(node, nothing, nothing)
end

function BinarySearchTree(vector::Vector{T}) where T<:Real
tree = BinarySearchTree()
foreach(node -> push!(tree, node), vector)
tree
end

nodedata(tree::BinarySearchTree) = tree.data
rightnode(tree::BinarySearchTree) = tree.right
leftnode(tree::BinarySearchTree) = tree.left

function Base.in(node, tree::BinarySearchTree,)
nodedata(tree) == node && return true
if node ≤ nodedata(tree)
isnothing(leftnode(tree)) ? false : in(node, leftnode(tree))
else
isnothing(rightnode(tree)) ? false : in(node, rightnode(tree))
end
end

function Base.push!(tree::BinarySearchTree, node::T) where T<:Real
if isnothing(nodedata(tree))
tree.data = node
elseif node ≤ nodedata(tree)
isnothing(leftnode(tree)) ? (tree.left = BinarySearchTree(node)) : push!(leftnode(tree), node)
else
isnothing(rightnode(tree)) ? (tree.right = BinarySearchTree(node)) : push!(rightnode(tree), node)
end
tree
end

function Base.append!(tree::BinarySearchTree, vector::Vector{T}) where T<:Real
foreach(node -> push!(tree, node), vector)
tree
end

function traverse(tree::BinarySearchTree, channel::Channel)
!isnothing(leftnode(tree)) && traverse(leftnode(tree), channel)
put!(channel, nodedata(tree))
!isnothing(rightnode(tree)) && traverse(rightnode(tree), channel)
end

Base.sort(tree::BinarySearchTree) = collect(Channel(channel -> traverse(tree, channel)))
40 changes: 40 additions & 0 deletions exercises/practice/binary-search-tree/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions exercises/practice/binary-search-tree/binary-search-tree.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Create a (Mutable) Struct BinarySearchTree, which has fields: data, left, right
# Three methods for accessing the fields have been provided to be used in testing
# Your BinarySearchTree should support the extra functionality seen in the tests


nodedata(tree::BinarySearchTree) = tree.data
rightnode(tree::BinarySearchTree) = tree.right
leftnode(tree::BinarySearchTree) = tree.left
149 changes: 149 additions & 0 deletions exercises/practice/binary-search-tree/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using Test

include("binary-search-tree.jl")

@testset verbose = true "tests" begin
@testset "instantiation with different inputs" begin
@testset "instantiation without input" begin
tree = BinarySearchTree()
@test isa(tree, BinarySearchTree)
end

@testset "instantiation with a single real number" begin
tree = BinarySearchTree(4)
@test isa(tree, BinarySearchTree)
@test nodedata(tree) == 4
@test isnothing(leftnode(tree))
@test isnothing(rightnode(tree))
end

@testset "instantiation with vector containing a single real number" begin
tree = BinarySearchTree([4])
@test isa(tree, BinarySearchTree)
@test nodedata(tree) == 4
@test isnothing(leftnode(tree))
@test isnothing(rightnode(tree))
end

@testset "instantiation with vector containing multiple real numbers" begin
tree = BinarySearchTree([4, 3, 5])
@test isa(tree, BinarySearchTree)
@test nodedata(tree) == 4
@test nodedata(leftnode(tree)) == 3
@test isnothing(leftnode(leftnode(tree)))
@test isnothing(rightnode(leftnode(tree)))
@test nodedata(rightnode(tree)) == 5
@test isnothing(leftnode(rightnode(tree)))
@test isnothing(rightnode(rightnode(tree)))
end
end

@testset "searching" begin
@testset "Single element" begin
tree = BinarySearchTree([4])
@test 4 ∈ tree
@test 5 ∉ tree
end

@testset "two elements" begin
tree = BinarySearchTree([4, 2])
@test 4 ∈ tree
@test 2 ∈ tree

tree = BinarySearchTree([4, 5])
@test 4 ∈ tree
@test 5 ∈ tree
end

@testset "complex tree" begin
tree = BinarySearchTree([4, 2, 6, 1, 3, 5, 7])
@test 4 ∈ tree
@test 2 ∈ tree
@test 6 ∈ tree
@test 1 ∈ tree
@test 3 ∈ tree
@test 5 ∈ tree
@test 7 ∈ tree
end
end

@testset "insert data at proper node" begin
@testset "smaller number at left node" begin
tree = BinarySearchTree([4])
push!(tree, 2)
@test nodedata(tree) == 4
@test nodedata(leftnode(tree)) == 2
@test isnothing(leftnode(leftnode(tree)))
@test isnothing(rightnode(leftnode(tree)))
@test isnothing(rightnode(tree))
end

@testset "same number at left node" begin
tree = BinarySearchTree([4])
push!(tree, 4)
@test nodedata(tree) == 4
@test nodedata(leftnode(tree)) == 4
@test isnothing(leftnode(leftnode(tree)))
@test isnothing(rightnode(leftnode(tree)))
@test isnothing(rightnode(tree))
end

@testset "greater number at right node" begin
tree = BinarySearchTree([4])
push!(tree, 5)
@test nodedata(tree) == 4
@test isnothing(leftnode(tree))
@test nodedata(rightnode(tree)) == 5
@test isnothing(leftnode(rightnode(tree)))
@test isnothing(rightnode(rightnode(tree)))
end
end

@testset "can create complex tree" begin
tree = BinarySearchTree([4])
foreach(node -> push!(tree, node), [2, 6, 1, 3, 5, 7])
@test nodedata(tree) == 4
@test nodedata(leftnode(tree)) == 2
@test nodedata(leftnode(leftnode(tree))) == 1
@test isnothing(leftnode(leftnode(leftnode(tree))))
@test isnothing(rightnode(leftnode(leftnode(tree))))
@test nodedata(rightnode(leftnode(tree))) == 3
@test isnothing(leftnode(rightnode(leftnode(tree))))
@test isnothing(rightnode(rightnode(leftnode(tree))))
@test nodedata(rightnode(tree)) == 6
@test nodedata(leftnode(rightnode(tree))) == 5
@test isnothing(leftnode(leftnode(rightnode(tree))))
@test isnothing(rightnode(leftnode(rightnode(tree))))
@test nodedata(rightnode(rightnode(tree))) == 7
@test isnothing(leftnode(rightnode(rightnode(tree))))
@test isnothing(rightnode(rightnode(rightnode(tree))))

end

@testset "can sort data" begin
@testset "can sort single number" begin
tree = BinarySearchTree([2])
@test sort(tree) == [2]
end

@testset "can sort if second number is smaller than first" begin
tree = BinarySearchTree([2, 1])
@test sort(tree) == [1, 2]
end

@testset "can sort if second number is same as first" begin
tree = BinarySearchTree([2, 2])
@test sort(tree) == [2, 2]
end

@testset "can sort if second number is greater than first" begin
tree = BinarySearchTree([2, 3])
@test sort(tree) == [2, 3]
end

@testset "can sort complex tree" begin
tree = BinarySearchTree([4, 2, 6, 1, 3, 5, 7])
@test sort(tree) == [1, 2, 3, 4, 5, 6, 7]
end
end
end