Skip to content

Commit 339dd32

Browse files
authored
Merge pull request #30 from AhmedHamdiy/main
Fixes #29
2 parents 560111b + cce34b7 commit 339dd32

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,19 @@ CTAVLTreeTest >> testSizeOneElement [
334334
tree add: 42.
335335
self assert: tree size equals: 1
336336
]
337+
338+
{ #category : 'tests' }
339+
CTAVLTreeTest >> testChildParentRelationship [
340+
tree add: 50.
341+
tree add: 30.
342+
tree add: 70.
343+
344+
self assert: (tree root left parent) equals: tree root.
345+
self assert: (tree root right parent) equals: tree root
346+
]
347+
348+
{ #category : 'tests' }
349+
CTAVLTreeTest >> testRootHasNoParent [
350+
tree add: 14.
351+
self assert: tree root parent isNil
352+
]

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Date: October 20, 2023
1515
Class {
1616
#name : 'CTAVLAbstractNode',
1717
#superclass : 'Object',
18+
#instVars : [
19+
'parent'
20+
],
1821
#category : 'Containers-AVL-Tree',
1922
#package : 'Containers-AVL-Tree'
2023
}
@@ -34,6 +37,11 @@ CTAVLAbstractNode >> children [
3437
^ #()
3538
]
3639

40+
{ #category : 'accessing' }
41+
CTAVLAbstractNode >> allChildren [
42+
^ #()
43+
]
44+
3745
{ #category : 'enumerating' }
3846
CTAVLAbstractNode >> childrenDo: aBlock [
3947
^ self subclassResponsibility
@@ -72,4 +80,14 @@ CTAVLAbstractNode >> remove: anObject path: list [
7280
{ #category : 'accessing' }
7381
CTAVLAbstractNode >> withAllChildren: aCollection [
7482
"Default is to do nothing"
83+
]
84+
85+
{ #category : 'accessing' }
86+
CTAVLAbstractNode >> parent [
87+
^ parent
88+
]
89+
90+
{ #category : 'accessing' }
91+
CTAVLAbstractNode >> parent: aNode [
92+
parent := aNode
7593
]

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Class {
1515

1616
{ #category : 'adding' }
1717
CTAVLNilNode >> addChild: newObject [
18-
^ CTAVLNode with: newObject
18+
^ self parent replace: self with: (CTAVLNode with: newObject)
1919
]
2020

2121
{ #category : 'private' }
@@ -28,6 +28,11 @@ CTAVLNilNode >> children [
2828
^ #()
2929
]
3030

31+
{ #category : 'accessing' }
32+
CTAVLNilNode >> allChildren [
33+
^ #()
34+
]
35+
3136
{ #category : 'enumerating' }
3237
CTAVLNilNode >> childrenDo: aBlock [
3338
"Do nothing for nil node"

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ CTAVLNode >> children [
119119
^ { left. right }
120120
]
121121

122+
{ #category : 'accessing' }
123+
CTAVLNode >> allChildren [
124+
"Collects all child nodes recursively."
125+
| children |
126+
children := OrderedCollection new.
127+
self left ifNotNil: [children addAll: self left allChildren].
128+
self right ifNotNil: [children addAll: self right allChildren].
129+
children add: self.
130+
^ children.
131+
]
132+
122133
{ #category : 'enumerating' }
123134
CTAVLNode >> childrenDo: aBlock [
124135
left childrenDo: aBlock.
@@ -180,7 +191,8 @@ CTAVLNode >> left [
180191

181192
{ #category : 'accessing' }
182193
CTAVLNode >> left: aNode [
183-
left := aNode
194+
left := aNode.
195+
aNode parent: self
184196
]
185197

186198
{ #category : 'private' }
@@ -261,8 +273,9 @@ CTAVLNode >> right [
261273
]
262274

263275
{ #category : 'accessing' }
264-
CTAVLNode >> right: anObject [
265-
right := anObject
276+
CTAVLNode >> right: aNode [
277+
right := aNode.
278+
aNode parent: self
266279
]
267280

268281
{ #category : 'private' }

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,14 @@ CTAVLTree >> search: anInteger [
9898
CTAVLTree >> size [
9999
^ root nodeSize
100100
]
101+
102+
{ #category : 'accessing' }
103+
CTAVLTree >> root [
104+
^ root
105+
]
106+
107+
{ #category : 'accessing' }
108+
CTAVLTree >> allChildren [
109+
"Returns a collection of all child nodes in the tree."
110+
^ root ifNil: [#()] ifNotNil: [root allChildren].
111+
]

0 commit comments

Comments
 (0)