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
16 changes: 16 additions & 0 deletions src/Containers-AVL-Tree-Tests/CTAVLTreeTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,19 @@ CTAVLTreeTest >> testSizeOneElement [
tree add: 42.
self assert: tree size equals: 1
]

{ #category : 'tests' }
CTAVLTreeTest >> testChildParentRelationship [
tree add: 50.
tree add: 30.
tree add: 70.

self assert: (tree root left parent) equals: tree root.
self assert: (tree root right parent) equals: tree root
]

{ #category : 'tests' }
CTAVLTreeTest >> testRootHasNoParent [
tree add: 14.
self assert: tree root parent isNil
]
18 changes: 18 additions & 0 deletions src/Containers-AVL-Tree/CTAVLAbstractNode.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Date: October 20, 2023
Class {
#name : 'CTAVLAbstractNode',
#superclass : 'Object',
#instVars : [
'parent'
],
#category : 'Containers-AVL-Tree',
#package : 'Containers-AVL-Tree'
}
Expand All @@ -34,6 +37,11 @@ CTAVLAbstractNode >> children [
^ #()
]

{ #category : 'accessing' }
CTAVLAbstractNode >> allChildren [
^ #()
]

{ #category : 'enumerating' }
CTAVLAbstractNode >> childrenDo: aBlock [
^ self subclassResponsibility
Expand Down Expand Up @@ -72,4 +80,14 @@ CTAVLAbstractNode >> remove: anObject path: list [
{ #category : 'accessing' }
CTAVLAbstractNode >> withAllChildren: aCollection [
"Default is to do nothing"
]

{ #category : 'accessing' }
CTAVLAbstractNode >> parent [
^ parent
]

{ #category : 'accessing' }
CTAVLAbstractNode >> parent: aNode [
parent := aNode
]
7 changes: 6 additions & 1 deletion src/Containers-AVL-Tree/CTAVLNilNode.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Class {

{ #category : 'adding' }
CTAVLNilNode >> addChild: newObject [
^ CTAVLNode with: newObject
^ self parent replace: self with: (CTAVLNode with: newObject)
]

{ #category : 'private' }
Expand All @@ -28,6 +28,11 @@ CTAVLNilNode >> children [
^ #()
]

{ #category : 'accessing' }
CTAVLNilNode >> allChildren [
^ #()
]

{ #category : 'enumerating' }
CTAVLNilNode >> childrenDo: aBlock [
"Do nothing for nil node"
Expand Down
19 changes: 16 additions & 3 deletions src/Containers-AVL-Tree/CTAVLNode.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ CTAVLNode >> children [
^ { left. right }
]

{ #category : 'accessing' }
CTAVLNode >> allChildren [
"Collects all child nodes recursively."
| children |
children := OrderedCollection new.
self left ifNotNil: [children addAll: self left allChildren].
self right ifNotNil: [children addAll: self right allChildren].
children add: self.
^ children.
]

{ #category : 'enumerating' }
CTAVLNode >> childrenDo: aBlock [
left childrenDo: aBlock.
Expand Down Expand Up @@ -180,7 +191,8 @@ CTAVLNode >> left [

{ #category : 'accessing' }
CTAVLNode >> left: aNode [
left := aNode
left := aNode.
aNode parent: self
]

{ #category : 'private' }
Expand Down Expand Up @@ -261,8 +273,9 @@ CTAVLNode >> right [
]

{ #category : 'accessing' }
CTAVLNode >> right: anObject [
right := anObject
CTAVLNode >> right: aNode [
right := aNode.
aNode parent: self
]

{ #category : 'private' }
Expand Down
11 changes: 11 additions & 0 deletions src/Containers-AVL-Tree/CTAVLTree.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,14 @@ CTAVLTree >> search: anInteger [
CTAVLTree >> size [
^ root nodeSize
]

{ #category : 'accessing' }
CTAVLTree >> root [
^ root
]

{ #category : 'accessing' }
CTAVLTree >> allChildren [
"Returns a collection of all child nodes in the tree."
^ root ifNil: [#()] ifNotNil: [root allChildren].
]