forked from pharo-containers/Container-AVL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTAVLNode.class.st
More file actions
350 lines (274 loc) · 7.11 KB
/
CTAVLNode.class.st
File metadata and controls
350 lines (274 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"
I represent a node in an AVL Tree.
I extend the functionality of a standard BST node by storing my height and performing self-balancing rotations after any modification (add or remove) to ensure the tree's height balance is maintained.
The AVL invariant I maintain is: |height(left) - height(right)| <= 1
"
Class {
#name : 'CTAVLNode',
#superclass : 'CTAVLAbstractNode',
#instVars : [
'left',
'contents',
'right',
'height'
],
#category : 'Containers-AVL-Tree',
#package : 'Containers-AVL-Tree'
}
{ #category : 'instance creation' }
CTAVLNode class >> with: anInteger [
^ self new
contents: anInteger;
left: CTAVLNilNode new;
right: CTAVLNilNode new;
yourself
]
{ #category : 'adding' }
CTAVLNode >> addChild: anObject [
anObject < contents
ifTrue: [ self left: (left addChild: anObject) ]
ifFalse: [
anObject > contents
ifTrue: [ self right: (right addChild: anObject) ] ].
^ self updateHeightAndRebalance
]
{ #category : 'accessing' }
CTAVLNode >> balanceFactor [
^ left height - right height
]
{ #category : 'accessing' }
CTAVLNode >> children [
^ { left . right }
]
{ #category : 'accessing' }
CTAVLNode >> contents [
^ contents
]
{ #category : 'accessing' }
CTAVLNode >> contents: anObject [
contents := anObject
]
{ #category : 'enumerating' }
CTAVLNode >> elementsFrom: min to: max into: aCollection [
contents > min ifTrue: [
left elementsFrom: min to: max into: aCollection ].
(contents between: min and: max) ifTrue: [ aCollection add: contents ].
contents < max ifTrue: [
right elementsFrom: min to: max into: aCollection ]
]
{ #category : 'enumerating' }
CTAVLNode >> elementsGreaterThan: anObject into: aCollection [
contents > anObject ifTrue: [
left elementsGreaterThan: anObject into: aCollection.
aCollection add: contents ].
right elementsGreaterThan: anObject into: aCollection
]
{ #category : 'enumerating' }
CTAVLNode >> elementsLessThan: anObject into: aCollection [
left elementsLessThan: anObject into: aCollection.
contents < anObject ifFalse: [ ^ self ].
aCollection add: contents.
right elementsLessThan: anObject into: aCollection
]
{ #category : 'searching' }
CTAVLNode >> findMax [
^ right isEmpty
ifTrue: [ contents ]
ifFalse: [ right findMax ]
]
{ #category : 'searching' }
CTAVLNode >> findMaxNode [
^ right isEmpty
ifTrue: [ self ]
ifFalse: [ right findMaxNode ]
]
{ #category : 'searching' }
CTAVLNode >> findMin [
^ left isEmpty
ifTrue: [ contents ]
ifFalse: [ left findMin ]
]
{ #category : 'searching' }
CTAVLNode >> findMinNode [
^ left isEmpty
ifTrue: [ self ]
ifFalse: [ left findMinNode ]
]
{ #category : 'accessing' }
CTAVLNode >> height [
^ height
]
{ #category : 'enumerating' }
CTAVLNode >> inOrderDo: aBlock [
left inOrderDo: aBlock.
aBlock value: contents.
right inOrderDo: aBlock
]
{ #category : 'initialization' }
CTAVLNode >> initialize [
super initialize.
left := CTAVLNilNode new parent: self.
right := CTAVLNilNode new parent: self.
height := 1
]
{ #category : 'testing' }
CTAVLNode >> isBalanced [
^ (left height - right height) abs <= 1
]
{ #category : 'testing' }
CTAVLNode >> isEmpty [
^ false
]
{ #category : 'testing' }
CTAVLNode >> isLeaf [
^ left isEmpty and: [ right isEmpty ]
]
{ #category : 'accessing' }
CTAVLNode >> left [
^ left
]
{ #category : 'accessing' }
CTAVLNode >> left: aNode [
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 [
left postOrderDo: aBlock.
right postOrderDo: aBlock.
aBlock value: contents
]
{ #category : 'enumerating' }
CTAVLNode >> preOrderDo: aBlock [
aBlock value: contents.
left preOrderDo: aBlock.
right preOrderDo: aBlock
]
{ #category : 'searching' }
CTAVLNode >> predecessorOf: anObject [
| rightResult |
anObject <= contents ifTrue: [
^ left predecessorOf: anObject ].
rightResult := right predecessorOf: anObject.
^ rightResult ifNil: [ contents ] ifNotNil: [ rightResult ]
]
{ #category : 'private' }
CTAVLNode >> rebalance [
| bf |
bf := self balanceFactor.
"Left-heavy case"
bf > 1 ifTrue: [
"Left-Right case"
(left balanceFactor < 0) ifTrue: [ self left: left rotateLeft ].
^ self rotateRight ].
"Right-heavy case"
bf < -1 ifTrue: [
"Right-Left case"
(right balanceFactor > 0) ifTrue: [ self right: right rotateRight ].
^ self rotateLeft ].
^ self "Already balanced"
]
{ #category : 'private' }
CTAVLNode >> removeThisNode [
"Handle removal of the current node, then rebalance."
| successorValue |
"Case 1 & 2: Leaf or one child"
left isEmpty ifTrue: [ ^ right ].
right isEmpty ifTrue: [ ^ left ].
"Case 3: Two children"
successorValue := right findMin.
self contents: successorValue.
self right: (right removeValue: successorValue).
^ self updateHeightAndRebalance
]
{ #category : 'removing' }
CTAVLNode >> removeValue: anObject [
anObject < contents ifTrue: [
self left: (left removeValue: anObject).
^ self updateHeightAndRebalance ].
anObject > contents ifTrue: [
self right: (right removeValue: anObject).
^ self updateHeightAndRebalance ].
"Found the node to remove"
^ self removeThisNode
]
{ #category : 'accessing' }
CTAVLNode >> right [
^ right
]
{ #category : 'accessing' }
CTAVLNode >> right: aNode [
right := aNode.
aNode ifNotNil: [ aNode parent: self ]
]
{ #category : 'private' }
CTAVLNode >> rotateLeft [
| newRoot |
newRoot := right.
self right: newRoot left.
newRoot left: self.
self updateHeight.
newRoot updateHeight.
^ newRoot
]
{ #category : 'private' }
CTAVLNode >> rotateRight [
| newRoot |
newRoot := left.
self left: newRoot right.
newRoot right: self.
self updateHeight.
newRoot updateHeight.
^ newRoot
]
{ #category : 'accessing' }
CTAVLNode >> search: anObject [
contents = anObject ifTrue: [ ^ contents ].
^ anObject < contents
ifTrue: [ left search: anObject ]
ifFalse: [ right search: anObject ]
]
{ #category : 'accessing' }
CTAVLNode >> size [
^ 1 + left size + right size
]
{ #category : 'searching' }
CTAVLNode >> successorOf: anObject [
| leftResult |
anObject >= contents ifTrue: [
^ right successorOf: anObject ].
leftResult := left successorOf: anObject.
^ leftResult ifNil: [ contents ] ifNotNil: [ leftResult ]
]
{ #category : 'private' }
CTAVLNode >> updateHeight [
height := 1 + (left height max: right height)
]
{ #category : 'private' }
CTAVLNode >> updateHeightAndRebalance [
self updateHeight.
^ self rebalance
]
{ #category : 'validation' }
CTAVLNode >> validateAsRoot [
^ self validateWithMin: nil max: nil
]
{ #category : 'validation' }
CTAVLNode >> validateWithMin: min max: max [
"Check BST property"
(min notNil and: [ contents < min ]) ifTrue: [ ^ false ].
(max notNil and: [ contents > max ]) ifTrue: [ ^ false ].
"Check AVL property"
(self balanceFactor abs > 1) ifTrue: [ ^ false ].
"Check height property"
(self height = (1 + (left height max: right height))) ifFalse: [ ^ false ].
"Recursively validate children"
^ (left validateWithMin: min max: contents) and: [
right validateWithMin: contents max: max ]
]