Skip to content

Commit da9dc67

Browse files
committed
fix: Refactor CTAVLNode copy to postCopy and add testCopyLeafNode
1 parent 28fbb1c commit da9dc67

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/Containers-AVL-Tree-Tests/CTAVLTreeTest.class.st

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ CTAVLTreeTest >> testCopyLargeTree [
148148
self assert: copiedTree asArray equals: tree asArray.
149149
]
150150

151+
{ #category : 'tests' }
152+
CTAVLTreeTest >> testCopyLeafNode [
153+
| node copiedNode |
154+
node := CTAVLNode new contents: 42.
155+
156+
copiedNode := node copy.
157+
158+
self assert: copiedNode contents equals: 42.
159+
self deny: node == copiedNode.
160+
161+
self assert: copiedNode height equals: 1.
162+
self assert: copiedNode left isNilNode.
163+
self assert: copiedNode right isNilNode
164+
]
165+
151166
{ #category : 'tests' }
152167
CTAVLTreeTest >> testCopySingleNode [
153168
| copiedTree |

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,6 @@ CTAVLNode >> contents: anObject [
6060
contents := anObject
6161
]
6262

63-
{ #category : 'copying' }
64-
CTAVLNode >> copy [
65-
| newNode |
66-
newNode := self class new.
67-
newNode contents: contents.
68-
newNode left: left copy.
69-
newNode right: right copy.
70-
71-
newNode instVarNamed: 'height' put: height.
72-
73-
^ newNode
74-
]
75-
7663
{ #category : 'enumerating' }
7764
CTAVLNode >> elementsFrom: min to: max into: aCollection [
7865

@@ -186,6 +173,14 @@ CTAVLNode >> left: aNode [
186173
aNode ifNotNil: [ aNode parent: self ]
187174
]
188175

176+
{ #category : 'copying' }
177+
CTAVLNode >> postCopy [
178+
super postCopy.
179+
180+
left isNilNode ifFalse: [ left := left copy ].
181+
right isNilNode ifFalse: [ right := right copy ]
182+
]
183+
189184
{ #category : 'enumerating' }
190185
CTAVLNode >> postOrderDo: aBlock [
191186

0 commit comments

Comments
 (0)