5144
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
1931
|
3 |
"
|
|
4 |
Copyright (c) 2005 Ian Piumarta
|
|
5 |
All rights reserved.
|
|
6 |
|
|
7 |
Permission is hereby granted, free of charge, to any person obtaining a
|
|
8 |
copy of this software and associated documentation files (the 'Software'),
|
|
9 |
to deal in the Software without restriction, including without limitation
|
|
10 |
the rights to use, copy, modify, merge, publish, distribute, and/or sell
|
|
11 |
copies of the Software, and to permit persons to whom the Software is
|
|
12 |
furnished to do so, provided that the above copyright notice(s) and this
|
|
13 |
permission notice appear in all copies of the Software and that both the
|
|
14 |
above copyright notice(s) and this permission notice appear in supporting
|
|
15 |
documentation.
|
|
16 |
|
|
17 |
THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
|
|
18 |
|
|
19 |
Last edited: 2007-01-25 03:17:27 by piumarta on emilia.local
|
|
20 |
"
|
|
21 |
"{ Package: 'stx:libbasic2' }"
|
|
22 |
|
4115
|
23 |
"{ NameSpace: Smalltalk }"
|
|
24 |
|
1931
|
25 |
SequenceableCollection subclass:#AVLTree
|
|
26 |
instanceVariableNames:'rootNode orderBlock'
|
|
27 |
classVariableNames:''
|
|
28 |
poolDictionaries:''
|
2765
|
29 |
category:'Collections-Ordered-Trees'
|
1931
|
30 |
!
|
|
31 |
|
|
32 |
Object subclass:#AVLNil
|
|
33 |
instanceVariableNames:''
|
|
34 |
classVariableNames:''
|
|
35 |
poolDictionaries:''
|
|
36 |
privateIn:AVLTree
|
|
37 |
!
|
|
38 |
|
|
39 |
Object subclass:#AVLTreeNode
|
|
40 |
instanceVariableNames:'left right height value'
|
|
41 |
classVariableNames:''
|
|
42 |
poolDictionaries:''
|
|
43 |
privateIn:AVLTree
|
|
44 |
!
|
|
45 |
|
|
46 |
!AVLTree class methodsFor:'documentation'!
|
|
47 |
|
|
48 |
copyright
|
|
49 |
"
|
|
50 |
Copyright (c) 2005 Ian Piumarta
|
|
51 |
All rights reserved.
|
|
52 |
|
|
53 |
Permission is hereby granted, free of charge, to any person obtaining a
|
|
54 |
copy of this software and associated documentation files (the 'Software'),
|
|
55 |
to deal in the Software without restriction, including without limitation
|
|
56 |
the rights to use, copy, modify, merge, publish, distribute, and/or sell
|
|
57 |
copies of the Software, and to permit persons to whom the Software is
|
|
58 |
furnished to do so, provided that the above copyright notice(s) and this
|
|
59 |
permission notice appear in all copies of the Software and that both the
|
|
60 |
above copyright notice(s) and this permission notice appear in supporting
|
|
61 |
documentation.
|
|
62 |
|
|
63 |
THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
|
|
64 |
|
|
65 |
Last edited: 2007-01-25 03:17:27 by piumarta on emilia.local
|
|
66 |
"
|
|
67 |
!
|
|
68 |
|
|
69 |
documentation
|
|
70 |
"
|
4115
|
71 |
AVLTree -- balanced trees.
|
1931
|
72 |
|
4115
|
73 |
This implements another kind of self-balancing tree, named after their inventors,
|
|
74 |
AVLTree is obsoleted by AATree, which has the same best/worst/average characteristics
|
|
75 |
(it is also self-balancing), but is always faster (roughly by a factor of 1.5 to 2).
|
|
76 |
|
|
77 |
Consider using an AATree instead.
|
|
78 |
(unless a special situation arises, of which we don't know yet)
|
|
79 |
|
|
80 |
[see also:]
|
4140
|
81 |
AATree
|
|
82 |
BTree
|
|
83 |
SortedCollection
|
4115
|
84 |
https://en.wikipedia.org/wiki/AVL_tree
|
|
85 |
|
|
86 |
Examples:
|
|
87 |
|
1931
|
88 |
|t|
|
|
89 |
|
|
90 |
t := AVLTree new.
|
|
91 |
self assert:(t depth == 0).
|
|
92 |
self assert:(t size == 0).
|
|
93 |
self assert:(t isEmpty).
|
|
94 |
|
|
95 |
t add:'hello'.
|
|
96 |
self assert:(t depth == 0).
|
|
97 |
self assert:(t size == 1).
|
|
98 |
self assert:(t notEmpty).
|
|
99 |
|
|
100 |
t add:'world'.
|
|
101 |
self assert:(t depth == 1).
|
|
102 |
self assert:(t size == 2).
|
|
103 |
|
|
104 |
t add:'aaa'.
|
|
105 |
self assert:(t depth == 1).
|
|
106 |
self assert:(t size == 3).
|
|
107 |
|
|
108 |
t add:'bbb'.
|
|
109 |
self assert:(t depth == 2).
|
|
110 |
self assert:(t size == 4).
|
|
111 |
|
|
112 |
self assert:(t printString = 'AVLTree(aaa bbb hello world)').
|
|
113 |
|
|
114 |
t remove:'aaa'.
|
|
115 |
self assert:(t printString = 'AVLTree(bbb hello world)').
|
|
116 |
self assert:(t depth == 1).
|
|
117 |
self assert:(t size == 3).
|
|
118 |
|
|
119 |
| words tree |
|
|
120 |
words := #( Peter Piper picked a peck of pickled peppers
|
|
121 |
A peck of pickled peppers Peter Piper picked
|
|
122 |
If Peter Piper picked a peck of pickled peppers
|
|
123 |
Where is the peck of pickled peppers Peter Piper picked? ).
|
|
124 |
tree := AVLTree new.
|
|
125 |
tree addAll: words.
|
|
126 |
tree printOn:Transcript. Transcript cr; cr.
|
|
127 |
tree := AVLTree withSortBlock: [:a :b | b < a].
|
|
128 |
tree addAll: words.
|
|
129 |
tree printOn:Transcript. Transcript cr; cr.
|
|
130 |
"
|
4117
|
131 |
!
|
|
132 |
|
|
133 |
performance
|
|
134 |
"
|
|
135 |
Time to insert random 1000000 into SortedCollection: 840ms
|
|
136 |
Time to insert random 1000000 into BinaryTree: 2040ms
|
|
137 |
Time to insert random 1000000 into AATree: 3060ms
|
|
138 |
Time to insert random 1000000 into AVLTree: 3780ms
|
|
139 |
|
|
140 |
Time to insert ordered 1000000 into SortedCollection: 30ms
|
|
141 |
Time to insert ordered 1000000 into BinaryTree: 72200ms
|
|
142 |
Time to insert ordered 1000000 into AATree: 110ms
|
|
143 |
Time to insert ordered 1000000 into AVLTree: 180ms
|
|
144 |
|
|
145 |
Time to insert reverse ordered 1000000 into SortedCollection: 30ms
|
|
146 |
Time to insert reverse ordered 1000000 into BinaryTree: 73880ms
|
|
147 |
Time to insert reverse ordered 1000000 into AATree: 80ms
|
|
148 |
Time to insert reverse ordered 1000000 into AVLTree: 160ms
|
|
149 |
"
|
1931
|
150 |
! !
|
|
151 |
|
|
152 |
!AVLTree class methodsFor:'instance creation'!
|
|
153 |
|
|
154 |
new
|
|
155 |
^ super new initialize
|
|
156 |
!
|
|
157 |
|
|
158 |
withSortBlock: binaryBlock
|
|
159 |
^ self new orderBlock:binaryBlock.
|
|
160 |
! !
|
|
161 |
|
|
162 |
!AVLTree methodsFor:'accessing'!
|
|
163 |
|
|
164 |
orderBlock:aBlock
|
|
165 |
orderBlock := aBlock
|
|
166 |
! !
|
|
167 |
|
|
168 |
!AVLTree methodsFor:'adding & removing'!
|
|
169 |
|
|
170 |
add: anObject
|
|
171 |
self addNode: (AVLTreeNode withValue: anObject).
|
|
172 |
^anObject
|
|
173 |
!
|
|
174 |
|
|
175 |
remove: anObject
|
|
176 |
^self removeNode: (AVLTreeNode withValue: anObject)
|
|
177 |
! !
|
|
178 |
|
|
179 |
!AVLTree methodsFor:'enumeration'!
|
|
180 |
|
|
181 |
do: unaryBlock
|
5144
|
182 |
rootNode avlTreeNodeDo: unaryBlock
|
1931
|
183 |
!
|
|
184 |
|
|
185 |
reverseDo: unaryBlock
|
5147
|
186 |
rootNode avlTreeNodeReverseDo: unaryBlock
|
1931
|
187 |
! !
|
|
188 |
|
|
189 |
!AVLTree methodsFor:'initialization'!
|
|
190 |
|
|
191 |
initialize
|
|
192 |
super initialize.
|
|
193 |
rootNode := AVLNil.
|
|
194 |
orderBlock := [:a :b | a < b].
|
|
195 |
! !
|
|
196 |
|
|
197 |
!AVLTree methodsFor:'private'!
|
|
198 |
|
|
199 |
addNode: aNode
|
|
200 |
rootNode := rootNode avlTreeNodeInsert: aNode orderedBy: orderBlock.
|
|
201 |
^aNode
|
|
202 |
!
|
|
203 |
|
|
204 |
removeNode: aNode
|
|
205 |
^rootNode := rootNode avlTreeNodeRemove: aNode orderedBy: orderBlock
|
|
206 |
! !
|
|
207 |
|
|
208 |
!AVLTree methodsFor:'queries'!
|
|
209 |
|
|
210 |
depth
|
|
211 |
^rootNode avlTreeNodeHeight
|
|
212 |
!
|
|
213 |
|
|
214 |
isEmpty
|
|
215 |
^rootNode == AVLNil
|
|
216 |
!
|
|
217 |
|
|
218 |
size
|
|
219 |
^rootNode avlTreeSize
|
|
220 |
! !
|
|
221 |
|
|
222 |
!AVLTree methodsFor:'searching'!
|
|
223 |
|
|
224 |
find: anObject
|
|
225 |
^self findNode: (AVLTreeNode with: anObject)
|
|
226 |
!
|
|
227 |
|
|
228 |
findNode: aNode
|
|
229 |
^rootNode avlTreeNodeFind: aNode orderedBy: orderBlock
|
|
230 |
! !
|
|
231 |
|
|
232 |
!AVLTree::AVLNil class methodsFor:'avl polymorphy'!
|
|
233 |
|
|
234 |
avlTreeNodeDo: unaryBlock
|
|
235 |
^ nil
|
|
236 |
!
|
|
237 |
|
|
238 |
avlTreeNodeFind: aNode
|
|
239 |
^AVLNil
|
|
240 |
!
|
|
241 |
|
|
242 |
avlTreeNodeHeight
|
|
243 |
^0
|
|
244 |
!
|
|
245 |
|
|
246 |
avlTreeNodeInsert: aNode orderedBy: binaryBlock
|
|
247 |
^aNode
|
|
248 |
!
|
|
249 |
|
|
250 |
avlTreeNodeMoveRight: aNode
|
|
251 |
^aNode
|
|
252 |
!
|
|
253 |
|
|
254 |
avlTreeNodeRemove: aNode orderedBy: binaryBlock
|
|
255 |
^AVLNil
|
|
256 |
!
|
|
257 |
|
|
258 |
avlTreeNodeReverseDo: unaryBlock
|
|
259 |
^nil
|
|
260 |
!
|
|
261 |
|
|
262 |
avlTreeSize
|
|
263 |
^0
|
|
264 |
! !
|
|
265 |
|
|
266 |
!AVLTree::AVLTreeNode class methodsFor:'instance creation'!
|
|
267 |
|
|
268 |
new
|
|
269 |
^ self basicNew initialize.
|
|
270 |
!
|
|
271 |
|
|
272 |
withValue: anObject
|
|
273 |
^ self new value:anObject.
|
|
274 |
! !
|
|
275 |
|
|
276 |
!AVLTree::AVLTreeNode methodsFor:'accessing'!
|
|
277 |
|
|
278 |
height
|
|
279 |
^ height
|
|
280 |
!
|
|
281 |
|
|
282 |
left
|
|
283 |
^ left
|
|
284 |
!
|
|
285 |
|
4267
|
286 |
left:aTreeNode
|
|
287 |
left := aTreeNode.
|
1931
|
288 |
!
|
|
289 |
|
|
290 |
right
|
|
291 |
^ right
|
|
292 |
!
|
|
293 |
|
4267
|
294 |
right:aTreeNode
|
|
295 |
right := aTreeNode.
|
1931
|
296 |
!
|
|
297 |
|
|
298 |
value
|
|
299 |
^ value
|
|
300 |
!
|
|
301 |
|
|
302 |
value:anObject
|
|
303 |
value := anObject
|
|
304 |
! !
|
|
305 |
|
|
306 |
!AVLTree::AVLTreeNode methodsFor:'adding & removing'!
|
|
307 |
|
|
308 |
avlTreeNodeRemove: aNode orderedBy: binaryBlock
|
|
309 |
(aNode equals: self orderedBy: binaryBlock)
|
|
310 |
ifTrue:
|
|
311 |
[| temp |
|
|
312 |
temp := left avlTreeNodeMoveRight: right.
|
|
313 |
left := right := AVLTree::AVLNil.
|
|
314 |
^temp].
|
|
315 |
(aNode precedes: self orderedBy: binaryBlock)
|
|
316 |
ifTrue: [left := left avlTreeNodeRemove: aNode orderedBy: binaryBlock]
|
|
317 |
ifFalse: [right := right avlTreeNodeRemove: aNode orderedBy: binaryBlock].
|
|
318 |
^self balance
|
|
319 |
! !
|
|
320 |
|
|
321 |
!AVLTree::AVLTreeNode methodsFor:'enumeration'!
|
|
322 |
|
|
323 |
avlTreeNodeDo: unaryBlock
|
|
324 |
left avlTreeNodeDo: unaryBlock.
|
|
325 |
unaryBlock value: value.
|
|
326 |
right avlTreeNodeDo: unaryBlock.
|
|
327 |
!
|
|
328 |
|
|
329 |
avlTreeNodeReverseDo: unaryBlock
|
|
330 |
right avlTreeNodeReverseDo: unaryBlock.
|
|
331 |
unaryBlock value: value.
|
|
332 |
left avlTreeNodeReverseDo: unaryBlock.
|
|
333 |
! !
|
|
334 |
|
|
335 |
!AVLTree::AVLTreeNode methodsFor:'initialization'!
|
|
336 |
|
|
337 |
initialize
|
|
338 |
left := right := AVLTree::AVLNil.
|
|
339 |
height := 0.
|
|
340 |
value := nil.
|
|
341 |
! !
|
|
342 |
|
|
343 |
!AVLTree::AVLTreeNode methodsFor:'misc'!
|
|
344 |
|
|
345 |
avlTreeNodeFind: aNode orderedBy: binaryBlock
|
|
346 |
(self equals:aNode orderedBy: binaryBlock) ifTrue: [^self].
|
|
347 |
^(aNode precedes: self orderedBy: binaryBlock)
|
|
348 |
ifTrue: [left avlTreeNodeFind: aNode orderedBy: binaryBlock]
|
|
349 |
ifFalse: [right avlTreeNodeFind: aNode orderedBy: binaryBlock]
|
|
350 |
!
|
|
351 |
|
|
352 |
avlTreeNodeMoveRight: aNode
|
|
353 |
right := right avlTreeNodeMoveRight: aNode.
|
|
354 |
^self balance
|
|
355 |
! !
|
|
356 |
|
|
357 |
!AVLTree::AVLTreeNode methodsFor:'printing & storing'!
|
|
358 |
|
|
359 |
printOn: aStream
|
|
360 |
super printOn: aStream.
|
|
361 |
aStream
|
|
362 |
nextPut: $(;
|
|
363 |
print: value;
|
|
364 |
nextPut: $)
|
|
365 |
! !
|
|
366 |
|
|
367 |
!AVLTree::AVLTreeNode methodsFor:'private'!
|
|
368 |
|
|
369 |
balance
|
|
370 |
| delta |
|
|
371 |
delta := self delta.
|
|
372 |
delta < -1
|
|
373 |
ifTrue:
|
|
374 |
[right delta > 0 ifTrue: [right := right rotateRight].
|
|
375 |
^self rotateLeft].
|
|
376 |
delta > 1
|
|
377 |
ifTrue:
|
|
378 |
[left delta < 0 ifTrue: [left := left rotateLeft].
|
|
379 |
^self rotateRight].
|
|
380 |
height := 0.
|
|
381 |
(left avlTreeNodeHeight > height) ifTrue: [height := left avlTreeNodeHeight].
|
|
382 |
(right avlTreeNodeHeight > height) ifTrue: [height := right avlTreeNodeHeight].
|
|
383 |
height := height + 1.
|
|
384 |
!
|
|
385 |
|
|
386 |
rotateLeft
|
|
387 |
| pivot |
|
|
388 |
pivot := right.
|
|
389 |
right := pivot left.
|
|
390 |
pivot left: self balance.
|
|
391 |
^pivot balance
|
|
392 |
!
|
|
393 |
|
|
394 |
rotateRight
|
|
395 |
| pivot |
|
|
396 |
pivot := left.
|
|
397 |
left := pivot right.
|
|
398 |
pivot right: self balance.
|
|
399 |
^pivot balance
|
|
400 |
! !
|
|
401 |
|
|
402 |
!AVLTree::AVLTreeNode methodsFor:'queries'!
|
|
403 |
|
|
404 |
avlTreeNodeHeight
|
|
405 |
^height
|
|
406 |
!
|
|
407 |
|
|
408 |
avlTreeNodeInsert: aNode orderedBy: binaryBlock
|
|
409 |
(aNode precedes: self orderedBy: binaryBlock)
|
|
410 |
ifTrue: [left := left avlTreeNodeInsert: aNode orderedBy: binaryBlock]
|
|
411 |
ifFalse: [right := right avlTreeNodeInsert: aNode orderedBy: binaryBlock].
|
|
412 |
^self balance
|
|
413 |
!
|
|
414 |
|
|
415 |
avlTreeSize
|
|
416 |
^ (left avlTreeSize) + 1 + (right avlTreeSize)
|
|
417 |
!
|
|
418 |
|
|
419 |
delta
|
|
420 |
^ (left avlTreeNodeHeight) - (right avlTreeNodeHeight)
|
|
421 |
!
|
|
422 |
|
|
423 |
equals: aNode orderedBy: binaryBlock
|
|
424 |
| l r lr rl |
|
|
425 |
l := self value.
|
|
426 |
r := aNode value.
|
|
427 |
lr := binaryBlock value: l value: r.
|
|
428 |
rl := binaryBlock value: r value: l.
|
|
429 |
"Partial order (<=): l = r => (l <= r) and (l >= r) => lr and rl."
|
|
430 |
"Strict order (< ): l = r => not (l < r) and not (l > r) => not (lr) and not (rl)."
|
|
431 |
"Augustus tells us the latter really means l = r => not (lr or rl), saving us one send."
|
|
432 |
^(lr and: [rl]) or: [(lr or: [rl]) not]
|
|
433 |
!
|
|
434 |
|
|
435 |
precedes: aNode orderedBy: binaryBlock
|
|
436 |
^ binaryBlock value: (self value) value: (aNode value)
|
|
437 |
! !
|
|
438 |
|
|
439 |
!AVLTree class methodsFor:'documentation'!
|
|
440 |
|
|
441 |
version
|
4115
|
442 |
^ '$Header$'
|
4117
|
443 |
!
|
|
444 |
|
|
445 |
version_CVS
|
|
446 |
^ '$Header$'
|
1931
|
447 |
! !
|
4115
|
448 |
|