Skip to content

Commit 8a565b4

Browse files
committed
fix: fix addChild: addChild: is not in sync with its implementation
1 parent e1d0dae commit 8a565b4

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-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
@@ -340,3 +340,19 @@ CTAVLTreeTest >> testSizeOneElement [
340340
tree add: 42.
341341
self assert: tree size equals: 1
342342
]
343+
344+
{ #category : 'tests' }
345+
CTAVLTreeTest >> testChildParentRelationship [
346+
tree add: 50.
347+
tree add: 30.
348+
tree add: 70.
349+
350+
self assert: (tree root left parent) equals: tree root.
351+
self assert: (tree root right parent) equals: tree root
352+
]
353+
354+
{ #category : 'tests' }
355+
CTAVLTreeTest >> testRootHasNoParent [
356+
tree add: 14.
357+
self assert: tree root parent isNil
358+
]

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

Lines changed: 13 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
}
@@ -72,4 +75,14 @@ CTAVLAbstractNode >> remove: anObject path: list [
7275
{ #category : 'accessing' }
7376
CTAVLAbstractNode >> withAllChildren: aCollection [
7477
"Default is to do nothing"
78+
]
79+
80+
{ #category : 'accessing' }
81+
CTAVLAbstractNode >> parent [
82+
^ parent
83+
]
84+
85+
{ #category : 'accessing' }
86+
CTAVLAbstractNode >> parent: aNode [
87+
parent := aNode
7588
]

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

Lines changed: 1 addition & 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' }

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ CTAVLNode >> left [
180180

181181
{ #category : 'accessing' }
182182
CTAVLNode >> left: aNode [
183-
left := aNode
183+
left := aNode.
184+
aNode parent: self
184185
]
185186

186187
{ #category : 'private' }
@@ -261,8 +262,9 @@ CTAVLNode >> right [
261262
]
262263

263264
{ #category : 'accessing' }
264-
CTAVLNode >> right: anObject [
265-
right := anObject
265+
CTAVLNode >> right: aNode [
266+
right := aNode.
267+
aNode parent: self
266268
]
267269

268270
{ #category : 'private' }

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,8 @@ CTAVLTree >> search: anInteger [
9999
CTAVLTree >> size [
100100
^ root nodeSize
101101
]
102+
103+
{ #category : 'accessing' }
104+
CTAVLTree >> root [
105+
^ root
106+
]

0 commit comments

Comments
 (0)