Skip to content
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
87 changes: 87 additions & 0 deletions src/Containers-AVL-Tree-Tests/CTAVLTreeTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,93 @@ CTAVLTreeTest >> testCopy [
self assert: (copiedTree includes: 99)
]

{ #category : 'tests' }
CTAVLTreeTest >> testCopyEmptyTree [
| copiedTree |

copiedTree := tree copy.

self assert: copiedTree isEmpty.
self assert: copiedTree size equals: 0.
self assert: copiedTree height equals: 0.

self deny: copiedTree == tree.
]

{ #category : 'tests' }
CTAVLTreeTest >> testCopyLargeTree [
| copiedTree elements |

elements := (1 to: 1000) asArray shuffled.
tree addAll: elements.

copiedTree := tree copy.

self assert: copiedTree size equals: 1000.
self assert: copiedTree height equals: tree height.
self assert: copiedTree validate.

self assert: copiedTree asArray equals: tree asArray.
]

{ #category : 'tests' }
CTAVLTreeTest >> testCopyLeafNode [
| node copiedNode |
node := CTAVLNode new contents: 42.

copiedNode := node copy.

self assert: copiedNode contents equals: 42.
self deny: node == copiedNode.

self assert: copiedNode height equals: 1.
self assert: copiedNode left isNilNode.
self assert: copiedNode right isNilNode
]

{ #category : 'tests' }
CTAVLTreeTest >> testCopySingleNode [
| copiedTree |

tree add: 42.
copiedTree := tree copy.

self assert: copiedTree size equals: 1.
self assert: copiedTree height equals: 1.
self assert: copiedTree root contents equals: 42.

self deny: copiedTree root == tree root.
self assert: copiedTree validate.
]

{ #category : 'tests' }
CTAVLTreeTest >> testCopyStructureAndIsolation [
| copiedTree |

tree addAll: #(50 30 70 20 40 60 80).
copiedTree := tree copy.

self assert: copiedTree size equals: tree size.
self assert: copiedTree height equals: tree height.
self assert: copiedTree asArray equals: tree asArray.
self assert: copiedTree validate.

self deny: copiedTree root == tree root.
self deny: copiedTree root left == tree root left.

copiedTree add: 99.
copiedTree remove: 20.

self assert: (copiedTree includes: 99).
self deny: (tree includes: 99).

self deny: (copiedTree includes: 20).
self assert: (tree includes: 20).

self assert: tree validate.
self assert: copiedTree validate.
]

{ #category : 'tests' }
CTAVLTreeTest >> testDetect [

Expand Down
5 changes: 5 additions & 0 deletions src/Containers-AVL-Tree/CTAVLNilNode.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ CTAVLNilNode >> contents: anObject [
"Do nothing for nil node"
]

{ #category : 'copying' }
CTAVLNilNode >> copy [
^ self class new
]

{ #category : 'enumerating' }
CTAVLNilNode >> elementsFrom: min to: max into: aCollection [

Expand Down
8 changes: 8 additions & 0 deletions src/Containers-AVL-Tree/CTAVLNode.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ CTAVLNode >> left: aNode [
aNode ifNotNil: [ aNode parent: self ]
]

{ #category : 'copying' }
CTAVLNode >> postCopy [
super postCopy.

left isNilNode ifFalse: [ left := left copy ].
right isNilNode ifFalse: [ right := right copy ]
]

{ #category : 'enumerating' }
CTAVLNode >> postOrderDo: aBlock [

Expand Down
15 changes: 6 additions & 9 deletions src/Containers-AVL-Tree/CTAVLTree.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,6 @@ CTAVLTree >> collect: aBlock [
^ result
]

{ #category : 'copying' }
CTAVLTree >> copy [

| newTree |
newTree := self class new.
self inOrderDo: [ :each | newTree add: each ].
^ newTree
]

{ #category : 'enumerating' }
CTAVLTree >> detect: aBlock ifNone: absentBlock [

Expand Down Expand Up @@ -236,6 +227,12 @@ CTAVLTree >> last [
^ self findMax
]

{ #category : 'copying' }
CTAVLTree >> postCopy [
super postCopy.
root := root copy.
]

{ #category : 'enumerating' }
CTAVLTree >> postOrderDo: aBlock [

Expand Down
Loading