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