Skip to content

Commit 28fbb1c

Browse files
committed
Optimize copy method from O(N log N) to O(N)
1 parent 7fe0bf5 commit 28fbb1c

4 files changed

Lines changed: 96 additions & 9 deletions

File tree

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,78 @@ CTAVLTreeTest >> testCopy [
119119
self assert: (copiedTree includes: 99)
120120
]
121121

122+
{ #category : 'tests' }
123+
CTAVLTreeTest >> testCopyEmptyTree [
124+
| copiedTree |
125+
126+
copiedTree := tree copy.
127+
128+
self assert: copiedTree isEmpty.
129+
self assert: copiedTree size equals: 0.
130+
self assert: copiedTree height equals: 0.
131+
132+
self deny: copiedTree == tree.
133+
]
134+
135+
{ #category : 'tests' }
136+
CTAVLTreeTest >> testCopyLargeTree [
137+
| copiedTree elements |
138+
139+
elements := (1 to: 1000) asArray shuffled.
140+
tree addAll: elements.
141+
142+
copiedTree := tree copy.
143+
144+
self assert: copiedTree size equals: 1000.
145+
self assert: copiedTree height equals: tree height.
146+
self assert: copiedTree validate.
147+
148+
self assert: copiedTree asArray equals: tree asArray.
149+
]
150+
151+
{ #category : 'tests' }
152+
CTAVLTreeTest >> testCopySingleNode [
153+
| copiedTree |
154+
155+
tree add: 42.
156+
copiedTree := tree copy.
157+
158+
self assert: copiedTree size equals: 1.
159+
self assert: copiedTree height equals: 1.
160+
self assert: copiedTree root contents equals: 42.
161+
162+
self deny: copiedTree root == tree root.
163+
self assert: copiedTree validate.
164+
]
165+
166+
{ #category : 'tests' }
167+
CTAVLTreeTest >> testCopyStructureAndIsolation [
168+
| copiedTree |
169+
170+
tree addAll: #(50 30 70 20 40 60 80).
171+
copiedTree := tree copy.
172+
173+
self assert: copiedTree size equals: tree size.
174+
self assert: copiedTree height equals: tree height.
175+
self assert: copiedTree asArray equals: tree asArray.
176+
self assert: copiedTree validate.
177+
178+
self deny: copiedTree root == tree root.
179+
self deny: copiedTree root left == tree root left.
180+
181+
copiedTree add: 99.
182+
copiedTree remove: 20.
183+
184+
self assert: (copiedTree includes: 99).
185+
self deny: (tree includes: 99).
186+
187+
self deny: (copiedTree includes: 20).
188+
self assert: (tree includes: 20).
189+
190+
self assert: tree validate.
191+
self assert: copiedTree validate.
192+
]
193+
122194
{ #category : 'tests' }
123195
CTAVLTreeTest >> testDetect [
124196

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ CTAVLNilNode >> contents: anObject [
3737
"Do nothing for nil node"
3838
]
3939

40+
{ #category : 'copying' }
41+
CTAVLNilNode >> copy [
42+
^ self class new
43+
]
44+
4045
{ #category : 'enumerating' }
4146
CTAVLNilNode >> elementsFrom: min to: max into: aCollection [
4247

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ 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+
6376
{ #category : 'enumerating' }
6477
CTAVLNode >> elementsFrom: min to: max into: aCollection [
6578

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,6 @@ CTAVLTree >> collect: aBlock [
9292
^ result
9393
]
9494

95-
{ #category : 'copying' }
96-
CTAVLTree >> copy [
97-
98-
| newTree |
99-
newTree := self class new.
100-
self inOrderDo: [ :each | newTree add: each ].
101-
^ newTree
102-
]
103-
10495
{ #category : 'enumerating' }
10596
CTAVLTree >> detect: aBlock ifNone: absentBlock [
10697

@@ -236,6 +227,12 @@ CTAVLTree >> last [
236227
^ self findMax
237228
]
238229

230+
{ #category : 'copying' }
231+
CTAVLTree >> postCopy [
232+
super postCopy.
233+
root := root copy.
234+
]
235+
239236
{ #category : 'enumerating' }
240237
CTAVLTree >> postOrderDo: aBlock [
241238

0 commit comments

Comments
 (0)