Skip to content

Commit 436a51b

Browse files
authored
Merge pull request #6 from Alokzh/fix/bst-implementation-improvements
Improve BST Implementation by adding essential methods & fixing parent ref. issues
2 parents 09aa48f + dfe7957 commit 436a51b

File tree

5 files changed

+164
-97
lines changed

5 files changed

+164
-97
lines changed

src/Containers-BinarySearchTree-Tests/CTBinarySearchTreeTest.class.st

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ CTBinarySearchTreeTest >> testAsArray [
4848
self assert: result equals: #(20 30 40 50 70)
4949
]
5050

51+
{ #category : 'tests' }
52+
CTBinarySearchTreeTest >> testAtIfAbsent [
53+
54+
tree addAll: #(50 30 70).
55+
self assert: (tree at: 30 ifAbsent: [ #notFound ]) equals: 30.
56+
self assert: (tree at: 99 ifAbsent: [ #notFound ]) equals: #notFound
57+
]
58+
5159
{ #category : 'tests' }
5260
CTBinarySearchTreeTest >> testBSTValidation [
5361

@@ -186,12 +194,13 @@ CTBinarySearchTreeTest >> testEmptyTreeOperations [
186194
]
187195

188196
{ #category : 'tests' }
189-
CTBinarySearchTreeTest >> testFindMinMax [
197+
CTBinarySearchTreeTest >> testFirstLast [
190198

191199
tree addAll: #(50 30 70 20 80).
192-
193-
self assert: tree findMin equals: 20.
194-
self assert: tree findMax equals: 80
200+
self assert: tree first equals: 20.
201+
self assert: tree last equals: 80.
202+
self assert: CTBinarySearchTree new first isNil.
203+
self assert: CTBinarySearchTree new last isNil
195204
]
196205

197206
{ #category : 'tests' }
@@ -362,16 +371,20 @@ CTBinarySearchTreeTest >> testRemoveNonExistentElement [
362371
{ #category : 'tests' }
363372
CTBinarySearchTreeTest >> testRemoveRoot [
364373
"Root with no children"
374+
365375
tree add: 50.
366376
tree remove: 50.
367377
self assert: tree isEmpty.
368-
378+
369379
"Root with two children"
370380
tree clear.
371-
tree addAll: #(50 30 70 20 40 60 80).
381+
tree addAll: #( 50 30 70 20 40 60 80 ).
372382
tree remove: 50.
373383
self assert: tree size equals: 6.
374384
self deny: (tree includes: 50).
385+
386+
self assert: (tree includes: 60).
387+
self assert: tree root contents equals: 60
375388
]
376389

377390
{ #category : 'tests' }

src/Containers-BinarySearchTree/CTBSTAbstractNode.class.st

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,24 @@ CTBSTAbstractNode >> findMax [
5757
^ self subclassResponsibility
5858
]
5959

60+
{ #category : 'searching' }
61+
CTBSTAbstractNode >> findMaxNode [
62+
63+
^ self subclassResponsibility
64+
]
65+
6066
{ #category : 'searching' }
6167
CTBSTAbstractNode >> findMin [
6268

6369
^ self subclassResponsibility
6470
]
6571

72+
{ #category : 'searching' }
73+
CTBSTAbstractNode >> findMinNode [
74+
75+
^ self subclassResponsibility
76+
]
77+
6678
{ #category : 'accessing' }
6779
CTBSTAbstractNode >> height [
6880

src/Containers-BinarySearchTree/CTBSTNillNode.class.st renamed to src/Containers-BinarySearchTree/CTBSTNilNode.class.st

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ I provide default 'do nothing' behavior for all tree operations, eliminating the
66
When elements are added to me, I create and return a new CTBSTNode containing the element, effectively growing the tree.
77
"
88
Class {
9-
#name : 'CTBSTNillNode',
9+
#name : 'CTBSTNilNode',
1010
#superclass : 'CTBSTAbstractNode',
1111
#category : 'Containers-BinarySearchTree',
1212
#package : 'Containers-BinarySearchTree'
1313
}
1414

1515
{ #category : 'adding' }
16-
CTBSTNillNode >> addChild: anObject [
16+
CTBSTNilNode >> addChild: anObject [
1717

1818
^ CTBSTNode new
1919
contents: anObject;
@@ -22,122 +22,134 @@ CTBSTNillNode >> addChild: anObject [
2222
]
2323

2424
{ #category : 'accessing' }
25-
CTBSTNillNode >> contents [
25+
CTBSTNilNode >> contents [
2626

2727
^ nil
2828
]
2929

3030
{ #category : 'accessing' }
31-
CTBSTNillNode >> contents: anObject [
31+
CTBSTNilNode >> contents: anObject [
3232

3333
"Do nothing for nil node"
3434
]
3535

3636
{ #category : 'enumerating' }
37-
CTBSTNillNode >> elementsFrom: min to: max into: aCollection [
37+
CTBSTNilNode >> elementsFrom: min to: max into: aCollection [
3838

3939
"Do nothing for nill node"
4040
]
4141

4242
{ #category : 'enumerating' }
43-
CTBSTNillNode >> elementsGreaterThan: anObject into: aCollection [
43+
CTBSTNilNode >> elementsGreaterThan: anObject into: aCollection [
4444

4545
"Do nothing for nill node"
4646
]
4747

4848
{ #category : 'enumerating' }
49-
CTBSTNillNode >> elementsLessThan: anObject into: aCollection [
49+
CTBSTNilNode >> elementsLessThan: anObject into: aCollection [
5050

5151
"Do nothing for nill node"
5252
]
5353

5454
{ #category : 'searching' }
55-
CTBSTNillNode >> findMax [
55+
CTBSTNilNode >> findMax [
5656

5757
^ nil
5858
]
5959

6060
{ #category : 'searching' }
61-
CTBSTNillNode >> findMin [
61+
CTBSTNilNode >> findMaxNode [
62+
63+
^ self
64+
]
65+
66+
{ #category : 'searching' }
67+
CTBSTNilNode >> findMin [
6268

6369
^ nil
6470
]
6571

72+
{ #category : 'searching' }
73+
CTBSTNilNode >> findMinNode [
74+
75+
^ self
76+
]
77+
6678
{ #category : 'accessing' }
67-
CTBSTNillNode >> height [
79+
CTBSTNilNode >> height [
6880

6981
^ 0
7082
]
7183

7284
{ #category : 'enumerating' }
73-
CTBSTNillNode >> inOrderDo: aBlock [
85+
CTBSTNilNode >> inOrderDo: aBlock [
7486

7587
"Do nothing for nil node"
7688
]
7789

7890
{ #category : 'testing' }
79-
CTBSTNillNode >> isEmpty [
91+
CTBSTNilNode >> isEmpty [
8092

8193
^ true
8294
]
8395

8496
{ #category : 'testing' }
85-
CTBSTNillNode >> isLeaf [
97+
CTBSTNilNode >> isLeaf [
8698

8799
^ false
88100
]
89101

90102
{ #category : 'enumerating' }
91-
CTBSTNillNode >> postOrderDo: aBlock [
103+
CTBSTNilNode >> postOrderDo: aBlock [
92104

93105
"Do nothing for nill node"
94106
]
95107

96108
{ #category : 'enumerating' }
97-
CTBSTNillNode >> preOrderDo: aBlock [
109+
CTBSTNilNode >> preOrderDo: aBlock [
98110

99111
"Do nothing for nill node"
100112
]
101113

102114
{ #category : 'searching' }
103-
CTBSTNillNode >> predecessorOf: anObject [
115+
CTBSTNilNode >> predecessorOf: anObject [
104116

105117
^ nil
106118
]
107119

108120
{ #category : 'removing' }
109-
CTBSTNillNode >> removeValue: anObject [
121+
CTBSTNilNode >> removeValue: anObject [
110122

111123
"Element not found - return self unchanged"
112124
^ self
113125
]
114126

115127
{ #category : 'accessing' }
116-
CTBSTNillNode >> search: anObject [
128+
CTBSTNilNode >> search: anObject [
117129

118130
^ nil
119131
]
120132

121133
{ #category : 'accessing' }
122-
CTBSTNillNode >> size [
134+
CTBSTNilNode >> size [
123135

124136
^ 0
125137
]
126138

127139
{ #category : 'searching' }
128-
CTBSTNillNode >> successorOf: anObject [
140+
CTBSTNilNode >> successorOf: anObject [
129141

130142
^ nil
131143
]
132144

133145
{ #category : 'testing' }
134-
CTBSTNillNode >> validateBSTProperty [
146+
CTBSTNilNode >> validateBSTProperty [
135147

136148
^ true
137149
]
138150

139151
{ #category : 'private' }
140-
CTBSTNillNode >> validateBSTPropertyWithMin: min max: max [
152+
CTBSTNilNode >> validateBSTPropertyWithMin: min max: max [
141153

142154
^ true
143155
]

0 commit comments

Comments
 (0)