-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.vbs
More file actions
256 lines (234 loc) · 7.72 KB
/
test2.vbs
File metadata and controls
256 lines (234 loc) · 7.72 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
option explicit
sub main()
dim tree : set tree = new tbst
set tree.m_root = (new tbstnode).init(5)
set tree.m_root.m_child(0) = (new tbstnode).init(3)
set tree.m_root.m_child(0).m_child(0) = (new tbstnode).init(1)
set tree.m_root.m_child(0).m_child(0).m_child(1) = (new tbstnode).init(2)
set tree.m_root.m_child(1) = (new tbstnode).init(7)
tree.traverse tree.m_root
wscript.echo tree.isbst(tree.m_root)
if tree.bstfindr(7) is nothing then
wscript.echo "not found"
else
wscript.echo "found"
end if
if tree.bstfindr(2) is nothing then
wscript.echo "not found"
else
wscript.echo "found"
end if
if tree.bstfindr(4) is nothing then
wscript.echo "not found"
else
wscript.echo "found"
end if
if tree.bstfindi(2) is nothing then
wscript.echo "not found"
else
wscript.echo "found"
end if
set tree = nothing
set tree = new tbst
tree.bstputr(5)
tree.bstputr(4)
tree.bstputr(1)
tree.bstputr(2)
tree.bstputr(7)
tree.bstputr(7)
tree.traverse(tree.m_root)
tree.bstdeleter(2)
tree.traverse(tree.m_root)
tree.bstdeleter(5)
tree.traverse(tree.m_root)
wscript.echo tree.isbst(tree.m_root)
set tree = nothing
set tree = new tbst
tree.bstputi(5)
tree.bstputi(4)
tree.bstputi(1)
tree.bstputi(2)
tree.bstputi(7)
tree.traverse(tree.m_root)
wscript.echo tree.isbst(tree.m_root)
tree.bstdeletei(1)
tree.traverse(tree.m_root)
tree.bstdeletei(5)
tree.traverse(tree.m_root)
wscript.echo tree.isbst(tree.m_root)
end sub
class tbstnode
public m_data
public m_child(1)
public function init(byval data)
m_data = data
set m_child(0) = nothing
set m_child(1) = nothing
set init = me
end function
end class
class tbst
public m_root
public m_cnt
private sub class_initialize()
set m_root = nothing
m_cnt = 0
end sub
public sub traverse(byval node)
dim queue(25), qhead, qtail, qcnt : qhead = 0 : qtail = -1 : qcnt = 0
qtail = (qtail+1) mod 25 : set queue(qtail) = node : qcnt = qcnt+1 ' enqueue
do until qcnt = 0 or node is nothing
set node = queue(qhead) : qhead = (qhead+1) mod 25 : qcnt = qcnt-1 ' dequeue
wscript.stdout.write node.m_data & " "
if not node.m_child(0) is nothing then
qtail = (qtail+1) mod 25 : set queue(qtail) = node.m_child(0) : qcnt = qcnt+1 ' enqueue
end if
if not node.m_child(1) is nothing then
qtail = (qtail+1) mod 25 : set queue(qtail) = node.m_child(1) : qcnt = qcnt+1 ' enqueue
end if
loop
wscript.stdout.writeline
end sub
private function isless(a, b)
if a is nothing or b is nothing then
isless = false
else
isless = (a.m_data < b.m_data)
end if
end function
public function isbst(node)
if node is nothing then
isbst = true
elseif isless(node, node.m_child(0)) or isless(node.m_child(1), node) then
wscript.echo "ERROR: not a BST"
isbst = false
else
isbst = true
end if
end function
public function bstfindr(data)
set bstfindr = bstfindnoder(m_root, data)
end function
private function bstfindnoder(byval node, byval data)
if node is nothing then
set bstfindnoder = node
elseif node.m_data = data then
set bstfindnoder = node
else
dim way : way = (node.m_data < data) and 1
set bstfindnoder = bstfindnoder(node.m_child(way), data)
end if
end function
public function bstfindi(data)
dim node : set node = m_root
do until node is nothing
if node.m_data = data then
exit do
end if
dim way : way = (node.m_data < data) and 1
set node = node.m_child(way)
loop
set bstfindi = node
end function
public function bstputr(byval data)
set m_root = bstputitemr(m_root, data)
set bstputr = m_root
end function
private function bstputitemr(byval node, byval data)
if node is nothing then
set node = (new tbstnode).init(data)
set bstputitemr = node
elseif node.m_data = data then
set bstputitemr = node
else
dim way : way = (node.m_data < data) and 1
set node.m_child(way) = bstputitemr(node.m_child(way), data)
set bstputitemr = node
end if
end function
public function bstputi(byval data)
dim node, parent, way : set node = m_root : set parent = nothing
do until node is nothing
if node.m_data = data then
set bstputi = node
exit function
end if
set parent = node
way = (node.m_data < data) and 1
set node = node.m_child(way)
loop
set node = (new tbstnode).init(data)
if parent is nothing then
set m_root = node
else
set parent.m_child(way) = node
end if
set bstputi = node
end function
public function bstdeleter(byval data)
set m_root = bstdeleteitemr(m_root, data)
set bstdeleter = m_root
end function
private function bstdeleteitemr(byval node, byval data)
if node is nothing then
set bstdeleteitemr = node
elseif node.m_data <> data then
dim way : way = (node.m_data < data) and 1
set node.m_child(way) = bstdeleteitemr(node.m_child(way), data)
set bstdeleteitemr = node
else ' found it
if node.m_child(0) is nothing then ' one child
set bstdeleteitemr = node.m_child(1)
elseif node.m_child(1) is nothing then ' one child
set bstdeleteitemr = node.m_child(0)
else ' 2 children
dim succ : set succ = node.m_child(1)
do until succ.m_child(0) is nothing
set succ = succ.m_next(0)
loop
node.m_data = succ.m_data
set node.m_child(1) = bstdeleteitemr(node.m_child(1), succ.m_data)
set bstdeleteitemr = node
end if
end if
end function
public function bstdeletei(byval data)
dim node, parent, q, way : set node = m_root : set parent = nothing
do until node is nothing
if node.m_data <> data then
set parent = node
way = (node.m_data < data) and 1
set node = node.m_child(way)
else
if node.m_child(0) is nothing then
set q = node.m_child(1)
exit do
elseif node.m_child(1) is nothing then
set q = node.m_child(0)
exit do
else
dim succ : set succ = node.m_child(1)
do until succ.m_child(0) is nothing
set succ = succ.m_next(0)
loop
node.m_data = succ.m_data
data = succ.m_data
set parent = node
way = 1 ' right child
set node = node.m_child(way)
end if
end if
loop
if node is nothing then ' not found
set bstdeletei = node
exit function
end if
if parent is nothing then
set m_root = q
else
set parent.m_child(way) = q
end if
set bstdeletei = node
end function
end class
call main()