2770
|
1 |
"
|
2777
|
2 |
COPYRIGHT (c) 2003 by eXept Software AG
|
|
3 |
All Rights Reserved
|
2770
|
4 |
|
2777
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
2770
|
11 |
"
|
2777
|
12 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
3666
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
2777
|
16 |
Collection subclass:#BinaryTree
|
|
17 |
instanceVariableNames:'treeRoot sortBlock'
|
|
18 |
classVariableNames:'DefaultSortBlock'
|
2776
|
19 |
poolDictionaries:''
|
|
20 |
category:'Collections-Ordered-Trees'
|
2770
|
21 |
!
|
|
22 |
|
2777
|
23 |
!BinaryTree class methodsFor:'documentation'!
|
2770
|
24 |
|
2777
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 2003 by eXept Software AG
|
|
28 |
All Rights Reserved
|
2770
|
29 |
|
2777
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
!
|
|
38 |
|
|
39 |
documentation
|
|
40 |
"
|
|
41 |
Loosely based on the Public Domain BinaryTreeNode class from Steve Chepurny.
|
|
42 |
|
|
43 |
WARNING:
|
|
44 |
This tree does not reorganize itself.
|
4119
|
45 |
Thus, its performance might degenerate to that of a linked list (see performance) when elements
|
|
46 |
are added in 'already sorted' or reverse order.
|
|
47 |
The performance is OK, if elements are added in random order and the tree is therefore more or less
|
|
48 |
balanced.
|
2777
|
49 |
The worst case is to add elements in order, reverseOrder or zig-zag order.
|
|
50 |
Use instances of my subclasses, which balance the tree if in doubt.
|
|
51 |
|
3666
|
52 |
EXTRA WARNING:
|
|
53 |
the inherited storeString will generate the elements in sorted order,
|
4119
|
54 |
which generates exactly the generated worst case when read-back.
|
3666
|
55 |
If you use this class and need textual persistency, you should consider rewriting
|
|
56 |
the storeOn: method, to enumerate elements in a binary segmentation fashion.
|
4119
|
57 |
Otherwise, please use one of the self-balancing trees instead,
|
3666
|
58 |
for example AATree or BTree.
|
|
59 |
|
2777
|
60 |
Changes:
|
|
61 |
Changed to be Collection-protocol compatible.
|
|
62 |
Slight speedup in insert-code.
|
|
63 |
|
|
64 |
[author:]
|
|
65 |
Steve Chepurny (original BinaryTreeNode implementation)
|
|
66 |
Claus Gittinger (cg@alan)
|
|
67 |
|
|
68 |
[instance variables:]
|
|
69 |
|
|
70 |
[class variables:]
|
|
71 |
|
|
72 |
[see also:]
|
|
73 |
|
|
74 |
"
|
|
75 |
!
|
|
76 |
|
|
77 |
examples
|
|
78 |
"
|
|
79 |
a degenerated tree:
|
|
80 |
[exBegin]
|
|
81 |
|coll|
|
|
82 |
|
|
83 |
coll := BinaryTree new.
|
|
84 |
(1 to:10) do:[:i | coll add:i].
|
|
85 |
coll addAll:(20 to:30).
|
|
86 |
coll inspect
|
|
87 |
[exEnd]
|
|
88 |
|
|
89 |
[exBegin]
|
|
90 |
|tree|
|
|
91 |
|
|
92 |
tree := self new.
|
|
93 |
tree add:'hello'.
|
|
94 |
tree add:'aaaa'.
|
|
95 |
tree add:'AAAb'.
|
|
96 |
tree add:'aaaC'.
|
|
97 |
tree add:'world'.
|
|
98 |
tree asOrderedCollection
|
|
99 |
[exEnd]
|
|
100 |
|
|
101 |
[exBegin]
|
|
102 |
|tree|
|
|
103 |
|
|
104 |
tree := self sortBlock:[:a :b | a asLowercase < b asLowercase].
|
|
105 |
tree add:'hello'.
|
|
106 |
tree add:'aaaa'.
|
|
107 |
tree add:'AAAb'.
|
|
108 |
tree add:'aaaC'.
|
|
109 |
tree add:'world'.
|
|
110 |
tree asOrderedCollection
|
|
111 |
[exEnd]
|
|
112 |
|
|
113 |
timing examples and benchmarks: see examples in AATree:
|
2770
|
114 |
|
|
115 |
|
2777
|
116 |
A functional example of a UCB-CS61A lecture's example.
|
|
117 |
The task is to extract all values within a given range (min..max) from
|
|
118 |
a binary tree.
|
|
119 |
The range 'function' below does this; given a binary tree, a min and max value,
|
|
120 |
range(bst, min, max)
|
|
121 |
returns an array of all values which are within that range.
|
|
122 |
Only the relevant branches of the binary tree are to be visited, of course.
|
|
123 |
[exBegin]
|
|
124 |
|t rangeNode range|
|
2770
|
125 |
|
2777
|
126 |
t := BinaryTree new.
|
|
127 |
t add:54; add:37; add:19; add:45; add:80; add:65; add:91; add:57.
|
|
128 |
|
|
129 |
rangeNode := [:node :min :max |
|
|
130 |
|nodeValue leftTree rightTree left right middle|
|
|
131 |
|
|
132 |
leftTree := node leftSubtree.
|
|
133 |
rightTree := node rightSubtree.
|
|
134 |
nodeValue := node data.
|
|
135 |
|
|
136 |
left := (leftTree notNil and:[nodeValue > min])
|
|
137 |
ifTrue:[ rangeNode value:leftTree value:min value:max ]
|
|
138 |
ifFalse:[ #() ].
|
|
139 |
|
|
140 |
right := (rightTree notNil and:[nodeValue < max])
|
|
141 |
ifTrue:[ rangeNode value:rightTree value:min value:max ]
|
|
142 |
ifFalse:[ #() ].
|
|
143 |
|
|
144 |
middle := (nodeValue between:min and:max)
|
|
145 |
ifTrue:[ (Array with:nodeValue) ]
|
|
146 |
ifFalse:[ #() ].
|
|
147 |
|
|
148 |
left, middle, right
|
|
149 |
].
|
|
150 |
range := [:tree :min :max |
|
|
151 |
rangeNode value:tree rootNode value:min value:max
|
|
152 |
].
|
|
153 |
range value:t value:30 value:60.
|
|
154 |
[exEnd]
|
|
155 |
|
|
156 |
"
|
|
157 |
! !
|
|
158 |
|
|
159 |
!BinaryTree class methodsFor:'initialization'!
|
|
160 |
|
|
161 |
initialize
|
|
162 |
"setup the default sortBlock.
|
|
163 |
Use #<, since this is the base method in Magnitude."
|
|
164 |
|
|
165 |
"/ only do this once at early startup
|
|
166 |
DefaultSortBlock isNil ifTrue:[
|
|
167 |
DefaultSortBlock := [:a :b | a < b]
|
|
168 |
]
|
|
169 |
|
|
170 |
"
|
|
171 |
BinaryTree initialize
|
|
172 |
"
|
|
173 |
! !
|
|
174 |
|
|
175 |
!BinaryTree class methodsFor:'instance creation'!
|
|
176 |
|
|
177 |
new
|
|
178 |
"return a new instance using the default sortOrder (which is a < b)"
|
|
179 |
|
|
180 |
^ self basicNew sortBlock:DefaultSortBlock
|
|
181 |
!
|
|
182 |
|
|
183 |
new:n
|
|
184 |
"return a new instance using the default sortOrder (which is a < b)"
|
|
185 |
|
|
186 |
^ self new
|
|
187 |
!
|
|
188 |
|
|
189 |
sortBlock:aTwoArgBlock
|
|
190 |
"return a new instance using the given sortBlock (which returns true if a < b)"
|
|
191 |
|
|
192 |
^ self basicNew sortBlock:aTwoArgBlock
|
|
193 |
|
|
194 |
"
|
|
195 |
|tree|
|
|
196 |
|
|
197 |
tree := self sortBlock:[:a :b | a asLowercase < b asLowercase].
|
|
198 |
tree add:'hello'.
|
|
199 |
tree add:'aaaa'.
|
|
200 |
tree add:'AAAb'.
|
|
201 |
tree add:'aaaC'.
|
|
202 |
tree add:'world'.
|
|
203 |
tree asOrderedCollection
|
|
204 |
"
|
|
205 |
! !
|
|
206 |
|
|
207 |
!BinaryTree methodsFor:'accessing'!
|
|
208 |
|
|
209 |
rootNode
|
|
210 |
"return the rootNode of the tree"
|
|
211 |
|
|
212 |
^ treeRoot
|
|
213 |
!
|
|
214 |
|
|
215 |
sortBlock:something
|
|
216 |
"set the sort block.
|
|
217 |
This is allowed only before any elements are stored in the tree"
|
|
218 |
|
|
219 |
self assert:treeRoot isNil message:'changing sortBlock in BinaryTree'.
|
|
220 |
sortBlock := something.
|
|
221 |
! !
|
|
222 |
|
|
223 |
!BinaryTree methodsFor:'adding & removing'!
|
|
224 |
|
|
225 |
add:anObject
|
2934
|
226 |
"add anObject to the collection. The object is inserted as defined by the sortBlock.
|
|
227 |
Returns anObject"
|
2777
|
228 |
|
|
229 |
treeRoot isNil ifTrue:[
|
2785
|
230 |
treeRoot := self rootTreeNodeClass data:anObject.
|
2777
|
231 |
] ifFalse:[
|
2785
|
232 |
treeRoot insertNode:(self treeNodeClass data:anObject) sortBlock:sortBlock
|
2777
|
233 |
].
|
|
234 |
^ anObject "sigh - collection protocol"
|
|
235 |
|
|
236 |
"
|
|
237 |
BinaryTree withAll:#(16 3 1 0 4 7 9)
|
|
238 |
BinaryTree new add:1; add:2; yourself
|
|
239 |
BinaryTree with:1 with:2 with:3
|
|
240 |
"
|
|
241 |
|
2785
|
242 |
"Modified: / 05-08-2012 / 12:34:42 / cg"
|
2777
|
243 |
!
|
|
244 |
|
|
245 |
includes:anElement
|
|
246 |
"return true, if the argument, anObject is contained in the collection.
|
|
247 |
Uses #= when comparing; i.e. the search is for an equal object."
|
|
248 |
|
|
249 |
treeRoot isNil ifTrue:[
|
|
250 |
^ false.
|
|
251 |
].
|
|
252 |
^ treeRoot includesValue:anElement sortBlock:sortBlock.
|
|
253 |
|
|
254 |
"
|
|
255 |
BinaryTree new
|
|
256 |
addAll:#( 2 1 4.0 3 6 5 7);
|
|
257 |
includes:4
|
|
258 |
"
|
|
259 |
"
|
|
260 |
BinaryTree new
|
|
261 |
addAll:#( 2 1 4.0 3 6 5 7);
|
|
262 |
includes:4.0
|
|
263 |
"
|
|
264 |
|
|
265 |
"
|
|
266 |
BinaryTree new
|
|
267 |
addAll:#(2 1 4 3 6 5 7);
|
|
268 |
includesIdentical:4
|
|
269 |
"
|
|
270 |
|
|
271 |
"
|
|
272 |
BinaryTree new
|
|
273 |
addAll:#( 2 1 4.0 3 6 5 7);
|
|
274 |
includesIdentical:4
|
|
275 |
"
|
|
276 |
!
|
|
277 |
|
|
278 |
includesIdentical:anElement
|
|
279 |
"return true, if the argument, anObject is contained in the collection.
|
|
280 |
Uses #== (instead of #=) when comparing;
|
|
281 |
i.e. the search is for the object itself, not some object being just equal."
|
2770
|
282 |
|
2777
|
283 |
treeRoot isNil ifTrue:[
|
|
284 |
^ false.
|
|
285 |
].
|
|
286 |
^ treeRoot includesIdenticalValue:anElement sortBlock:sortBlock.
|
2770
|
287 |
|
2777
|
288 |
"
|
|
289 |
BinaryTree new
|
|
290 |
addAll:#(4 2 1 4.0 3 6 5 7);
|
|
291 |
includesIdentical:4
|
|
292 |
"
|
2770
|
293 |
|
2777
|
294 |
"
|
|
295 |
BinaryTree new
|
|
296 |
addAll:#( 2 1 4.0 3 6 5 7);
|
|
297 |
includesIdentical:4
|
|
298 |
"
|
|
299 |
|
|
300 |
"
|
|
301 |
BinaryTree new
|
|
302 |
addAll:#(4 2 1 3 6 5 7);
|
|
303 |
includesIdentical:8
|
|
304 |
"
|
|
305 |
|
|
306 |
"
|
|
307 |
BinaryTree new
|
|
308 |
addAll:#( 2 1 4.0 3 6 5 7);
|
|
309 |
includes:4
|
|
310 |
"
|
|
311 |
"
|
|
312 |
BinaryTree new
|
|
313 |
addAll:#( 2 1 4.0 3 6 5 7);
|
|
314 |
includes:4.0
|
|
315 |
"
|
|
316 |
!
|
|
317 |
|
|
318 |
remove:oldObject ifAbsent:exceptionValue
|
|
319 |
|newRoot|
|
|
320 |
|
|
321 |
treeRoot isNil ifTrue:[
|
|
322 |
^ exceptionValue value.
|
|
323 |
].
|
|
324 |
newRoot := treeRoot removeValue:oldObject using:#= sortBlock:sortBlock.
|
|
325 |
newRoot isNil ifTrue:[
|
|
326 |
treeRoot data = oldObject ifFalse:[
|
|
327 |
^ exceptionValue value.
|
|
328 |
].
|
|
329 |
].
|
|
330 |
treeRoot := newRoot.
|
|
331 |
^ oldObject "sigh - collection protocol"
|
|
332 |
|
|
333 |
"
|
|
334 |
BinaryTree new
|
|
335 |
addAll:#(4 2 1 3 6 5 7);
|
|
336 |
removeIdentical:4;
|
|
337 |
yourself
|
|
338 |
"
|
|
339 |
|
|
340 |
"
|
|
341 |
BinaryTree new
|
|
342 |
addAll:#(4 2 1 3 6 5 7);
|
|
343 |
removeIdentical:7;
|
|
344 |
yourself
|
|
345 |
"
|
|
346 |
!
|
|
347 |
|
|
348 |
removeIdentical:oldObject ifAbsent:exceptionValue
|
|
349 |
|newRoot|
|
|
350 |
|
|
351 |
treeRoot isNil ifTrue:[
|
|
352 |
^ exceptionValue value.
|
|
353 |
].
|
|
354 |
newRoot := treeRoot removeValue:oldObject using:#== sortBlock:sortBlock.
|
|
355 |
newRoot isNil ifTrue:[
|
|
356 |
treeRoot data == oldObject ifFalse:[
|
|
357 |
^ exceptionValue value.
|
|
358 |
].
|
|
359 |
].
|
|
360 |
treeRoot := newRoot.
|
|
361 |
^ oldObject "sigh - collection protocol"
|
|
362 |
|
|
363 |
"
|
|
364 |
BinaryTree new
|
|
365 |
addAll:#(4 2 1 3 6 5 7);
|
|
366 |
removeIdentical:4;
|
|
367 |
yourself
|
|
368 |
"
|
|
369 |
|
|
370 |
"
|
|
371 |
BinaryTree new
|
|
372 |
addAll:#(4 2 1 3 6 5 7);
|
|
373 |
removeIdentical:7;
|
|
374 |
yourself
|
|
375 |
"
|
|
376 |
! !
|
|
377 |
|
|
378 |
!BinaryTree methodsFor:'enumerating'!
|
|
379 |
|
|
380 |
do:aBlock
|
|
381 |
"enumerate the tree in order"
|
|
382 |
|
|
383 |
treeRoot notNil ifTrue:[
|
|
384 |
treeRoot inOrderDo:[:eachNode | aBlock value:(eachNode data)].
|
|
385 |
].
|
|
386 |
|
|
387 |
"
|
|
388 |
|coll|
|
|
389 |
|
|
390 |
coll:= OrderedCollection new.
|
|
391 |
(BinaryTree withAll:#(5 4 3 2 1 6 7 8 9 0)) do:[:each| coll add:each].
|
|
392 |
coll
|
|
393 |
"
|
|
394 |
|
|
395 |
"
|
|
396 |
|coll|
|
|
397 |
|
|
398 |
coll:= OrderedCollection new.
|
|
399 |
(BinaryTree withAll:#(5 4 3 2 1 6 7 8 9 0)) preOrderDo:[:each| coll add:each].
|
|
400 |
coll
|
|
401 |
"
|
|
402 |
|
|
403 |
"
|
|
404 |
|coll|
|
|
405 |
|
|
406 |
coll:= OrderedCollection new.
|
|
407 |
(BinaryTree withAll:#(5 4 3 2 1 6 7 8 9 0)) postOrderDo:[:each| coll add:each].
|
|
408 |
coll
|
|
409 |
"
|
|
410 |
!
|
|
411 |
|
|
412 |
postOrderDo:aBlock
|
|
413 |
"enumerate in postOrder - Left, Right, Root"
|
|
414 |
|
|
415 |
treeRoot notNil ifTrue:[
|
|
416 |
treeRoot postOrderDo:[:eachNode | aBlock value:(eachNode data)].
|
|
417 |
].
|
|
418 |
!
|
|
419 |
|
|
420 |
preOrderDo:aBlock
|
|
421 |
"enumerate in preOrder - Root, Left, Right"
|
|
422 |
|
|
423 |
treeRoot notNil ifTrue:[
|
|
424 |
treeRoot preOrderDo:[:eachNode | aBlock value:(eachNode data)].
|
|
425 |
].
|
|
426 |
! !
|
|
427 |
|
|
428 |
!BinaryTree methodsFor:'queries'!
|
|
429 |
|
3015
|
430 |
isFixedSize
|
|
431 |
"return true if the receiver cannot grow"
|
|
432 |
|
|
433 |
^ false
|
|
434 |
!
|
|
435 |
|
2785
|
436 |
rootTreeNodeClass
|
|
437 |
^ self treeNodeClass
|
|
438 |
|
|
439 |
"Created: / 05-08-2012 / 12:30:26 / cg"
|
|
440 |
!
|
|
441 |
|
2777
|
442 |
size
|
|
443 |
"return the number of tree elements"
|
|
444 |
|
|
445 |
^ treeRoot size
|
|
446 |
!
|
|
447 |
|
|
448 |
treeNodeClass
|
|
449 |
^ BinaryTreeNode
|
|
450 |
|
|
451 |
"Created: / 05-08-2012 / 11:36:26 / cg"
|
|
452 |
! !
|
|
453 |
|
|
454 |
!BinaryTree class methodsFor:'documentation'!
|
|
455 |
|
|
456 |
version
|
3666
|
457 |
^ '$Header$'
|
2777
|
458 |
!
|
|
459 |
|
|
460 |
version_CVS
|
3666
|
461 |
^ '$Header$'
|
2777
|
462 |
! !
|
|
463 |
|
2934
|
464 |
|
2777
|
465 |
BinaryTree initialize!
|