-
Notifications
You must be signed in to change notification settings - Fork 14
/
TreeSets.bas
331 lines (252 loc) · 7.32 KB
/
TreeSets.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'@desc Util Class TreeSets, java TreeSet API implemented with VBA
'@dependency Lists, Nodes
'@author Qiou Yang
'@license MIT
'@lastUpdate 18.02.2020
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Private pLen As Long
Private pRoot As Nodes
Private pCnt As Long
Private Sub Class_Initialize()
init
End Sub
Private Sub Class_Terminate()
clear
End Sub
Public Property Get root() As Nodes
Set root = pRoot
End Property
Public Function toString(Optional ByVal replaceDecimalPoint As Boolean = True) As String
If pLen = 0 Then
toString = "{ }"
Else
Dim i
Dim res As String
res = "{ "
For Each i In Me.toArray
If replaceDecimalPoint And IsNumeric(i) Then
res = res & Replace("" & i, ",", ".") & ", "
Else
res = res & i & ", "
End If
Next i
toString = Left(res, Len(res) - 2) & " }"
End If
End Function
Public Function p()
Debug.Print Me.toString
End Function
Public Function clear()
pLen = 0
pCnt = 0
Set pRoot = Nothing
End Function
Public Function ceiling(ByVal e, Optional ByVal asNode As Boolean = False) As Variant
If Me.Count = 0 Then
If asNode Then
Set ceiling = Nothing
Else
ceiling = Null
End If
Else
Dim res As New Nodes
res.init Nothing, Nothing, -1, 0
Call ceiling_(e, pRoot, res)
If res.index = -1 Then
If asNode Then
Set ceiling = Nothing
Else
ceiling = Null
End If
Else
If asNode Then
Set ceiling = res
Else
ceiling = res.value
End If
End If
End If
End Function
Public Function contains(ByVal e) As Boolean
Dim res As Boolean
res = False
If Not isNothing(Me.ceiling(e)) Then
res = Me.ceiling(e) = e
End If
contains = res
End Function
Public Function isEmpty() As Boolean
isEmpty = pLen = 0
End Function
Public Function size() As Long
size = pLen
End Function
Public Function subSet(ByVal fromElement, ByVal toElement) As TreeSets
Dim tree As New TreeSets
tree.init
tree.addAll toArray_(pRoot).filter("AND(_>=" & Replace("" & fromElement, ",", ".") & ", _<" & Replace("" & toElement, ",", ".") & ")")
Set subSet = tree
End Function
Public Function pollFirst() As Variant
Dim res
res = Me.first()
Me.Remove res
pollFirst = res
End Function
Public Function pollLast() As Variant
Dim res
res = Me.last()
Me.Remove res
pollLast = res
End Function
Public Function Remove(e As Variant) As Boolean
Dim parent As Nodes
Set parent = Nothing
Dim res As Boolean
res = False
Dim tmp As Long
Call remove_(e, pRoot, parent, res, tmp)
If res Then
pLen = pLen - 1
End If
Remove = res
End Function
Private Function remove_(ByVal e, ByRef n As Nodes, ByRef parent As Nodes, ByRef res As Boolean, ByRef tmp As Long)
If e = n.value Then
' appendNode will add one back
res = True
If Not parent Is Nothing Then
If parent.value > n.value Then
parent.leftNode = Nothing
Else
parent.RightNode = Nothing
End If
If Not n.leftNode Is Nothing Then
Call appendNode(n.leftNode, pRoot, tmp)
End If
If Not n.RightNode Is Nothing Then
Call appendNode(n.RightNode, pRoot, tmp)
End If
Else
'root element
If n.leftNode Is Nothing Then
Set pRoot = n.RightNode
ElseIf n.RightNode Is Nothing Then
Set pRoot = n.leftNode
Else
Call appendNode(n.leftNode, n.RightNode, tmp)
Set pRoot = n.RightNode
End If
End If
ElseIf e > n.value Then
If Not n.RightNode Is Nothing Then
Call remove_(e, n.RightNode, n, res, tmp)
End If
Else
If Not n.leftNode Is Nothing Then
Call remove_(e, n.leftNode, n, res, tmp)
End If
End If
End Function
Private Function ceiling_(ByVal e, ByRef n As Nodes, ByRef res As Nodes)
If e > n.value Then
If Not n.RightNode Is Nothing Then
Call ceiling_(e, n.RightNode, res)
End If
ElseIf e = n.value Then
Set res = n
Else
If Not n.leftNode Is Nothing Then
Set res = n
Call ceiling_(e, n.leftNode, res)
Else
Set res = n
End If
End If
End Function
Public Property Get length() As Long
length = pLen
End Property
Public Property Get Count() As Long
Count = pCnt
End Property
Public Function init()
pLen = 0
pCnt = 0
End Function
Public Function add(val As Variant, Optional ByVal updateIfDuplicated As Boolean = False)
Dim n As New Nodes
n.init Nothing, Nothing, pCnt, val
If pLen > 0 Then
Call appendNode(n, pRoot, pLen, updateIfDuplicated)
Else
Set pRoot = n
pLen = 1
pCnt = 0
End If
Set n = Nothing
pCnt = pCnt + 1
End Function
Private Function appendNode(ByRef n As Nodes, ByRef root As Nodes, ByRef l As Long, Optional ByVal updateIfDuplicated As Boolean = False)
Dim targVal, rootVal
targVal = n.value
rootVal = root.value
If targVal > rootVal Then
If Not root.RightNode Is Nothing Then
Call appendNode(n, root.RightNode, l, updateIfDuplicated)
Else
root.RightNode = n
l = l + 1
End If
ElseIf targVal < rootVal Then
If Not root.leftNode Is Nothing Then
Call appendNode(n, root.leftNode, l, updateIfDuplicated)
Else
root.leftNode = n
l = l + 1
End If
Else
If updateIfDuplicated Then
root.index = n.index
End If
End If
End Function
Public Function addAll(ParamArray val() As Variant)
Dim arr
Dim i
For Each i In val
Me.add i
Next i
End Function
Public Function first() As Variant
Dim tmp As New Nodes
Set tmp = Me.root
Do While Not tmp.leftNode Is Nothing
Set tmp = tmp.leftNode
Loop
first = tmp.value
End Function
Public Function last() As Variant
Dim tmp As New Nodes
Set tmp = Me.root
Do While Not tmp.RightNode Is Nothing
Set tmp = tmp.RightNode
Loop
last = tmp.value
End Function
Private Function isNothing(e) As Boolean
isNothing = TypeName(e) = "Nothing"
End Function
Public Function toArray()
toArray = toArray_(Me.root).toArray
End Function
Private Function toArray_(ByRef n As Nodes) As Lists
Dim l As New Lists
l.init
If Not n Is Nothing Then
l.addAll(toArray_(n.leftNode)).add(n.value).addAll toArray_(n.RightNode)
End If
Set toArray_ = l
End Function