Skip to content

Commit 28d1263

Browse files
authored
Merge pull request #43 from HossamSaberr/refactor-nil-pattern
Refactor: replace isNilNode with polymorphic
2 parents 432edc4 + 1533013 commit 28d1263

3 files changed

Lines changed: 13 additions & 17 deletions

File tree

src/Containers-AVL-Tree/CTAVLAbstractNode.class.st

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,6 @@ CTAVLAbstractNode >> isLeaf [
119119
^ self subclassResponsibility
120120
]
121121

122-
{ #category : 'testing' }
123-
CTAVLAbstractNode >> isNilNode [
124-
125-
^ false
126-
]
127-
128122
{ #category : 'accessing' }
129123
CTAVLAbstractNode >> parent [
130124

src/Containers-AVL-Tree/CTAVLNode.class.st

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ CTAVLNode >> balanceFactor [
4444

4545
{ #category : 'accessing' }
4646
CTAVLNode >> children [
47-
48-
^ { left . right }
47+
| validChildren |
48+
validChildren := OrderedCollection new: 2.
49+
left isEmpty ifFalse: [ validChildren add: left ].
50+
right isEmpty ifFalse: [ validChildren add: right ].
51+
^ validChildren asArray
4952
]
5053

5154
{ #category : 'accessing' }
@@ -175,10 +178,10 @@ CTAVLNode >> left: aNode [
175178

176179
{ #category : 'copying' }
177180
CTAVLNode >> postCopy [
178-
super postCopy.
179-
180-
left isNilNode ifFalse: [ left := left copy ].
181-
right isNilNode ifFalse: [ right := right copy ]
181+
super postCopy.
182+
183+
left := left copy.
184+
right := right copy
182185
]
183186

184187
{ #category : 'enumerating' }

src/Containers-AVL-Tree/CTAVLTree.class.st

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ CTAVLTree >> addAll: aCollection [
4040
CTAVLTree >> allChildren [
4141
| allNodes queue currentNode |
4242
allNodes := OrderedCollection new.
43-
self root isNilNode ifTrue: [ ^ allNodes ].
43+
self root isEmpty ifTrue: [ ^ allNodes ].
4444

4545
queue := OrderedCollection with: self root.
4646
[ queue isNotEmpty ] whileTrue: [
4747
currentNode := queue removeFirst.
4848
allNodes add: currentNode.
49-
currentNode left isNilNode ifFalse: [ queue add: currentNode left ].
50-
currentNode right isNilNode ifFalse: [ queue add: currentNode right ]
49+
queue addAll: currentNode children
5150
].
5251

5352
^ allNodes
@@ -231,8 +230,8 @@ CTAVLTree >> last [
231230

232231
{ #category : 'copying' }
233232
CTAVLTree >> postCopy [
234-
super postCopy.
235-
root := root copy.
233+
super postCopy.
234+
root := root copy.
236235
]
237236

238237
{ #category : 'enumerating' }

0 commit comments

Comments
 (0)