-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllrb.vbs
More file actions
542 lines (493 loc) · 16.1 KB
/
llrb.vbs
File metadata and controls
542 lines (493 loc) · 16.1 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
'
' llrb.vbs - A left leaning 2-3 red black tree implementation.
'
option explicit
const red = true
const black = false
sub main()
includefile "queue.vbs" ' tqueue and tstack
dim i, arr : arr = array(5,3,4,7,1,9,11,13,15,2)
' dim i, arr : arr = array(3,5)
dim tree : set tree = new trbtree
randomize timer
for i=1 to 10
' for i = 0 to ubound(arr)
' tree.rbput(arr(i))
tree.rbput(cint(rnd*100))
' tree.print
if not tree.isrbtree then
exit for
end if
next
tree.print
tree.printbtree
tree.isrbtree
wscript.stdout.write "Press enter to continue..."
wscript.stdin.readline
' wscript.echo tree.rbget(1).m_key
' wscript.stdout.write "Press enter to continue..."
' wscript.stdin.readline
' tree.printbtree
' tree.isrbtree
do until tree.isempty or not tree.isrbtree
tree.rbdeletemin
' tree.rbdelete(tree.m_root.m_key)
tree.printbtree
loop
end sub
sub includefile(fspec)
executeglobal createobject("scripting.filesystemobject").opentextfile(fspec).readall()
end sub
class trbnode
public m_key
public m_left
public m_right
public m_color
public function init(byval key)
m_key = key
set m_left = nothing
set m_right = nothing
m_color = red
set init = me
end function
end class
class trbtree
public m_root
public m_cnt
private sub class_initialize()
set m_root = nothing
m_cnt = 0
end sub
private function iif(byval condition, byval a, byval b)
if condition then
iif = a
else
iif = b
end if
end function
private function isless(byval a, byval b)
if a is nothing or b is nothing then
isless = false
else
isless = (a.m_key < b.m_key)
end if
end function
private function max(a, b)
if a > b then
max = a
else
max = b
end if
end function
private function isred(byval node)
if node is nothing then
isred = false
else
isred = (node.m_color = red)
end if
end function
private function color2char(byval node)
if node is nothing then
color2char = "b"
elseif isred(node) then
color2char = "r"
else
color2char = "b"
end if
end function
public function isempty
isempty = (m_root is nothing)
end function
public function isrbtree()
isrbtree = (isrbnode(m_root) <> -1)
end function
private function isrbnode(byval node)
if node is nothing then
isrbnode = 0
elseif node is m_root and isred(node) then
wscript.echo "ERROR: root is red", node.m_key
isrbnode = -1
elseif isless(node, node.m_left) or isless(node.m_right, node) then
wscript.echo "ERROR: not a BST", node.m_key
isrbnode = -1
elseif isred(node.m_right) then
wscript.echo "ERROR: right child is red", node.m_key
isrbnode = -1
elseif isred(node) and isred(node.m_left) then
wscript.echo "ERROR: two red nodes in a row", node.m_key
isrbnode = -1
else
dim lh, rh
lh = isrbnode(node.m_left)
rh = isrbnode(node.m_right)
if lh = -1 or rh = -1 then
isrbnode = -1
elseif lh <> rh then
wscript.echo "ERROR: black heights don't match", node.m_key
isrbnode = -1
elseif isred(node) then
isrbnode = lh
else ' black node
isrbnode = lh+1
end if
end if
end function
public sub print()
dim queue : set queue = new tqueue
if m_root is nothing then
exit sub
end if
queue.enqueue m_root
dim lcnt : lcnt = queue.length
do until queue.isempty
dim node : set node = queue.dequeue
wscript.stdout.write node.m_key & "," & color2char(node) & " "
if not node.m_left is nothing then
queue.enqueue node.m_left ' left child
end if
if not node.m_right is nothing then
queue.enqueue node.m_right ' right child
end if
lcnt = lcnt-1
if lcnt = 0 then ' all done with current level
wscript.stdout.writeline
lcnt = queue.length ' set level count to next level size
end if
loop
wscript.stdout.writeline
end sub
public sub printbtree()
dim queue : set queue = new tqueue
if m_root is nothing then
exit sub
end if
queue.enqueue m_root
do until queue.isempty
dim lcnt : lcnt = queue.length
do while lcnt > 0
dim node : set node = queue.dequeue
if isred(node.m_left) then ' red node
dim lc : set lc = node.m_left
wscript.stdout.write lc.m_key
if not lc.m_left is nothing then
queue.enqueue lc.m_left ' left left child
end if
if not lc.m_right is nothing then
queue.enqueue lc.m_right ' left right child
end if
else ' black node
wscript.stdout.write "*"
if not node.m_left is nothing then
queue.enqueue node.m_left ' left child
end if
end if
wscript.stdout.write "," & node.m_key
if isred(node.m_right) then ' red node
dim rc : set rc = node.m_right
wscript.stdout.write "," & rc.m_key
if not rc.m_left is nothing then
queue.enqueue rc.m_left ' right left child
end if
if not rc.m_right is nothing then
queue.enqueue rc.m_right ' right right child
end if
else ' black node
wscript.stdout.write ",*"
if not node.m_right is nothing then
queue.enqueue node.m_right ' right child
end if
end if
wscript.stdout.write " "
lcnt = lcnt-1
loop
wscript.stdout.writeline
loop
wscript.stdout.writeline
end sub
public function rbget(byval key)
set rbget = rbgetn(m_root, key)
end function
public function rbgetn(byval node, byval key)
if node is nothing then
set node = nothing
else
if key < node.m_key then
set node = rbgetn(node.m_left, key)
elseif key > node.m_key then
set node = rbgetn(node.m_right, key)
else
set node = node
end if
end if
set rbgetn = node
end function
public function rotateleft(byval node)
wscript.echo "Rotate Left.", node.m_key
dim root : set root = node.m_right
set node.m_right = root.m_left
set root.m_left = node
root.m_color = node.m_color
node.m_color = red
set rotateleft = root
end function
public function rotateright(byval node)
wscript.echo "Rotate Right.", node.m_key
dim root : set root = node.m_left
set node.m_left = root.m_right
set root.m_right = node
root.m_color = node.m_color
node.m_color = red
set rotateright = root
end function
private sub colorflip(byval node)
wscript.echo "Color Flip", node.m_key
node.m_color = not node.m_color
node.m_left.m_color = not node.m_color
node.m_right.m_color = not node.m_color
end sub
public function rbbalance(byval node)
wscript.echo "Balance.", node.m_key
if isred(node.m_right) then
set node = rotateleft(node)
end if
if isred(node.m_left) then
if isred(node.m_left.m_left) then
set node = rotateright(node)
end if
end if
if isred(node.m_left) and isred(node.m_right) then
colorflip node
end if
set rbbalance = node
end function
public function rbput(byval key)
wscript.echo "** Inserting => ", key
set m_root = rbputn(m_root, key)
m_root.m_color = black
set rbput = m_root
end function
public function rbputn(byval node, byval key)
if node is nothing then
set node = (new trbnode).init(key)
m_cnt = m_cnt+1
else
if key < node.m_key then
set node.m_left = rbputn(node.m_left, key)
elseif key > node.m_key then
set node.m_right = rbputn(node.m_right, key)
else
node.m_key = key
end if
if isred(node.m_right) and not isred(node.m_left) then
set node = rotateleft(node)
end if
if isred(node.m_left) then
if isred(node.m_left.m_left) then
set node = rotateright(node)
end if
end if
if isred(node.m_left) and isred(node.m_right) then
colorflip node
end if
end if
set rbputn = node
end function
public function succ(byval node)
set node = node.m_right
do until node.m_left is nothing
set node = node.m_left
loop
set succ = node
end function
public function moveredleft(byval node)
' assert: not node is nothing
' assert: isred(node) and not isred(node.m_left)
wscript.echo "Move Red Left.", node.m_key
colorflip node
if isred(node.m_right.m_left) then
set node.m_right = rotateright(node.m_right)
set node = rotateleft(node)
colorflip node
end if
set moveredleft = node
end function
public function moveredright(byval node)
' assert: not node is nothing
' assert: isred(node) and not isred(node.m_right) and not isred(node.m_right.m_left)
wscript.echo "Move Red Right.", node.m_key
colorflip node
if isred(node.m_left.m_left) then
set node = rotateright(node)
colorflip node
end if
set moveredright = node
end function
public function rbdeletemax()
wscript.echo "** Deleting max."
if not isempty then
if not isred(m_root.m_left) and not isred(m_root.m_right) then
m_root.m_color = red
end if
set m_root = rbdeletemaxn(m_root)
if not isempty then
m_root.m_color = black
end if
end if
set rbdeletemax = nothing
end function
public function rbdeletemaxn(byval node)
if isred(node.m_left) then
set node = rotateright(node)
end if
if node.m_right is nothing then
m_cnt = m_cnt-1
set node = nothing
else
if not isred(node.m_right) and not isred(node.m_right.m_left) then
set node = moveredright(node)
end if
set node.m_right = rbdeletemaxn(node.m_right)
end if
if not node is nothing then
set node = rbbalance(node)
end if
set rbdeletemaxn = node
end function
public function rbdeletemin()
wscript.echo "** Deleting min."
if not isempty then
if not isred(m_root.m_left) and not isred(m_root.m_right) then
m_root.m_color = red
end if
set m_root = rbdeleteminn(m_root)
if not isempty then
m_root.m_color = black
end if
end if
set rbdeletemin = nothing
end function
public function rbdeleteminn(byval node)
if node.m_left is nothing then
m_cnt = m_cnt-1
set node = nothing
else
if not isred(node.m_left) and not isred(node.m_left.m_left) then
set node = moveredleft(node)
end if
set node.m_left = rbdeleteminn(node.m_left)
end if
if not node is nothing then
set node = rbbalance(node)
end if
set rbdeleteminn = node
end function
public function rbdelete(byval key)
wscript.echo "** Deleting => ", key
if not rbget(key) is nothing then
if not isred(m_root.m_left) and not isred(m_root.m_right) then
m_root.m_color = red
end if
set m_root = rbdeleten(m_root, key)
if not isempty then
m_root.m_color = black
end if
end if
set rbdelete = nothing
end function
public function rbdeleten(byval node, byval key)
if key < node.m_key then
if not isred(node.m_left) and not isred(node.m_left.m_left) then
set node = moveredleft(node)
end if
set node.m_left = rbdeleten(node.m_left, key)
else
if isred(node.m_left) then
set node = rotateright(node)
end if
if key = node.m_key and node.m_right is nothing then
m_cnt = m_cnt-1
set node = nothing
else
if not isred(node.m_right) and not isred(node.m_right.m_left) then
set node = moveredright(node)
end if
if (key = node.m_key) then
dim s : set s = succ(node)
node.m_key = s.m_key
set node.m_right = rbdeleteminn(node.m_right)
else
set node.m_right = rbdeleten(node.m_right, key)
end if
end if
end if
if not node is nothing then
set node = rbbalance(node)
end if
set rbdeleten = node
end function
public sub preorderr()
preorderrnode(m_root)
wscript.stdout.writeline
end sub
private sub preorderrnode(byval node)
if not node is nothing then
wscript.stdout.write node.m_key & " "
preorderrnode(node.m_left)
preorderrnode(node.m_right)
end if
end sub
public sub inorderr()
inorderrnode(m_root)
wscript.stdout.writeline
end sub
private sub inorderrnode(byval node)
if not node is nothing then
inorderrnode(node.m_left)
wscript.stdout.write node.m_key & " "
inorderrnode(node.m_right)
end if
end sub
public sub postorderr()
postorderrnode(m_root)
wscript.stdout.writeline
end sub
private sub postorderrnode(byval node)
if not node is nothing then
postorderrnode(node.m_left)
postorderrnode(node.m_right)
wscript.stdout.write node.m_key & " "
end if
end sub
public sub levelorderi()
dim queue : set queue = new tqueue
if m_root is nothing then
exit sub
end if
queue.enqueue m_root
do until queue.isempty
dim node : set node = queue.dequeue
wscript.stdout.write node.m_key & " "
if not node.m_left is nothing then
queue.enqueue node.m_left
end if
if not node.m_right is nothing then
queue.enqueue node.m_right
end if
loop
wscript.stdout.writeline
end sub
public function heightr()
heightr = heightrnode(m_root)
end function
private function heightrnode(node)
if node is nothing then
heightrnode = 0
else
dim lh, rh
lh = heightrnode(node.m_left)
rh = heightrnode(node.m_right)
heightrnode = 1 + max(lh, rh)
end if
end function
end class
call main()