3537
|
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
|
|
47 |
binary search tree. One caveat with the implementation is that it needs an extra level instvar
|
|
48 |
in the treeNode; this results in 25% more storage overhead as compared to a Red/Black or plain Binary tree.
|
|
49 |
For performance data, see performance.
|
|
50 |
|
3537
|
51 |
Usage:
|
|
52 |
As seen in the performance charts, AA trees offer better average and worst case
|
|
53 |
performance, with a lower best case performance.
|
|
54 |
Thus providing a more predictable performance (within a factor of 2, as opposed to
|
|
55 |
a much wider range for sortedCollections)
|
|
56 |
|
2266
|
57 |
[author:]
|
|
58 |
Original algorithm by Arne Andersson
|
|
59 |
ported from wikipedia to smalltalk code by Claus Gittinger
|
|
60 |
"
|
|
61 |
!
|
|
62 |
|
|
63 |
examples
|
|
64 |
"
|
|
65 |
[exBegin]
|
|
66 |
|coll|
|
|
67 |
|
|
68 |
coll := AATree new.
|
|
69 |
(1 to:10) do:[:i | coll add:i].
|
|
70 |
coll addAll:(20 to:30).
|
|
71 |
coll inspect
|
|
72 |
[exEnd]
|
|
73 |
|
|
74 |
[exBegin]
|
|
75 |
|tree|
|
|
76 |
|
|
77 |
tree := AATree new.
|
|
78 |
tree add:'hello'.
|
|
79 |
tree add:'aaaa'.
|
|
80 |
tree add:'AAAb'.
|
|
81 |
tree add:'aaaC'.
|
|
82 |
tree add:'world'.
|
|
83 |
tree asOrderedCollection
|
|
84 |
[exEnd]
|
|
85 |
|
|
86 |
[exBegin]
|
|
87 |
|tree|
|
|
88 |
|
|
89 |
tree := AATree sortBlock:[:a :b | a asLowercase < b asLowercase].
|
|
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 |
timing 1:
|
|
99 |
[exBegin]
|
|
100 |
|N randomNumbers coll1_BT coll2_AT coll3_SC t1_BT t2_AT t3_SC|
|
|
101 |
|
|
102 |
N := 1000000.
|
|
103 |
randomNumbers := (1 to:N) collect:[:i | Random nextInteger].
|
|
104 |
|
|
105 |
ObjectMemory garbageCollect.
|
|
106 |
t1_BT := Time millisecondsToRun:[
|
|
107 |
coll1_BT := BinaryTree new.
|
|
108 |
coll1_BT addAll:randomNumbers
|
|
109 |
].
|
|
110 |
coll1_BT := nil.
|
|
111 |
ObjectMemory garbageCollect.
|
|
112 |
|
|
113 |
t2_AT := Time millisecondsToRun:[
|
|
114 |
coll2_AT := AATree new.
|
|
115 |
coll2_AT addAll:randomNumbers
|
|
116 |
].
|
|
117 |
coll2_AT := nil.
|
|
118 |
ObjectMemory garbageCollect.
|
|
119 |
|
|
120 |
t3_SC := Time millisecondsToRun:[
|
|
121 |
coll3_SC := SortedCollection new.
|
|
122 |
coll3_SC addAll:randomNumbers
|
|
123 |
].
|
|
124 |
coll3_SC := nil.
|
|
125 |
ObjectMemory garbageCollect.
|
|
126 |
randomNumbers := nil.
|
|
127 |
|
|
128 |
Transcript show:'Time to insert random '; show:N; show:' into SortedCollection: '; show:t3_SC; showCR:'ms'.
|
|
129 |
Transcript show:'Time to insert random '; show:N; show:' into BinaryTree: '; show:t1_BT; showCR:'ms'.
|
|
130 |
Transcript show:'Time to insert random '; show:N; show:' into AATree: '; show:t2_AT; showCR:'ms'.
|
|
131 |
|
|
132 |
ObjectMemory garbageCollect.
|
|
133 |
t1_BT := Time millisecondsToRun:[
|
|
134 |
coll1_BT := BinaryTree new.
|
|
135 |
coll1_BT addAll:(1 to:100000).
|
|
136 |
].
|
|
137 |
coll1_BT := nil.
|
|
138 |
ObjectMemory garbageCollect.
|
|
139 |
|
|
140 |
t2_AT := Time millisecondsToRun:[
|
|
141 |
coll2_AT := AATree new.
|
|
142 |
coll2_AT addAll:(1 to:100000)
|
|
143 |
].
|
|
144 |
coll2_AT := nil.
|
|
145 |
ObjectMemory garbageCollect.
|
|
146 |
|
|
147 |
t3_SC := Time millisecondsToRun:[
|
|
148 |
coll3_SC := SortedCollection new.
|
|
149 |
coll3_SC addAll:(1 to:100000)
|
|
150 |
].
|
|
151 |
coll3_SC := nil.
|
|
152 |
ObjectMemory garbageCollect.
|
|
153 |
|
|
154 |
Transcript show:'Time to insert ordered '; show:N; show:' into SortedCollection: '; show:t3_SC; showCR:'ms'.
|
|
155 |
Transcript show:'Time to insert ordered '; show:N; show:' into BinaryTree: '; show:t1_BT; showCR:'ms'.
|
|
156 |
Transcript show:'Time to insert ordered '; show:N; show:' into AATree: '; show:t2_AT; showCR:'ms'.
|
|
157 |
|
|
158 |
ObjectMemory garbageCollect.
|
|
159 |
t1_BT := Time millisecondsToRun:[
|
|
160 |
coll1_BT := BinaryTree new.
|
|
161 |
coll1_BT addAll:(100000 downTo:1).
|
|
162 |
].
|
|
163 |
coll1_BT := nil.
|
|
164 |
ObjectMemory garbageCollect.
|
|
165 |
|
|
166 |
t2_AT := Time millisecondsToRun:[
|
|
167 |
coll2_AT := AATree new.
|
|
168 |
coll2_AT addAll:(100000 downTo:1)
|
|
169 |
].
|
|
170 |
coll2_AT := nil.
|
|
171 |
ObjectMemory garbageCollect.
|
|
172 |
|
|
173 |
t3_SC := Time millisecondsToRun:[
|
|
174 |
coll3_SC := SortedCollection new.
|
|
175 |
coll3_SC addAll:(100000 downTo:1)
|
|
176 |
].
|
|
177 |
coll3_SC := nil.
|
|
178 |
ObjectMemory garbageCollect.
|
|
179 |
|
|
180 |
Transcript show:'Time to insert reverse ordered '; show:N; show:' into SortedCollection: '; show:t3_SC; showCR:'ms'.
|
|
181 |
Transcript show:'Time to insert reverse ordered '; show:N; show:' into BinaryTree: '; show:t1_BT; showCR:'ms'.
|
|
182 |
Transcript show:'Time to insert reverse ordered '; show:N; show:' into AATree: '; show:t2_AT; showCR:'ms'.
|
|
183 |
[exEnd]
|
|
184 |
|
|
185 |
timing 2:
|
|
186 |
[exBegin]
|
2760
|
187 |
|allSelectors coll1_SC coll2_BT coll3_AT coll4_Trie t1_SC t2_BT t3_AT t4_Trie|
|
2266
|
188 |
|
|
189 |
allSelectors := OrderedCollection new.
|
|
190 |
Smalltalk allClassesDo:[:cls |
|
|
191 |
cls instAndClassSelectorsAndMethodsDo:[:sel :mthd |
|
|
192 |
allSelectors add:sel.
|
|
193 |
].
|
|
194 |
].
|
2760
|
195 |
Transcript show:'Unique selectors: '; show:allSelectors asSet size; showCR:''.
|
2266
|
196 |
|
|
197 |
t1_SC := Time millisecondsToRun:[
|
|
198 |
coll1_SC := SortedCollection new.
|
|
199 |
allSelectors do:[:sel |
|
|
200 |
coll1_SC add:sel
|
|
201 |
].
|
|
202 |
].
|
|
203 |
Transcript show:'Time to insert '; show:coll1_SC size; show:' selectors into SortedCollection: '; show:t1_SC; showCR:'ms'.
|
|
204 |
|
|
205 |
t2_BT := Time millisecondsToRun:[
|
|
206 |
coll2_BT := BinaryTree new.
|
|
207 |
allSelectors do:[:sel |
|
|
208 |
coll2_BT add:sel
|
|
209 |
].
|
|
210 |
].
|
|
211 |
Transcript show:'Time to insert '; show:coll2_BT size; show:' selectors into BinaryTree: '; show:t2_BT; showCR:'ms'.
|
|
212 |
|
|
213 |
t3_AT := Time millisecondsToRun:[
|
|
214 |
coll3_AT := BinaryTree new.
|
|
215 |
allSelectors do:[:sel |
|
|
216 |
coll3_AT add:sel
|
|
217 |
].
|
|
218 |
].
|
|
219 |
Transcript show:'Time to insert '; show:coll3_AT size; show:' selectors into AATree: '; show:t3_AT; showCR:'ms'.
|
|
220 |
|
2760
|
221 |
t4_Trie := Time millisecondsToRun:[
|
|
222 |
coll4_Trie := TrieCollection new.
|
|
223 |
allSelectors do:[:sel |
|
|
224 |
coll4_Trie add:sel
|
|
225 |
].
|
|
226 |
].
|
|
227 |
Transcript show:'Time to insert '; show:coll4_Trie size; show:' selectors into Trie: '; show:t4_Trie; showCR:'ms'.
|
|
228 |
|
2266
|
229 |
t1_SC := Time millisecondsToRun:[
|
|
230 |
allSelectors do:[:sel |
|
|
231 |
coll1_SC remove:sel
|
|
232 |
].
|
|
233 |
].
|
|
234 |
self assert:(coll1_SC isEmpty).
|
|
235 |
Transcript show:'Time to remove selectors from SortedCollection: '; show:t1_SC; showCR:'ms'.
|
|
236 |
|
|
237 |
t2_BT := Time millisecondsToRun:[
|
|
238 |
allSelectors do:[:sel |
|
|
239 |
coll2_BT remove:sel
|
|
240 |
].
|
|
241 |
].
|
|
242 |
self assert:(coll2_BT isEmpty).
|
|
243 |
Transcript show:'Time to remove selectors from BinaryTree: '; show:t2_BT; showCR:'ms'.
|
|
244 |
|
|
245 |
t3_AT := Time millisecondsToRun:[
|
|
246 |
allSelectors do:[:sel |
|
|
247 |
coll3_AT remove:sel
|
|
248 |
].
|
|
249 |
].
|
|
250 |
self assert:(coll3_AT isEmpty).
|
2760
|
251 |
Transcript show:'Time to remove selectors from AATree: '; show:t3_AT; showCR:'ms'.
|
|
252 |
|
|
253 |
t4_Trie := Time millisecondsToRun:[
|
|
254 |
allSelectors do:[:sel |
|
|
255 |
coll4_Trie remove:sel
|
|
256 |
].
|
|
257 |
].
|
|
258 |
self assert:(coll4_Trie isEmpty).
|
|
259 |
Transcript show:'Time to remove selectors from Trie: '; show:t4_Trie; showCR:'ms'.
|
2266
|
260 |
[exEnd]
|
|
261 |
|
|
262 |
"
|
|
263 |
!
|
|
264 |
|
|
265 |
performance
|
|
266 |
"
|
|
267 |
Time to insert random 100000 individually into SortedCollection: 6037ms
|
|
268 |
Time to insert random 100000 en-bloque into SortedCollection: 172ms
|
|
269 |
Time to insert in order 100000 individually into SortedCollection: 31ms
|
|
270 |
Time to insert in order 100000 en-bloque into SortedCollection: 125ms
|
|
271 |
Time to insert in reverse order 100000 individually into SortedCollection: 93ms
|
|
272 |
Time to insert in reverse order 100000 en-bloque into SortedCollection: 125ms
|
|
273 |
Time to remove in random order 100000 from SortedCollection: 6380ms
|
|
274 |
Time to remove in order 100000 from SortedCollection: 109ms
|
|
275 |
Time to remove in reverse order 100000 from SortedCollection: 125ms
|
|
276 |
|
|
277 |
Time to insert random 100000 individually into AATree: 281ms
|
|
278 |
Time to insert random 100000 en-bloque into AATree: 265ms
|
|
279 |
Time to insert in order 100000 individually into AATree: 281ms
|
|
280 |
Time to insert in order 100000 en-bloque into AATree: 328ms
|
|
281 |
Time to insert in reverse order 100000 individually into AATree: 203ms
|
|
282 |
Time to insert in reverse order 100000 en-bloque into AATree: 218ms
|
|
283 |
Time to remove in random order 100000 from AATree: 452ms
|
|
284 |
Time to remove in order 100000 from AATree: 312ms
|
2404
|
285 |
Time to remove in reverse order 100000 from AATree: 499ms
|
2266
|
286 |
|
|
287 |
Time to insert random 100000 individually into BinaryTree: 156ms
|
|
288 |
Time to insert random 100000 en-bloque into BinaryTree: 156ms
|
|
289 |
Time to insert in order 100000 individually into BinaryTree: 195921ms
|
|
290 |
Time to insert in order 100000 en-bloque into BinaryTree: 205266ms
|
|
291 |
Time to insert in reverse order 100000 individually into BinaryTree: 202271ms
|
|
292 |
Time to insert in reverse order 100000 en-bloque into BinaryTree: 197684ms
|
|
293 |
Time to remove in random order 100000 from BinaryTree: 234ms
|
|
294 |
Time to remove in order 100000 from BinaryTree: 78ms
|
|
295 |
Time to remove in reverse order 100000 from BinaryTree: 78ms
|
|
296 |
"
|
|
297 |
! !
|
|
298 |
|
|
299 |
!AATree methodsFor:'adding & removing'!
|
|
300 |
|
|
301 |
add:anObject
|
|
302 |
"add the argument, anObject to the receiver.
|
|
303 |
Returns the object (sigh).
|
|
304 |
|
|
305 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
306 |
Iterate over a copy to do this."
|
|
307 |
|
|
308 |
treeRoot isNil ifTrue:[
|
2769
|
309 |
treeRoot := self treeNodeClass data:anObject level:1.
|
2783
|
310 |
] ifFalse:[
|
|
311 |
treeRoot := treeRoot insert:anObject usingSortBlock:sortBlock.
|
2266
|
312 |
].
|
|
313 |
^ anObject "sigh - collection protocol"
|
2769
|
314 |
|
2783
|
315 |
"Modified: / 05-08-2012 / 12:36:26 / cg"
|
2769
|
316 |
!
|
|
317 |
|
|
318 |
treeNodeClass
|
|
319 |
^ AATreeNode
|
|
320 |
|
|
321 |
"Created: / 05-08-2012 / 11:39:29 / cg"
|
|
322 |
! !
|
|
323 |
|
2780
|
324 |
!AATree class methodsFor:'documentation'!
|
2769
|
325 |
|
|
326 |
version
|
3537
|
327 |
^ '$Header: /cvs/stx/stx/libbasic2/AATree.st,v 1.9 2015-04-10 06:55:43 cg Exp $'
|
2769
|
328 |
!
|
|
329 |
|
|
330 |
version_CVS
|
3537
|
331 |
^ '$Header: /cvs/stx/stx/libbasic2/AATree.st,v 1.9 2015-04-10 06:55:43 cg Exp $'
|
2769
|
332 |
! !
|
3537
|
333 |
|