Skip to content
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 @@ -810,6 +810,14 @@
],
"difficulty": 5
},
{
"slug": "binary-search-tree",
"name": "Binary Search Tree",
"uuid": "f3b38287-45f2-4e17-b9a8-70382521f15c",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "bob",
"name": "Bob",
Expand Down
1 change: 1 addition & 0 deletions exercises/Exercises.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Project Path="practice/bank-account/BankAccount.vbproj" />
<Project Path="practice/beer-song/BeerSong.vbproj" />
<Project Path="practice/binary-search/BinarySearch.vbproj" />
<Project Path="practice/binary-search-tree/BinarySearchTree.vbproj" />
<Project Path="practice/binary/Binary.vbproj" />
<Project Path="practice/bob/Bob.vbproj" />
<Project Path="practice/book-store/BookStore.vbproj" />
Expand Down
70 changes: 70 additions & 0 deletions exercises/practice/binary-search-tree/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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
89 changes: 89 additions & 0 deletions exercises/practice/binary-search-tree/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
###############################
# Core EditorConfig Options #
###############################

; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2022
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2022

root = true

[*]
end_of_line = lf
indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true

[*.{vbproj,slnx}]
indent_size = 2

[*.vb]
indent_size = 4
charset = utf-8

###############################
# .NET Coding Conventions #
###############################

# Organize imports
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

# Me. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion

# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion

# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion

###############################
# Naming Conventions #
###############################

# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const

###################################
# Visual Basic Coding Conventions #
###################################
visual_basic_preferred_modifier_order = Partial, Default, Private, Protected, Public, Friend, NotOverridable, Overridable, MustOverride, Overloads, Overrides, MustInherit, NotInheritable, Static, Shared, Shadows, ReadOnly, WriteOnly, Dim, Const, WithEvents, Widening, Narrowing, Custom, Async:suggestion
visual_basic_style_unused_variable_expression_statement_preference = unused_local_variable:suggestion
visual_basic_style_unused_value_assignment_preference = unused_local_variable:suggestion
visual_basic_style_prefer_isnot_expression = true:suggestion
visual_basic_style_prefer_simplified_object_creation = false:none
dotnet_diagnostic.IDE0090.severity = none
43 changes: 43 additions & 0 deletions exercises/practice/binary-search-tree/.meta/Example.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Public Class BinarySearchTree(Of T As IComparable(Of T))
Public Sub New(ByVal data As T)
Me.Data = data
End Sub

Public ReadOnly Property Data As T
Public Property Left As BinarySearchTree(Of T)
Public Property Right As BinarySearchTree(Of T)

Public Sub Insert(ByVal value As T)
If value.CompareTo(Data) <= 0 Then
If Left Is Nothing Then
Left = New BinarySearchTree(Of T)(value)
Else
Left.Insert(value)
End If
ElseIf Right Is Nothing Then
Right = New BinarySearchTree(Of T)(value)
Else
Right.Insert(value)
End If
End Sub

Public Function SortedData() As IEnumerable(Of T)
Dim values As New List(Of T)

AddSortedData(values)

Return values
End Function

Private Sub AddSortedData(ByVal values As List(Of T))
If Left IsNot Nothing Then
Left.AddSortedData(values)
End If

values.Add(Data)

If Right IsNot Nothing Then
Right.AddSortedData(values)
End If
End Sub
End Class
18 changes: 18 additions & 0 deletions exercises/practice/binary-search-tree/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"BinarySearchTree.vb"
],
"test": [
"BinarySearchTreeTests.vb"
],
"example": [
".meta/Example.vb"
]
},
"blurb": "Insert and search for numbers in a binary tree.",
"source": "Josh Cheek"
}
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"
17 changes: 17 additions & 0 deletions exercises/practice/binary-search-tree/BinarySearchTree.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Public Class BinarySearchTree(Of T As IComparable(Of T))
Public Sub New(ByVal data As T)
Throw New NotImplementedException("You need to implement this function.")
End Sub

Public ReadOnly Property Data As T
Public Property Left As BinarySearchTree(Of T)
Public Property Right As BinarySearchTree(Of T)

Public Sub Insert(ByVal value As T)
Throw New NotImplementedException("You need to implement this function.")
End Sub

Public Function SortedData() As IEnumerable(Of T)
Throw New NotImplementedException("You need to implement this function.")
End Function
End Class
19 changes: 19 additions & 0 deletions exercises/practice/binary-search-tree/BinarySearchTree.vbproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<Import Include="Xunit" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
<PackageReference Include="xunit.v3" Version="3.2.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
</ItemGroup>

</Project>
Loading