2779
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2009 by eXept Software AG
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
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.
|
|
11 |
"
|
2782
|
12 |
"{ Package: 'stx:libbasic2' }"
|
2779
|
13 |
|
4266
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
2782
|
16 |
BinaryTreeNode subclass:#AATreeNode
|
2779
|
17 |
instanceVariableNames:'level'
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Collections-Ordered-Trees'
|
|
21 |
!
|
|
22 |
|
|
23 |
!AATreeNode class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 2009 by eXept Software AG
|
|
28 |
All Rights Reserved
|
|
29 |
|
|
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 |
"
|
3140
|
37 |
!
|
|
38 |
|
|
39 |
documentation
|
|
40 |
"
|
|
41 |
I represent nodes in an AA tree, which is a form of balanced tree used for storing and retrieving ordered data efficiently.
|
|
42 |
AA trees are named for Arne Andersson, their inventor.
|
|
43 |
See details in AATree
|
|
44 |
"
|
2779
|
45 |
! !
|
|
46 |
|
|
47 |
!AATreeNode class methodsFor:'instance creation'!
|
|
48 |
|
|
49 |
data:dataArg level:levelArg
|
|
50 |
^ self basicNew data:dataArg level:levelArg
|
|
51 |
! !
|
|
52 |
|
|
53 |
!AATreeNode methodsFor:'accessing'!
|
|
54 |
|
4266
|
55 |
data:dataArg level:levelIntegerArg
|
2779
|
56 |
data := dataArg.
|
4266
|
57 |
level := levelIntegerArg.
|
2779
|
58 |
!
|
|
59 |
|
|
60 |
level
|
|
61 |
^ level
|
|
62 |
!
|
|
63 |
|
4266
|
64 |
level:anInteger
|
|
65 |
level := anInteger.
|
2779
|
66 |
! !
|
|
67 |
|
|
68 |
!AATreeNode methodsFor:'helpers'!
|
|
69 |
|
|
70 |
decrease_level
|
|
71 |
"/ function decrease_level is
|
|
72 |
"/ input: T, a tree for which we want to remove links that skip levels.
|
|
73 |
"/ output: T with its level decreased.
|
|
74 |
"/
|
|
75 |
"/ should_be = min(level(left(T)), level(right(T))) + 1
|
|
76 |
"/ if should_be < level(T) then
|
|
77 |
"/ level(T) := should_be
|
|
78 |
"/ if should_be < level(right(T)) then
|
|
79 |
"/ level(right(T)) := should_be
|
|
80 |
"/ end if
|
|
81 |
"/ end if
|
|
82 |
"/ return T
|
|
83 |
"/ end function
|
|
84 |
|
|
85 |
|shouldBe leftLevel rightLevel|
|
|
86 |
|
|
87 |
leftLevel := leftSubtree isNil ifTrue:[0] ifFalse:[leftSubtree level].
|
|
88 |
rightLevel := rightSubtree isNil ifTrue:[0] ifFalse:[rightSubtree level].
|
|
89 |
shouldBe := (leftLevel min:rightLevel) + 1.
|
|
90 |
shouldBe < level ifTrue:[
|
|
91 |
level := shouldBe.
|
|
92 |
shouldBe < rightLevel ifTrue:[
|
|
93 |
rightSubtree level:shouldBe
|
|
94 |
].
|
|
95 |
].
|
|
96 |
!
|
|
97 |
|
|
98 |
insert:anObject usingSortBlock:sortBlock
|
|
99 |
"/ function insert is
|
|
100 |
"/ input: X, the value to be inserted, and T, the root of the tree to insert it into.
|
|
101 |
"/ output: A balanced version T including X.
|
|
102 |
"/
|
|
103 |
"/ Do the normal binary tree insertion procedure. Set the result of the
|
|
104 |
"/ recursive call to the correct child in case a new node was created or the
|
|
105 |
"/ root of the subtree changes.
|
|
106 |
"/ if nil(T) then
|
|
107 |
"/ Create a new leaf node with X.
|
|
108 |
"/ return node(X, 1, Nil, Nil)
|
|
109 |
"/ else if X < value(T) then
|
|
110 |
"/ left(T) := insert(X, left(T))
|
|
111 |
"/ else if X > value(T) then
|
|
112 |
"/ right(T) := insert(X, right(T))
|
|
113 |
"/ end if
|
|
114 |
"/ Note that the case of X == value(T) is unspecified. As given, an insert
|
|
115 |
"/ will have no effect. The implementor may desire different behavior.
|
|
116 |
"/
|
|
117 |
"/ Perform skew and then split. The conditionals that determine whether or
|
|
118 |
"/ not a rotation will occur or not are inside of the procedures, as given
|
|
119 |
"/ above.
|
|
120 |
"/ T := skew(T)
|
|
121 |
"/ T := split(T)
|
|
122 |
"/
|
|
123 |
"/ return T
|
|
124 |
"/ end function
|
|
125 |
|
|
126 |
(sortBlock value:anObject value:data) ifTrue:[
|
|
127 |
leftSubtree isNil ifTrue:[
|
|
128 |
leftSubtree := self class data:anObject level:1
|
|
129 |
] ifFalse:[
|
|
130 |
leftSubtree := (leftSubtree insert:anObject usingSortBlock:sortBlock)
|
|
131 |
].
|
|
132 |
] ifFalse:[
|
|
133 |
rightSubtree isNil ifTrue:[
|
|
134 |
rightSubtree := self class data:anObject level:1
|
|
135 |
] ifFalse:[
|
|
136 |
rightSubtree := (rightSubtree insert:anObject usingSortBlock:sortBlock)
|
|
137 |
]
|
|
138 |
].
|
|
139 |
|
|
140 |
^ self skew split.
|
|
141 |
|
|
142 |
"Modified: / 05-08-2012 / 11:40:23 / cg"
|
|
143 |
!
|
|
144 |
|
|
145 |
removeValue:oldValue using:equalSelector sortBlock:sortBlock
|
|
146 |
"remove a value - returns a new treeNode, or nil if the value is not in the tree"
|
|
147 |
|
|
148 |
|nextValue prevValue newTop rr thisIsMyNode newLeftSubtree newRightSubtree|
|
|
149 |
|
|
150 |
"/ function delete is
|
|
151 |
"/ input: X, the value to delete, and T, the root of the tree from which it should be deleted.
|
|
152 |
"/ output: T, balanced, without the value X.
|
|
153 |
"/
|
|
154 |
"/ if X > value(T) then
|
|
155 |
"/ right(T) := delete(X, right(T))
|
|
156 |
"/ else if X < value(T) then
|
|
157 |
"/ left(T) := delete(X, left(T))
|
|
158 |
"/ else
|
|
159 |
"/ If we're a leaf, easy, otherwise reduce to leaf case.
|
|
160 |
"/ if leaf(T) then
|
|
161 |
"/ return Nil
|
|
162 |
"/ else if nil(left(T)) then
|
|
163 |
"/ L := successor(T)
|
|
164 |
"/ right(T) := delete(L, right(T))
|
|
165 |
"/ value(T) := L
|
|
166 |
"/ else
|
|
167 |
"/ L := predecessor(T)
|
|
168 |
"/ left(T) := delete(L, left(T))
|
|
169 |
"/ value(T) := L
|
|
170 |
"/ end if
|
|
171 |
"/ end if
|
|
172 |
"/
|
|
173 |
"/ Rebalance the tree. Decrease the level of all nodes in this level if
|
|
174 |
"/ necessary, and then skew and split all nodes in the new level.
|
|
175 |
"/ T := decrease_level(T)
|
|
176 |
"/ T := skew(T)
|
|
177 |
"/ right(T) := skew(right(T))
|
|
178 |
"/ right(right(T)) := skew(right(right(T)))
|
|
179 |
"/ T := split(T)
|
|
180 |
"/ right(T) := split(right(T))
|
|
181 |
"/ return T
|
|
182 |
"/ end function
|
|
183 |
|
|
184 |
"/ speed hack - avoids message sends (and also better inline caching)
|
|
185 |
equalSelector == #== ifTrue:[
|
|
186 |
thisIsMyNode := (data == oldValue).
|
|
187 |
] ifFalse:[
|
|
188 |
equalSelector == #= ifTrue:[
|
|
189 |
thisIsMyNode := (data = oldValue).
|
|
190 |
] ifFalse:[
|
|
191 |
thisIsMyNode := data perform:equalSelector with:oldValue.
|
|
192 |
].
|
|
193 |
].
|
|
194 |
|
|
195 |
thisIsMyNode ifTrue:[
|
|
196 |
leftSubtree isNil ifTrue:[
|
|
197 |
rightSubtree isNil ifTrue:[
|
|
198 |
^ nil
|
|
199 |
].
|
|
200 |
nextValue := rightSubtree leftMostNode data.
|
|
201 |
rightSubtree := (rightSubtree removeValue:nextValue using:equalSelector sortBlock:sortBlock).
|
|
202 |
data := nextValue.
|
|
203 |
] ifFalse:[
|
|
204 |
prevValue := leftSubtree rightMostNode data.
|
|
205 |
leftSubtree := (leftSubtree removeValue:prevValue using:equalSelector sortBlock:sortBlock).
|
|
206 |
data := prevValue.
|
|
207 |
].
|
|
208 |
] ifFalse:[
|
|
209 |
(sortBlock value:oldValue value:data) ifTrue:[
|
|
210 |
"/ the value should be in the left part.
|
|
211 |
leftSubtree isNil ifTrue:[
|
|
212 |
^ nil.
|
|
213 |
].
|
|
214 |
newLeftSubtree := leftSubtree removeValue:oldValue using:equalSelector sortBlock:sortBlock.
|
|
215 |
newLeftSubtree isNil ifTrue:[
|
|
216 |
(leftSubtree data perform:equalSelector with:oldValue) ifFalse:[
|
|
217 |
^ nil
|
|
218 |
].
|
|
219 |
].
|
|
220 |
leftSubtree := newLeftSubtree.
|
|
221 |
] ifFalse:[
|
|
222 |
"/ the value should be in the right part.
|
|
223 |
rightSubtree isNil ifTrue:[
|
|
224 |
^ nil.
|
|
225 |
].
|
|
226 |
newRightSubtree := rightSubtree removeValue:oldValue using:equalSelector sortBlock:sortBlock.
|
|
227 |
newRightSubtree isNil ifTrue:[
|
|
228 |
(rightSubtree data perform:equalSelector with:oldValue) ifFalse:[
|
|
229 |
^ nil
|
|
230 |
].
|
|
231 |
].
|
|
232 |
rightSubtree := newRightSubtree.
|
|
233 |
].
|
|
234 |
].
|
|
235 |
|
|
236 |
self decrease_level.
|
|
237 |
newTop := self skew.
|
|
238 |
(rr := newTop rightSubtree) notNil ifTrue:[
|
|
239 |
newTop rightSubtree:(rr skew).
|
|
240 |
(rr := newTop rightSubtree rightSubtree) notNil ifTrue:[
|
|
241 |
newTop rightSubtree rightSubtree:(rr skew).
|
|
242 |
].
|
|
243 |
].
|
|
244 |
newTop := newTop split.
|
|
245 |
(rr := newTop rightSubtree) notNil ifTrue:[
|
|
246 |
newTop rightSubtree:(rr split).
|
|
247 |
].
|
|
248 |
^ newTop
|
|
249 |
!
|
|
250 |
|
|
251 |
skew
|
|
252 |
|oldLeft|
|
|
253 |
|
|
254 |
"/ function skew is
|
|
255 |
"/ input: T, a node representing an AA tree that needs to be rebalanced.
|
|
256 |
"/ output: Another node representing the rebalanced AA tree.
|
|
257 |
"/
|
|
258 |
"/ if nil(T) then
|
|
259 |
"/ return Nil
|
|
260 |
"/ else if level(left(T)) == level(T) then
|
|
261 |
"/ Swap the pointers of horizontal left links.
|
|
262 |
"/ L = left(T)
|
|
263 |
"/ left(T) := right(L)
|
|
264 |
"/ right(L) := T
|
|
265 |
"/ return L
|
|
266 |
"/ else
|
|
267 |
"/ return T
|
|
268 |
"/ end if
|
|
269 |
"/ end function
|
|
270 |
|
|
271 |
leftSubtree isNil ifTrue:[^ self].
|
|
272 |
leftSubtree level == level ifTrue:[
|
|
273 |
oldLeft := leftSubtree.
|
|
274 |
leftSubtree := (oldLeft rightSubtree).
|
|
275 |
oldLeft rightSubtree:self.
|
|
276 |
^ oldLeft
|
|
277 |
].
|
|
278 |
^ self
|
|
279 |
!
|
|
280 |
|
|
281 |
split
|
|
282 |
|oldRight oldRightRight|
|
|
283 |
|
|
284 |
"/ function split is
|
|
285 |
"/ input: T, a node representing an AA tree that needs to be rebalanced.
|
|
286 |
"/ output: Another node representing the rebalanced AA tree.
|
|
287 |
"/
|
|
288 |
"/ if nil(T) then
|
|
289 |
"/ return Nil
|
|
290 |
"/ else if level(T) == level(right(right(T))) then
|
|
291 |
"/ We have two horizontal right links. Take the middle node, elevate it, and return it.
|
|
292 |
"/ R = right(T)
|
|
293 |
"/ right(T) := left(R)
|
|
294 |
"/ left(R) := T
|
|
295 |
"/ level(R) := level(R) + 1
|
|
296 |
"/ return R
|
|
297 |
"/ else
|
|
298 |
"/ return T
|
|
299 |
"/ end if
|
|
300 |
"/ end function
|
|
301 |
|
|
302 |
rightSubtree isNil ifTrue:[^ self].
|
|
303 |
oldRightRight := rightSubtree rightSubtree.
|
|
304 |
oldRightRight isNil ifTrue:[^ self].
|
|
305 |
level == oldRightRight level ifTrue:[
|
|
306 |
oldRight := rightSubtree.
|
|
307 |
rightSubtree := oldRight leftSubtree.
|
|
308 |
oldRight leftSubtree:self.
|
|
309 |
oldRight level:(oldRight level + 1).
|
|
310 |
^ oldRight
|
|
311 |
].
|
|
312 |
^ self
|
|
313 |
! !
|
|
314 |
|
2782
|
315 |
!AATreeNode class methodsFor:'documentation'!
|
|
316 |
|
|
317 |
version
|
4266
|
318 |
^ '$Header$'
|
2782
|
319 |
!
|
|
320 |
|
|
321 |
version_CVS
|
4266
|
322 |
^ '$Header$'
|
2782
|
323 |
! !
|
3140
|
324 |
|