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

added initial implementation of Binary Search Tree exercise #588

Merged
merged 5 commits into from
Sep 14, 2023
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
16 changes: 16 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,22 @@
"lists",
"list-methods"
]
},
{
"slug": "binary-search-tree",
"name": "Binary Search Tree",
"uuid": "4f8a2a30-3bb5-4ed6-9304-5fd7211dd77d",
"practices": [],
"prerequisites": [],
"difficulty" : 6,
"topics" : [
"algorithms",
"conditionals",
"data_structures",
"recursion",
"lists",
"list-methods"
]
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion dev/src/BaselineOfExercism/BaselineOfExercism.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ BaselineOfExercism class >> exerciseGoldenTestPackageNames [
BaselineOfExercism class >> exercisePackageNames [
"Answer the list of exercise package names (as we don't yet have proper projects)"

^ #('Exercise@Acronym' 'Exercise@Allergies' 'Exercise@Anagram' 'Exercise@ArmstrongNumbers' 'Exercise@AtbashCipher' 'Exercise@Binary' 'Exercise@Bowling' 'Exercise@CircularBuffer' 'Exercise@Clock' 'Exercise@CollatzConjecture' 'Exercise@Darts' 'Exercise@Diamond' 'Exercise@Die' 'Exercise@Etl' 'Exercise@FlattenArray' 'Exercise@Forth' 'Exercise@GradeSchool' 'Exercise@Grains' 'Exercise@Hamming' 'Exercise@HelloWorld' 'Exercise@HighScores' 'Exercise@IsbnVerifier' 'Exercise@Isogram' 'Exercise@Leap' 'Exercise@Luhn' 'Exercise@MatchingBrackets' 'Exercise@Matrix' 'Exercise@Minesweeper' 'Exercise@Pangram' 'Exercise@Proverb' 'Exercise@Raindrops' 'Exercise@ResistorColorDuo' 'Exercise@ReverseString' 'Exercise@RobotSimulator' 'Exercise@RomanNumerals' 'Exercise@SecretHandshake' 'Exercise@Sieve' 'Exercise@SpaceAge' 'Exercise@SumOfMultiples' 'Exercise@Tournament' 'Exercise@TwelveDays' 'Exercise@TwoFer' 'Exercise@Welcome' 'Exercise@WordCount')
^ #('Exercise@Acronym' 'Exercise@Allergies' 'Exercise@Anagram' 'Exercise@ArmstrongNumbers' 'Exercise@AtbashCipher' 'Exercise@Binary' 'Exercise@BinarySearchTree' 'Exercise@Bowling' 'Exercise@CircularBuffer' 'Exercise@Clock' 'Exercise@CollatzConjecture' 'Exercise@Darts' 'Exercise@Diamond' 'Exercise@Die' 'Exercise@Etl' 'Exercise@FlattenArray' 'Exercise@Forth' 'Exercise@GradeSchool' 'Exercise@Grains' 'Exercise@Hamming' 'Exercise@HelloWorld' 'Exercise@HighScores' 'Exercise@IsbnVerifier' 'Exercise@Isogram' 'Exercise@Leap' 'Exercise@Luhn' 'Exercise@MatchingBrackets' 'Exercise@Matrix' 'Exercise@Minesweeper' 'Exercise@Pangram' 'Exercise@Proverb' 'Exercise@Raindrops' 'Exercise@ResistorColorDuo' 'Exercise@ReverseString' 'Exercise@RobotSimulator' 'Exercise@RomanNumerals' 'Exercise@SecretHandshake' 'Exercise@Sieve' 'Exercise@SpaceAge' 'Exercise@SumOfMultiples' 'Exercise@Tournament' 'Exercise@TwelveDays' 'Exercise@TwoFer' 'Exercise@Welcome' 'Exercise@WordCount')
]

{ #category : #baselines }
Expand Down
93 changes: 93 additions & 0 deletions dev/src/Exercise@BinarySearchTree/BinarySearchTree.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"
I represent Binary Search Tree. I can insert data and have references to left and right subtrees that are of same kind as me. I support operations like: data, left and right. I have sortedData operation to return sorted data elements.
"
Class {
#name : #BinarySearchTree,
#superclass : #Object,
#instVars : [
'data',
'left',
'right'
],
#category : #'Exercise@BinarySearchTree'
}

{ #category : #adding }
BinarySearchTree >> addNode: aString [

self data ifNil: [^ self data: aString].
aString <= self data
ifTrue: [ self getOrCreateLeftNode addNode: aString ]
ifFalse: [ self getOrCreateRightNode addNode: aString ]
]

{ #category : #accessing }
BinarySearchTree >> data [

^ data
]

{ #category : #private }
BinarySearchTree >> data: anObject [

data := anObject
]

{ #category : #private }
BinarySearchTree >> getOrCreateLeftNode [

^ self left ifNil: [ self left: self class new. self left ]
]

{ #category : #private }
BinarySearchTree >> getOrCreateRightNode [

^ self right ifNil: [ self right: self class new. self right ]
]

{ #category : #accessing }
BinarySearchTree >> left [

^ left
]

{ #category : #private }
BinarySearchTree >> left: anObject [

left := anObject
]

{ #category : #exercism }
BinarySearchTree >> leftMostOrderOn: resultCollection [

self left ifNotNil: [ self left leftMostOrderOn: resultCollection ].
resultCollection add: self data.
self right ifNotNil: [ self right leftMostOrderOn: resultCollection ].
]

{ #category : #accessing }
BinarySearchTree >> right [

^ right
]

{ #category : #private }
BinarySearchTree >> right: anObject [

right := anObject
]

{ #category : #exercism }
BinarySearchTree >> sortedData [

|resultCollection|
resultCollection := OrderedCollection new.
self leftMostOrderOn: resultCollection.
^ resultCollection asArray
]

{ #category : #input }
BinarySearchTree >> treeData: aCollection [

aCollection do: [:element | self addNode: element]
]
234 changes: 234 additions & 0 deletions dev/src/Exercise@BinarySearchTree/BinarySearchTreeTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
"
# BinarySearchTree

# Description

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

## Hint

Yo may want to use objects of the same class that represent tree and subtrees (nodes). Try to use recursion method for adding and sorting nodes.

"
Class {
#name : #BinarySearchTreeTest,
#superclass : #ExercismTest,
#instVars : [
'binarySearchTreeCalculator'
],
#category : #'Exercise@BinarySearchTree'
}

{ #category : #config }
BinarySearchTreeTest class >> exercise [

^(ExercismExercise for: self)
isCore: false;
isAutoApproved: true;
difficulty: 6;
topics: #('algorithms' 'conditionals' 'data_structures' 'recursion');
yourself
]

{ #category : #config }
BinarySearchTreeTest class >> uuid [
"Answer a unique id for this exercise"
^'4f8a2a30-3bb5-4ed6-9304-5fd7211dd77d'
]

{ #category : #config }
BinarySearchTreeTest class >> version [
"Generated from specification: 13 September 2023"
^'Not specified'
]

{ #category : #running }
BinarySearchTreeTest >> setUp [
super setUp.
binarySearchTreeCalculator := BinarySearchTree new
]

{ #category : #tests }
BinarySearchTreeTest >> test01_DataIsRetained [
"Tip: Remember to review the class [Comment] tab"
<exeTestName: 'data is retained'>
<exeTestUUID: 'e9c93a78-c536-4750-a336-94583d23fafa'>

| result |

result := binarySearchTreeCalculator treeData: #('4').
self assert: result data equals: '4'.
self assert: result right isNil.
self assert: result left isNil.

]

{ #category : #tests }
BinarySearchTreeTest >> test02_InsertDataAtProperNodeSmallerNumberAtLeftNode [
<exeTestName: 'smaller number at left node'>
<exeTestUUID: '7a95c9e8-69f6-476a-b0c4-4170cb3f7c91'>

| result |

result := binarySearchTreeCalculator treeData: #('4' '2').
self assert: result data equals: '4'.
self assert: result right isNil.
self assert: result left data equals: '2'.
self assert: result left right isNil.
self assert: result left left isNil.
]

{ #category : #tests }
BinarySearchTreeTest >> test03_InsertDataAtProperNodeSameNumberAtLeftNode [
<exeTestName: 'same number at left node'>
<exeTestUUID: '22b89499-9805-4703-a159-1a6e434c1585'>

| result |

result := binarySearchTreeCalculator treeData: #('4' '4').
self assert: result data equals: '4'.
self assert: result right isNil.
self assert: result left data equals: '4'.
self assert: result left right isNil.
self assert: result left left isNil.
]

{ #category : #tests }
BinarySearchTreeTest >> test04_InsertDataAtProperNodeGreaterNumberAtRightNode [
<exeTestName: 'greater number at right node'>
<exeTestUUID: '2e85fdde-77b1-41ed-b6ac-26ce6b663e34'>

| result |

result := binarySearchTreeCalculator treeData: #('4' '5') .
self assert: result data equals: '4'.
self assert: result right data equals: '5'.
self assert: result right right isNil.
self assert: result right left isNil.
self assert: result left isNil.
]

{ #category : #tests }
BinarySearchTreeTest >> test05_CanCreateComplexTree [
<exeTestName: 'can create complex tree'>
<exeTestUUID: 'dd898658-40ab-41d0-965e-7f145bf66e0b'>

| result |

result := binarySearchTreeCalculator treeData: #('4' '2' '6' '1' '3' '5' '7').
self assert: result data equals: '4'.
self assert: result right data equals: '6'.
self assert: result right right data equals: '7'.
self assert: result right right right isNil.
self assert: result right right left isNil.
self assert: result right left data equals: '5'.
self assert: result right left right isNil.
self assert: result right left left isNil.
self assert: result left data equals: '2'.
self assert: result left right data equals: '3'.
self assert: result left right right isNil.
self assert: result left right left isNil.
self assert: result left left data equals: '1'.
self assert: result left left right isNil.
self assert: result left left left isNil.

]

{ #category : #tests }
BinarySearchTreeTest >> test06_CanSortDataCanSortSingleNumber [
<exeTestName: 'can sort single number'>
<exeTestUUID: '9e0c06ef-aeca-4202-b8e4-97f1ed057d56'>

| result |

result := binarySearchTreeCalculator treeData: #('2') .
self assert: result sortedData equals: #('2')
]

{ #category : #tests }
BinarySearchTreeTest >> test07_CanSortDataCanSortIfSecondNumberIsSmallerThanFirst [
<exeTestName: 'can sort if second number is smaller than first'>
<exeTestUUID: '425e6d07-fceb-4681-a4f4-e46920e380bb'>

| result |

result := binarySearchTreeCalculator treeData: #('2' '1') .
self assert: result sortedData equals: #('1' '2')
]

{ #category : #tests }
BinarySearchTreeTest >> test08_CanSortDataCanSortIfSecondNumberIsSameAsFirst [
<exeTestName: 'can sort if second number is same as first'>
<exeTestUUID: 'bd7532cc-6988-4259-bac8-1d50140079ab'>

| result |

result := binarySearchTreeCalculator treeData: #('2' '2') .
self assert: result sortedData equals: #('2' '2')
]

{ #category : #tests }
BinarySearchTreeTest >> test09_CanSortDataCanSortIfSecondNumberIsGreaterThanFirst [
<exeTestName: 'can sort if second number is greater than first'>
<exeTestUUID: 'b6d1b3a5-9d79-44fd-9013-c83ca92ddd36'>

| result |

result := binarySearchTreeCalculator treeData: #('2' '3') .
self assert: result sortedData equals: #('2' '3')
]

{ #category : #tests }
BinarySearchTreeTest >> test10_CanSortDataCanSortComplexTree [
<exeTestName: 'can sort complex tree'>
<exeTestUUID: 'd00ec9bd-1288-4171-b968-d44d0808c1c8'>

| result |

result := binarySearchTreeCalculator treeData: #('2' '1' '3' '6' '7' '5') .
self assert: result sortedData equals: #('1' '2' '3' '5' '6' '7')
]
1 change: 1 addition & 0 deletions dev/src/Exercise@BinarySearchTree/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'Exercise@BinarySearchTree' }
Loading