4682
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
2279
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2009 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
2266
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
3537
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
2266
|
18 |
BinaryTree subclass:#AATree
|
|
19 |
instanceVariableNames:''
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
2761
|
22 |
category:'Collections-Ordered-Trees'
|
2266
|
23 |
!
|
|
24 |
|
|
25 |
!AATree class methodsFor:'documentation'!
|
|
26 |
|
2279
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2009 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
2266
|
41 |
documentation
|
|
42 |
"
|
|
43 |
An AA tree is a form of balanced tree used for storing and retrieving ordered data efficiently.
|
|
44 |
AA trees are named for Arne Andersson, their inventor.
|
|
45 |
|
|
46 |
AA trees are a variation of the red-black tree, which in turn is an enhancement to the
|
4682
|
47 |
binary search tree.
|
|
48 |
One caveat with the implementation is that it needs an extra level instvar in the treeNode,
|
|
49 |
which results in 25% more storage overhead as compared to a Red/Black or plain Binary trees.
|
|
50 |
For performance data, red the comment in the performance documentation method.
|
2266
|
51 |
|
3537
|
52 |
Usage:
|
|
53 |
As seen in the performance charts, AA trees offer better average and worst case
|
4682
|
54 |
performance compared to SortedCollection, with a slightly lower best case performance.
|
4116
|
55 |
Thus providing a more predictable performance
|
|
56 |
(within a factor of 2, as opposed to a much wider range for sortedCollections)
|
3537
|
57 |
|
4142
|
58 |
[instance variables:]
|
|
59 |
treeRoot TreeNode the top node
|
|
60 |
sortBlock Block sorter;
|
|
61 |
gets two args a,b and should return true if a is
|
|
62 |
to come before b in the collection
|
|
63 |
|
2266
|
64 |
[author:]
|
|
65 |
Original algorithm by Arne Andersson
|
4114
|
66 |
ported from wikipedia to Smalltalk code by Claus Gittinger
|
4116
|
67 |
|
|
68 |
[see also:]
|
4142
|
69 |
BTree
|
|
70 |
SortedCollection
|
4116
|
71 |
https://en.wikipedia.org/wiki/AA_tree
|
2266
|
72 |
"
|
|
73 |
!
|
|
74 |
|
|
75 |
examples
|
|
76 |
"
|
|
77 |
[exBegin]
|
|
78 |
|coll|
|
|
79 |
|
|
80 |
coll := AATree new.
|
|
81 |
(1 to:10) do:[:i | coll add:i].
|
|
82 |
coll addAll:(20 to:30).
|
|
83 |
coll inspect
|
|
84 |
[exEnd]
|
|
85 |
|
|
86 |
[exBegin]
|
|
87 |
|tree|
|
|
88 |
|
|
89 |
tree := AATree new.
|
|
90 |
tree add:'hello'.
|
|
91 |
tree add:'aaaa'.
|
|
92 |
tree add:'AAAb'.
|
|
93 |
tree add:'aaaC'.
|
|
94 |
tree add:'world'.
|
|
95 |
tree asOrderedCollection
|
|
96 |
[exEnd]
|
|
97 |
|
|
98 |
[exBegin]
|
|
99 |
|tree|
|
|
100 |
|
|
101 |
tree := AATree sortBlock:[:a :b | a asLowercase < b asLowercase].
|
|
102 |
tree add:'hello'.
|
|
103 |
tree add:'aaaa'.
|
|
104 |
tree add:'AAAb'.
|
|
105 |
tree add:'aaaC'.
|
|
106 |
tree add:'world'.
|
|
107 |
tree asOrderedCollection
|
|
108 |
[exEnd]
|
|
109 |
|
|
110 |
timing 1:
|
|
111 |
[exBegin]
|
4116
|
112 |
|N randomNumbers coll1_BT coll2_AT coll3_SC coll4_AVL t1_BT t2_AT t3_SC t4_AVL|
|
2266
|
113 |
|
|
114 |
N := 1000000.
|
|
115 |
randomNumbers := (1 to:N) collect:[:i | Random nextInteger].
|
|
116 |
|
|
117 |
ObjectMemory garbageCollect.
|
|
118 |
t1_BT := Time millisecondsToRun:[
|
|
119 |
coll1_BT := BinaryTree new.
|
|
120 |
coll1_BT addAll:randomNumbers
|
|
121 |
].
|
|
122 |
coll1_BT := nil.
|
|
123 |
ObjectMemory garbageCollect.
|
|
124 |
|
|
125 |
t2_AT := Time millisecondsToRun:[
|
|
126 |
coll2_AT := AATree new.
|
|
127 |
coll2_AT addAll:randomNumbers
|
|
128 |
].
|
|
129 |
coll2_AT := nil.
|
|
130 |
ObjectMemory garbageCollect.
|
|
131 |
|
|
132 |
t3_SC := Time millisecondsToRun:[
|
|
133 |
coll3_SC := SortedCollection new.
|
|
134 |
coll3_SC addAll:randomNumbers
|
|
135 |
].
|
|
136 |
coll3_SC := nil.
|
|
137 |
ObjectMemory garbageCollect.
|
4116
|
138 |
|
|
139 |
t4_AVL := Time millisecondsToRun:[
|
|
140 |
coll4_AVL := AVLTree new.
|
|
141 |
coll4_AVL addAll:randomNumbers
|
|
142 |
].
|
|
143 |
coll4_AVL := nil.
|
|
144 |
ObjectMemory garbageCollect.
|
|
145 |
|
2266
|
146 |
randomNumbers := nil.
|
|
147 |
|
|
148 |
Transcript show:'Time to insert random '; show:N; show:' into SortedCollection: '; show:t3_SC; showCR:'ms'.
|
|
149 |
Transcript show:'Time to insert random '; show:N; show:' into BinaryTree: '; show:t1_BT; showCR:'ms'.
|
|
150 |
Transcript show:'Time to insert random '; show:N; show:' into AATree: '; show:t2_AT; showCR:'ms'.
|
4116
|
151 |
Transcript show:'Time to insert random '; show:N; show:' into AVLTree: '; show:t4_AVL; showCR:'ms'.
|
2266
|
152 |
|
|
153 |
ObjectMemory garbageCollect.
|
|
154 |
t1_BT := Time millisecondsToRun:[
|
|
155 |
coll1_BT := BinaryTree new.
|
|
156 |
coll1_BT addAll:(1 to:100000).
|
|
157 |
].
|
|
158 |
coll1_BT := nil.
|
|
159 |
ObjectMemory garbageCollect.
|
|
160 |
|
|
161 |
t2_AT := Time millisecondsToRun:[
|
|
162 |
coll2_AT := AATree new.
|
|
163 |
coll2_AT addAll:(1 to:100000)
|
|
164 |
].
|
|
165 |
coll2_AT := nil.
|
|
166 |
ObjectMemory garbageCollect.
|
|
167 |
|
|
168 |
t3_SC := Time millisecondsToRun:[
|
|
169 |
coll3_SC := SortedCollection new.
|
|
170 |
coll3_SC addAll:(1 to:100000)
|
|
171 |
].
|
|
172 |
coll3_SC := nil.
|
|
173 |
ObjectMemory garbageCollect.
|
|
174 |
|
4116
|
175 |
t4_AVL := Time millisecondsToRun:[
|
|
176 |
coll4_AVL := AVLTree new.
|
|
177 |
coll4_AVL addAll:(1 to:100000)
|
|
178 |
].
|
|
179 |
coll4_AVL := nil.
|
|
180 |
ObjectMemory garbageCollect.
|
|
181 |
|
2266
|
182 |
Transcript show:'Time to insert ordered '; show:N; show:' into SortedCollection: '; show:t3_SC; showCR:'ms'.
|
|
183 |
Transcript show:'Time to insert ordered '; show:N; show:' into BinaryTree: '; show:t1_BT; showCR:'ms'.
|
|
184 |
Transcript show:'Time to insert ordered '; show:N; show:' into AATree: '; show:t2_AT; showCR:'ms'.
|
4116
|
185 |
Transcript show:'Time to insert ordered '; show:N; show:' into AVLTree: '; show:t4_AVL; showCR:'ms'.
|
2266
|
186 |
|
|
187 |
ObjectMemory garbageCollect.
|
|
188 |
t1_BT := Time millisecondsToRun:[
|
|
189 |
coll1_BT := BinaryTree new.
|
|
190 |
coll1_BT addAll:(100000 downTo:1).
|
|
191 |
].
|
|
192 |
coll1_BT := nil.
|
|
193 |
ObjectMemory garbageCollect.
|
|
194 |
|
|
195 |
t2_AT := Time millisecondsToRun:[
|
|
196 |
coll2_AT := AATree new.
|
|
197 |
coll2_AT addAll:(100000 downTo:1)
|
|
198 |
].
|
|
199 |
coll2_AT := nil.
|
|
200 |
ObjectMemory garbageCollect.
|
|
201 |
|
|
202 |
t3_SC := Time millisecondsToRun:[
|
|
203 |
coll3_SC := SortedCollection new.
|
|
204 |
coll3_SC addAll:(100000 downTo:1)
|
|
205 |
].
|
|
206 |
coll3_SC := nil.
|
|
207 |
ObjectMemory garbageCollect.
|
|
208 |
|
4116
|
209 |
t4_AVL := Time millisecondsToRun:[
|
|
210 |
coll4_AVL := AVLTree new.
|
|
211 |
coll4_AVL addAll:(100000 downTo:1)
|
|
212 |
].
|
|
213 |
coll4_AVL := nil.
|
|
214 |
ObjectMemory garbageCollect.
|
|
215 |
|
2266
|
216 |
Transcript show:'Time to insert reverse ordered '; show:N; show:' into SortedCollection: '; show:t3_SC; showCR:'ms'.
|
|
217 |
Transcript show:'Time to insert reverse ordered '; show:N; show:' into BinaryTree: '; show:t1_BT; showCR:'ms'.
|
|
218 |
Transcript show:'Time to insert reverse ordered '; show:N; show:' into AATree: '; show:t2_AT; showCR:'ms'.
|
4116
|
219 |
Transcript show:'Time to insert reverse ordered '; show:N; show:' into AVLTree: '; show:t4_AVL; showCR:'ms'.
|
2266
|
220 |
[exEnd]
|
|
221 |
|
|
222 |
timing 2:
|
|
223 |
[exBegin]
|
2760
|
224 |
|allSelectors coll1_SC coll2_BT coll3_AT coll4_Trie t1_SC t2_BT t3_AT t4_Trie|
|
2266
|
225 |
|
|
226 |
allSelectors := OrderedCollection new.
|
|
227 |
Smalltalk allClassesDo:[:cls |
|
|
228 |
cls instAndClassSelectorsAndMethodsDo:[:sel :mthd |
|
|
229 |
allSelectors add:sel.
|
|
230 |
].
|
|
231 |
].
|
2760
|
232 |
Transcript show:'Unique selectors: '; show:allSelectors asSet size; showCR:''.
|
2266
|
233 |
|
|
234 |
t1_SC := Time millisecondsToRun:[
|
|
235 |
coll1_SC := SortedCollection new.
|
|
236 |
allSelectors do:[:sel |
|
|
237 |
coll1_SC add:sel
|
|
238 |
].
|
|
239 |
].
|
|
240 |
Transcript show:'Time to insert '; show:coll1_SC size; show:' selectors into SortedCollection: '; show:t1_SC; showCR:'ms'.
|
|
241 |
|
|
242 |
t2_BT := Time millisecondsToRun:[
|
|
243 |
coll2_BT := BinaryTree new.
|
|
244 |
allSelectors do:[:sel |
|
|
245 |
coll2_BT add:sel
|
|
246 |
].
|
|
247 |
].
|
|
248 |
Transcript show:'Time to insert '; show:coll2_BT size; show:' selectors into BinaryTree: '; show:t2_BT; showCR:'ms'.
|
|
249 |
|
|
250 |
t3_AT := Time millisecondsToRun:[
|
|
251 |
coll3_AT := BinaryTree new.
|
|
252 |
allSelectors do:[:sel |
|
|
253 |
coll3_AT add:sel
|
|
254 |
].
|
|
255 |
].
|
|
256 |
Transcript show:'Time to insert '; show:coll3_AT size; show:' selectors into AATree: '; show:t3_AT; showCR:'ms'.
|
|
257 |
|
2760
|
258 |
t4_Trie := Time millisecondsToRun:[
|
|
259 |
coll4_Trie := TrieCollection new.
|
|
260 |
allSelectors do:[:sel |
|
|
261 |
coll4_Trie add:sel
|
|
262 |
].
|
|
263 |
].
|
|
264 |
Transcript show:'Time to insert '; show:coll4_Trie size; show:' selectors into Trie: '; show:t4_Trie; showCR:'ms'.
|
|
265 |
|
2266
|
266 |
t1_SC := Time millisecondsToRun:[
|
|
267 |
allSelectors do:[:sel |
|
|
268 |
coll1_SC remove:sel
|
|
269 |
].
|
|
270 |
].
|
|
271 |
self assert:(coll1_SC isEmpty).
|
|
272 |
Transcript show:'Time to remove selectors from SortedCollection: '; show:t1_SC; showCR:'ms'.
|
|
273 |
|
|
274 |
t2_BT := Time millisecondsToRun:[
|
|
275 |
allSelectors do:[:sel |
|
|
276 |
coll2_BT remove:sel
|
|
277 |
].
|
|
278 |
].
|
|
279 |
self assert:(coll2_BT isEmpty).
|
|
280 |
Transcript show:'Time to remove selectors from BinaryTree: '; show:t2_BT; showCR:'ms'.
|
|
281 |
|
|
282 |
t3_AT := Time millisecondsToRun:[
|
|
283 |
allSelectors do:[:sel |
|
|
284 |
coll3_AT remove:sel
|
|
285 |
].
|
|
286 |
].
|
|
287 |
self assert:(coll3_AT isEmpty).
|
2760
|
288 |
Transcript show:'Time to remove selectors from AATree: '; show:t3_AT; showCR:'ms'.
|
|
289 |
|
|
290 |
t4_Trie := Time millisecondsToRun:[
|
|
291 |
allSelectors do:[:sel |
|
|
292 |
coll4_Trie remove:sel
|
|
293 |
].
|
|
294 |
].
|
|
295 |
self assert:(coll4_Trie isEmpty).
|
|
296 |
Transcript show:'Time to remove selectors from Trie: '; show:t4_Trie; showCR:'ms'.
|
2266
|
297 |
[exEnd]
|
|
298 |
|
|
299 |
"
|
|
300 |
!
|
|
301 |
|
|
302 |
performance
|
|
303 |
"
|
4118
|
304 |
warning: the times below are very old, taken on a pentium-class machine.
|
|
305 |
Your times will be much shorter, so only look at the ratios.
|
|
306 |
|
4116
|
307 |
SortedCollection keeps the collection sorted and dense.
|
4118
|
308 |
It is super fast, when adding in 'almost sorted' or almost reverse-sorted order,
|
|
309 |
or when only a few elements are to be added later.
|
4116
|
310 |
To create a big sorted collection, it is better to first collect them all unsorted in
|
4682
|
311 |
an OrderedCollection, then convert in one operation using #asSortedCollection.
|
4116
|
312 |
|
|
313 |
Time to insert random 100000 individually into SortedCollection: 6037ms
|
|
314 |
Time to insert random 100000 en-bloque into SortedCollection: 172ms
|
|
315 |
Time to insert in order 100000 individually into SortedCollection: 31ms
|
|
316 |
Time to insert in order 100000 en-bloque into SortedCollection: 125ms
|
|
317 |
Time to insert in reverse order 100000 individually into SortedCollection: 93ms
|
|
318 |
Time to insert in reverse order 100000 en-bloque into SortedCollection: 125ms
|
|
319 |
Time to remove in random order 100000 from SortedCollection: 6380ms
|
|
320 |
Time to remove in order 100000 from SortedCollection: 109ms
|
|
321 |
Time to remove in reverse order 100000 from SortedCollection: 125ms
|
|
322 |
|
|
323 |
BinaryTree (which is not balancing) degenerates to a linear list,
|
|
324 |
if elements come in already sorted or reverse sorted,
|
|
325 |
but behaves better than AATree if they come in randomly:
|
2266
|
326 |
|
4116
|
327 |
Time to insert random 100000 individually into BinaryTree: 156ms
|
|
328 |
Time to insert random 100000 en-bloque into BinaryTree: 156ms
|
|
329 |
Time to insert in order 100000 individually into BinaryTree: 195921ms
|
|
330 |
Time to insert in order 100000 en-bloque into BinaryTree: 205266ms
|
|
331 |
Time to insert in reverse order 100000 individually into BinaryTree: 202271ms
|
|
332 |
Time to insert in reverse order 100000 en-bloque into BinaryTree: 197684ms
|
|
333 |
Time to remove in random order 100000 from BinaryTree: 234ms
|
|
334 |
Time to remove in order 100000 from BinaryTree: 78ms
|
|
335 |
Time to remove in reverse order 100000 from BinaryTree: 78ms
|
2266
|
336 |
|
4116
|
337 |
AATree is slower than the best-case above (because it keeps the tre balanced),
|
|
338 |
but has a much better worst case performance.
|
|
339 |
Thus providing a more predictable performance (all roughly within a factor of 2):
|
|
340 |
|
|
341 |
Time to insert random 100000 individually into AATree: 281ms
|
|
342 |
Time to insert random 100000 en-bloque into AATree: 265ms
|
|
343 |
Time to insert in order 100000 individually into AATree: 281ms
|
|
344 |
Time to insert in order 100000 en-bloque into AATree: 328ms
|
|
345 |
Time to insert in reverse order 100000 individually into AATree: 203ms
|
|
346 |
Time to insert in reverse order 100000 en-bloque into AATree: 218ms
|
|
347 |
Time to remove in random order 100000 from AATree: 452ms
|
|
348 |
Time to remove in order 100000 from AATree: 312ms
|
|
349 |
Time to remove in reverse order 100000 from AATree: 499ms
|
2266
|
350 |
"
|
|
351 |
! !
|
|
352 |
|
|
353 |
!AATree methodsFor:'adding & removing'!
|
|
354 |
|
|
355 |
add:anObject
|
|
356 |
"add the argument, anObject to the receiver.
|
|
357 |
Returns the object (sigh).
|
|
358 |
|
|
359 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
360 |
Iterate over a copy to do this."
|
|
361 |
|
|
362 |
treeRoot isNil ifTrue:[
|
2769
|
363 |
treeRoot := self treeNodeClass data:anObject level:1.
|
2783
|
364 |
] ifFalse:[
|
|
365 |
treeRoot := treeRoot insert:anObject usingSortBlock:sortBlock.
|
2266
|
366 |
].
|
|
367 |
^ anObject "sigh - collection protocol"
|
2769
|
368 |
|
2783
|
369 |
"Modified: / 05-08-2012 / 12:36:26 / cg"
|
2769
|
370 |
!
|
|
371 |
|
|
372 |
treeNodeClass
|
|
373 |
^ AATreeNode
|
|
374 |
|
|
375 |
"Created: / 05-08-2012 / 11:39:29 / cg"
|
|
376 |
! !
|
|
377 |
|
2780
|
378 |
!AATree class methodsFor:'documentation'!
|
2769
|
379 |
|
|
380 |
version
|
4114
|
381 |
^ '$Header$'
|
2769
|
382 |
!
|
|
383 |
|
|
384 |
version_CVS
|
4114
|
385 |
^ '$Header$'
|
2769
|
386 |
! !
|
3537
|
387 |
|