forked from pharo-containers/Container-AVL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTAVLTree.class.st
More file actions
297 lines (224 loc) · 5.52 KB
/
CTAVLTree.class.st
File metadata and controls
297 lines (224 loc) · 5.52 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
"
I represent an AVL Tree, a self-balancing binary search tree.
I maintain the BST property (left < node < right) while also ensuring that the height difference between the left and right subtrees of any node is at most 1. This balancing act guarantees O(log n) worst-case performance for all primary operations.
My public API is designed to be polymorphic with CTBinarySearchTree and includes a rich set of collection methods for enumeration, searching, and accessing.
Usage:
tree := CTAVLTree new.
tree addAll: #(50 30 70 20 40 60 80).
tree asArray. => #(20 30 40 50 60 70 80)
tree height. => 3
tree first. => 20
tree includes: 30. => true
tree elementsFrom: 30 to: 60. => #(30 40 50 60)
"
Class {
#name : 'CTAVLTree',
#superclass : 'Object',
#instVars : [
'root'
],
#category : 'Containers-AVL-Tree',
#package : 'Containers-AVL-Tree'
}
{ #category : 'adding' }
CTAVLTree >> add: anObject [
root := root addChild: anObject.
root parent: nil.
^ anObject
]
{ #category : 'adding' }
CTAVLTree >> addAll: aCollection [
aCollection do: [ :each | self add: each ].
^ aCollection
]
{ #category : 'enumerating' }
CTAVLTree >> anySatisfy: aBlock [
self inOrderDo: [ :each |
(aBlock value: each) ifTrue: [ ^ true ]
].
^ false
]
{ #category : 'converting' }
CTAVLTree >> asArray [
| result |
result := OrderedCollection new: self size.
self inOrderDo: [ :each | result add: each ].
^ result asArray
]
{ #category : 'accessing' }
CTAVLTree >> at: anObject ifAbsent: aBlock [
| result |
result := root search: anObject.
^ result ifNil: [ aBlock value ] ifNotNil: [ result ]
]
{ #category : 'removing' }
CTAVLTree >> clear [
root := CTAVLNilNode new
]
{ #category : 'enumerating' }
CTAVLTree >> collect: aBlock [
| result |
result := OrderedCollection new: self size.
self inOrderDo: [ :each | result add: (aBlock value: each) ].
^ result
]
{ #category : 'copying' }
CTAVLTree >> copy [
| newTree |
newTree := self class new.
self inOrderDo: [ :each | newTree add: each ].
^ newTree
]
{ #category : 'enumerating' }
CTAVLTree >> detect: aBlock ifNone: absentBlock [
self inOrderDo: [ :each |
(aBlock value: each) ifTrue: [ ^ each ]
].
^ absentBlock value
]
{ #category : 'enumerating' }
CTAVLTree >> do: aBlock [
"Alias for inOrderDo: - visits elements in sorted order"
self inOrderDo: aBlock
]
{ #category : 'enumerating' }
CTAVLTree >> elementsFrom: min to: max [
| result |
min > max ifTrue: [ ^ #() ].
result := OrderedCollection new.
root elementsFrom: min to: max into: result.
^ result
]
{ #category : 'enumerating' }
CTAVLTree >> elementsGreaterThan: anObject [
| result |
result := OrderedCollection new.
root elementsGreaterThan: anObject into: result.
^ result
]
{ #category : 'enumerating' }
CTAVLTree >> elementsLessThan: anObject [
| result |
result := OrderedCollection new.
root elementsLessThan: anObject into: result.
^ result
]
{ #category : 'searching' }
CTAVLTree >> findMax [
^ self isEmpty
ifTrue: [ nil ]
ifFalse: [ root findMax ]
]
{ #category : 'searching' }
CTAVLTree >> findMaxNode [
^ self isEmpty
ifTrue: [ nil ]
ifFalse: [ root findMaxNode ]
]
{ #category : 'searching' }
CTAVLTree >> findMin [
^ self isEmpty
ifTrue: [ nil ]
ifFalse: [ root findMin ]
]
{ #category : 'searching' }
CTAVLTree >> findMinNode [
^ self isEmpty
ifTrue: [ nil ]
ifFalse: [ root findMinNode ]
]
{ #category : 'accessing' }
CTAVLTree >> first [
^ self findMin
]
{ #category : 'accessing' }
CTAVLTree >> height [
^ root height
]
{ #category : 'testing' }
CTAVLTree >> ifEmpty: aBlock [
^ self isEmpty
ifTrue: [ aBlock value ]
ifFalse: [ self ]
]
{ #category : 'testing' }
CTAVLTree >> ifNotEmpty: aBlock [
^ self isEmpty
ifFalse: [ aBlock value: self ]
ifTrue: [ self ]
]
{ #category : 'enumerating' }
CTAVLTree >> inOrderDo: aBlock [
root inOrderDo: aBlock
]
{ #category : 'testing' }
CTAVLTree >> includes: anObject [
^ (root search: anObject) notNil
]
{ #category : 'initialization' }
CTAVLTree >> initialize [
super initialize.
root := CTAVLNilNode new
]
{ #category : 'testing' }
CTAVLTree >> isEmpty [
^ root isEmpty
]
{ #category : 'accessing' }
CTAVLTree >> last [
^ self findMax
]
{ #category : 'enumerating' }
CTAVLTree >> postOrderDo: aBlock [
root postOrderDo: aBlock
]
{ #category : 'enumerating' }
CTAVLTree >> preOrderDo: aBlock [
root preOrderDo: aBlock
]
{ #category : 'searching' }
CTAVLTree >> predecessorOf: anObject [
^ root predecessorOf: anObject
]
{ #category : 'removing' }
CTAVLTree >> remove: anObject [
^ self
remove: anObject
ifAbsent: [ NotFound signalFor: anObject in: self ]
]
{ #category : 'removing' }
CTAVLTree >> remove: anObject ifAbsent: aBlock [
(self includes: anObject) ifFalse: [ ^ aBlock value ].
root := root removeValue: anObject.
root parent: nil.
^ anObject
]
{ #category : 'removing' }
CTAVLTree >> removeAll: aCollection [
aCollection do: [ :each | self remove: each ifAbsent: [ ] ].
^ aCollection
]
{ #category : 'accessing' }
CTAVLTree >> root [
^ root isEmpty ifTrue: [ nil ] ifFalse: [ root ]
]
{ #category : 'enumerating' }
CTAVLTree >> select: aBlock [
| result |
result := OrderedCollection new.
self inOrderDo: [ :each | (aBlock value: each) ifTrue: [ result add: each ] ].
^ result
]
{ #category : 'accessing' }
CTAVLTree >> size [
^ root size
]
{ #category : 'searching' }
CTAVLTree >> successorOf: anObject [
^ root successorOf: anObject
]
{ #category : 'accessing' }
CTAVLTree >> validate [
"Validate that the tree maintains both BST and AVL properties"
^ root validateAsRoot
]