Discussion:
vbscript binary tree - help needed
(too old to reply)
ljb
2005-01-25 13:37:35 UTC
Permalink
I tried converting the Binsry Tree sample VBA from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbadev/html/implementingbinarytree.asp
to vbscript and am stumped. Does anyone have time to find what I did wrong?
I get "runtime error: Object required" immediatly on "If ti Is Nothing Then"
in the AddNode function.

thanks
LJB

'-----------------------------------------
Class TreeItem
Public Value
Public LeftChild
Public RightChild
End Class

Class Tree

Private tiHead

'done to reduce program stack space
Private mfAddDups
Private mvarItemToAdd

Private Sub Class_Initialize()
Set tiHead = New TreeItem
End Sub

Public Sub Add(varNewItem)
mfAddDups = True
mvarItemToAdd = varNewItem
AddNode tiHead
End Sub

Public Function AddNode(ti)
If ti Is Nothing Then
Set ti = New TreeItem
ti.Value = mvarItemToAdd
Else
If mvarItemToAdd < ti.Value Then
Set ti.LeftChild = AddNode(ti.LeftChild)
ElseIf mvarItemToAdd > ti.Value Then
Set ti.LeftChild = AddNode(ti.RightChild)
Else
If mfAddDups Then
Set ti.RightChild = AddNode(ti.RightChild)
End If
End If
End If

Set AddNode = ti
End Function

Public Sub WalkPostOrder()
PostOrder tiHead
End Sub

Private Sub PostOrder(ti)
If Not ti Is Nothing Then
InOrder ti.LeftChild
InOrder ti.RightChild
wscript.echo ti.Value
End If
End Sub

End Class


Dim MyTree
Set MyTree = New Tree

With MyTree

.Add 11
.Add 12
.Add 14
.Add 15
.Add 13

.WalkPostOrder

End With
Tom Lavedas
2005-01-25 14:57:01 UTC
Permalink
It would seem to me that the problem is that scripting can not type the
variable 'ti' in the function definition, so it cannot be tested in the way
the example for VB is doing it. Rather, consider testing for whether it is
already an object or not ...

If Not IsObject(ti) Then
...

Need to change the test in PostOrder function as well.

Tom Lavedas
===========
Post by ljb
I tried converting the Binsry Tree sample VBA from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbadev/html/implementingbinarytree.asp
to vbscript and am stumped. Does anyone have time to find what I did wrong?
I get "runtime error: Object required" immediatly on "If ti Is Nothing Then"
in the AddNode function.
thanks
LJB
'-----------------------------------------
Class TreeItem
Public Value
Public LeftChild
Public RightChild
End Class
Class Tree
Private tiHead
'done to reduce program stack space
Private mfAddDups
Private mvarItemToAdd
Private Sub Class_Initialize()
Set tiHead = New TreeItem
End Sub
Public Sub Add(varNewItem)
mfAddDups = True
mvarItemToAdd = varNewItem
AddNode tiHead
End Sub
Public Function AddNode(ti)
If ti Is Nothing Then
Set ti = New TreeItem
ti.Value = mvarItemToAdd
Else
If mvarItemToAdd < ti.Value Then
Set ti.LeftChild = AddNode(ti.LeftChild)
ElseIf mvarItemToAdd > ti.Value Then
Set ti.LeftChild = AddNode(ti.RightChild)
Else
If mfAddDups Then
Set ti.RightChild = AddNode(ti.RightChild)
End If
End If
End If
Set AddNode = ti
End Function
Public Sub WalkPostOrder()
PostOrder tiHead
End Sub
Private Sub PostOrder(ti)
If Not ti Is Nothing Then
InOrder ti.LeftChild
InOrder ti.RightChild
wscript.echo ti.Value
End If
End Sub
End Class
Dim MyTree
Set MyTree = New Tree
With MyTree
.Add 11
.Add 12
.Add 14
.Add 15
.Add 13
.WalkPostOrder
End With
ljb
2005-01-25 15:36:08 UTC
Permalink
That seems to be the problem.

I've found other problems too and am working on them.

thanks
Post by Tom Lavedas
It would seem to me that the problem is that scripting can not type the
variable 'ti' in the function definition, so it cannot be tested in the way
the example for VB is doing it. Rather, consider testing for whether it is
already an object or not ...
If Not IsObject(ti) Then
...
Need to change the test in PostOrder function as well.
Tom Lavedas
===========
Post by ljb
I tried converting the Binsry Tree sample VBA from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbadev/html/implementingbinarytree.asp
Post by Tom Lavedas
Post by ljb
to vbscript and am stumped. Does anyone have time to find what I did wrong?
I get "runtime error: Object required" immediatly on "If ti Is Nothing Then"
in the AddNode function.
thanks
LJB
'-----------------------------------------
Class TreeItem
Public Value
Public LeftChild
Public RightChild
End Class
Class Tree
Private tiHead
'done to reduce program stack space
Private mfAddDups
Private mvarItemToAdd
Private Sub Class_Initialize()
Set tiHead = New TreeItem
End Sub
Public Sub Add(varNewItem)
mfAddDups = True
mvarItemToAdd = varNewItem
AddNode tiHead
End Sub
Public Function AddNode(ti)
If ti Is Nothing Then
Set ti = New TreeItem
ti.Value = mvarItemToAdd
Else
If mvarItemToAdd < ti.Value Then
Set ti.LeftChild = AddNode(ti.LeftChild)
ElseIf mvarItemToAdd > ti.Value Then
Set ti.LeftChild = AddNode(ti.RightChild)
Else
If mfAddDups Then
Set ti.RightChild = AddNode(ti.RightChild)
End If
End If
End If
Set AddNode = ti
End Function
Public Sub WalkPostOrder()
PostOrder tiHead
End Sub
Private Sub PostOrder(ti)
If Not ti Is Nothing Then
InOrder ti.LeftChild
InOrder ti.RightChild
wscript.echo ti.Value
End If
End Sub
End Class
Dim MyTree
Set MyTree = New Tree
With MyTree
.Add 11
.Add 12
.Add 14
.Add 15
.Add 13
.WalkPostOrder
End With
Michael Harris (MVP)
2005-01-26 01:48:30 UTC
Permalink
Post by ljb
I tried converting the Binsry Tree sample VBA from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbadev/html/implementingbinarytree.asp
to vbscript and am stumped. Does anyone have time to find what I did
wrong? I get "runtime error: Object required" immediatly on "If ti Is
Nothing Then" in the AddNode function.
Here's a complete port of the sample VBA to VBScript...

1) All 'As Whatever' clause removed

2) Appropriate Class_Initialize added to both classes

3) All Debug.Print statements changed to equivalent WScript.Echo

4) Fixed bad definition of AddNode

a) Incorrectly ended with End Sub

===> changed to End Function

b) AddNode never returned a value (i.e, the ti object argument passed to
it)

===> added: Set AddNode = ti

I also scrambled the order in which values were added in the test code so
that the difference between the WalkPost/In/PreOrder was more obvious.

Dim MyTree
Set MyTree = New Tree

With MyTree

.Add 14
.Add 11
.Add 15
.Add 12
.Add 13
WScript.Echo "WalkPostOrder"
.WalkPostOrder
WScript.Echo "WalkInOrder"
.WalkInOrder
WScript.Echo "WalkPreOrder"
.WalkPreOrder

End With

'-----------------------------------------
Class TreeItem
Public Value
Public LeftChild
Public RightChild
'-----------------------------------------
Private Sub Class_Initialize()
Set LeftChild = Nothing
Set RightChild = Nothing
End Sub
End Class

'-----------------------------------------
Class Tree

Private tiHead

'done to reduce program stack space
Private mfAddDups
Private mvarItemToAdd

'-----------------------------------------
Private Sub Class_Initialize()
Set tiHead = Nothing
End Sub

'-----------------------------------------
Public Sub Add(varNewItem)
' Add a new node, allowing duplicates.
' Use module variables to place as little as
' possible on the stack in recursive procedure calls.
mfAddDupes = True
mvarItemToAdd = varNewItem
Call AddNode(tiHead)
End Sub
'-----------------------------------------
Public Sub AddUnique(varNewItem)
' Add a new node, skipping duplicate values.
' Use module variables to place as little as
' possible on the stack in recursive procedure calls.
mfAddDupes = False
mvarItemToAdd = varNewItem
Call AddNode(tiHead)
End Sub
'-----------------------------------------
Private Function AddNode(ti)
' Add a node to the tree pointed to by ti.
' Module variables used:
' mvarItemToAdd: the value to add to the tree.
' mfAddDupes: Boolean indicating whether to add items
' that already exist or to skip them.
If ti Is Nothing Then
Set ti = New TreeItem
ti.Value = mvarItemToAdd
Else
If mvarItemToAdd < ti.Value Then
Set ti.LeftChild = AddNode(ti.LeftChild)
ElseIf mvarItemToAdd > ti.Value Then
Set ti.RightChild = AddNode(ti.RightChild)
Else
' mvarItemToAdd = ti.Value
' You're adding a node that already exists.
' You could add it to the left or to the right,
' but this code arbitrarily adds it to the right.
If mfAddDupes Then
Set ti.RightChild = AddNode(ti.RightChild)
End If
End If
End If
Set AddNode = ti
End Function
'-----------------------------------------
Public Sub WalkInOrder()
Call InOrder(tiHead)
End Sub
'-----------------------------------------
Public Sub WalkPreOrder()
Call PreOrder(tiHead)
End Sub
'-----------------------------------------
Public Sub WalkPostOrder()
Call PostOrder(tiHead)
End Sub
'-----------------------------------------
Private Sub InOrder(ti)
If Not ti Is Nothing Then
Call InOrder(ti.LeftChild)
WScript.Echo ti.Value & " "
Call InOrder(ti.RightChild)
End If
End Sub
'-----------------------------------------
Private Sub PreOrder(ti)
If Not ti Is Nothing Then
WScript.Echo ti.Value & " "
Call PreOrder(ti.LeftChild)
Call PreOrder(ti.RightChild)
End If
End Sub
'-----------------------------------------
Private Sub PostOrder(ti)
If Not ti Is Nothing Then
Call PostOrder(ti.LeftChild)
Call PostOrder(ti.RightChild)
WScript.Echo ti.Value & " "
End If
End Sub

End Class
--
Michael Harris
Microsoft MVP Scripting
ljb
2005-01-26 13:52:06 UTC
Permalink
Thank You, Michael!

I don't have a particular need for the binary tree, queue, stack and linked
list presented in the VBA book sample chapter. I looked at them only as an
exercise. And now its been proven you can do the above in VBScript.

thanks
LJB

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbadev/html/chapter6creatingdynamicdatastructuresusingclassmodules.asp
or
http://www.developershandbook.com/Downloads/1951c06.pdf
Post by Michael Harris (MVP)
Post by ljb
I tried converting the Binsry Tree sample VBA from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbadev/html/implementingbinarytree.asp
Post by Michael Harris (MVP)
Post by ljb
to vbscript and am stumped. Does anyone have time to find what I did
wrong? I get "runtime error: Object required" immediatly on "If ti Is
Nothing Then" in the AddNode function.
Here's a complete port of the sample VBA to VBScript...
1) All 'As Whatever' clause removed
2) Appropriate Class_Initialize added to both classes
3) All Debug.Print statements changed to equivalent WScript.Echo
4) Fixed bad definition of AddNode
a) Incorrectly ended with End Sub
===> changed to End Function
b) AddNode never returned a value (i.e, the ti object argument passed to
it)
===> added: Set AddNode = ti
I also scrambled the order in which values were added in the test code so
that the difference between the WalkPost/In/PreOrder was more obvious.
Dim MyTree
Set MyTree = New Tree
With MyTree
.Add 14
.Add 11
.Add 15
.Add 12
.Add 13
WScript.Echo "WalkPostOrder"
.WalkPostOrder
WScript.Echo "WalkInOrder"
.WalkInOrder
WScript.Echo "WalkPreOrder"
.WalkPreOrder
End With
'-----------------------------------------
Class TreeItem
Public Value
Public LeftChild
Public RightChild
'-----------------------------------------
Private Sub Class_Initialize()
Set LeftChild = Nothing
Set RightChild = Nothing
End Sub
End Class
'-----------------------------------------
Class Tree
Private tiHead
'done to reduce program stack space
Private mfAddDups
Private mvarItemToAdd
'-----------------------------------------
Private Sub Class_Initialize()
Set tiHead = Nothing
End Sub
'-----------------------------------------
Public Sub Add(varNewItem)
' Add a new node, allowing duplicates.
' Use module variables to place as little as
' possible on the stack in recursive procedure calls.
mfAddDupes = True
mvarItemToAdd = varNewItem
Call AddNode(tiHead)
End Sub
'-----------------------------------------
Public Sub AddUnique(varNewItem)
' Add a new node, skipping duplicate values.
' Use module variables to place as little as
' possible on the stack in recursive procedure calls.
mfAddDupes = False
mvarItemToAdd = varNewItem
Call AddNode(tiHead)
End Sub
'-----------------------------------------
Private Function AddNode(ti)
' Add a node to the tree pointed to by ti.
' mvarItemToAdd: the value to add to the tree.
' mfAddDupes: Boolean indicating whether to add items
' that already exist or to skip them.
If ti Is Nothing Then
Set ti = New TreeItem
ti.Value = mvarItemToAdd
Else
If mvarItemToAdd < ti.Value Then
Set ti.LeftChild = AddNode(ti.LeftChild)
ElseIf mvarItemToAdd > ti.Value Then
Set ti.RightChild = AddNode(ti.RightChild)
Else
' mvarItemToAdd = ti.Value
' You're adding a node that already exists.
' You could add it to the left or to the right,
' but this code arbitrarily adds it to the right.
If mfAddDupes Then
Set ti.RightChild = AddNode(ti.RightChild)
End If
End If
End If
Set AddNode = ti
End Function
'-----------------------------------------
Public Sub WalkInOrder()
Call InOrder(tiHead)
End Sub
'-----------------------------------------
Public Sub WalkPreOrder()
Call PreOrder(tiHead)
End Sub
'-----------------------------------------
Public Sub WalkPostOrder()
Call PostOrder(tiHead)
End Sub
'-----------------------------------------
Private Sub InOrder(ti)
If Not ti Is Nothing Then
Call InOrder(ti.LeftChild)
WScript.Echo ti.Value & " "
Call InOrder(ti.RightChild)
End If
End Sub
'-----------------------------------------
Private Sub PreOrder(ti)
If Not ti Is Nothing Then
WScript.Echo ti.Value & " "
Call PreOrder(ti.LeftChild)
Call PreOrder(ti.RightChild)
End If
End Sub
'-----------------------------------------
Private Sub PostOrder(ti)
If Not ti Is Nothing Then
Call PostOrder(ti.LeftChild)
Call PostOrder(ti.RightChild)
WScript.Echo ti.Value & " "
End If
End Sub
End Class
--
Michael Harris
Microsoft MVP Scripting
l***@juno.com
2005-01-31 17:16:18 UTC
Permalink
One small correction to the Binary Tree code shown here:
mfAddDupes should be mfAddDups
Notice one with "e" and the other without.

Loading...