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

RE: vbscript binary tree - help needed by tlavedas

tlavedas
Tue Jan 25 08:57:01 CST 2005

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
===========

"ljb" wrote:

> 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
>
>
>
>
>

Re: vbscript binary tree - help needed by ljb

ljb
Tue Jan 25 09:36:08 CST 2005

That seems to be the problem.

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

thanks

"Tom Lavedas" <tlavedas@hotmail.remove.com> wrote in message
news:46730510-40BD-4664-B5EB-9EA4E0B8003C@microsoft.com...
> 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
> ===========
>
> "ljb" wrote:
>
> > 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
> >
> >
> >
> >
> >



Re: vbscript binary tree - help needed by Michael

Michael
Tue Jan 25 19:48:30 CST 2005

ljb wrote:
> 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



Re: vbscript binary tree - help needed by ljb

ljb
Wed Jan 26 07:52:06 CST 2005

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


"Michael Harris (MVP)" <mikhar at mvps dot org> wrote in message
news:uvivOk0AFHA.2112@TK2MSFTNGP14.phx.gbl...
> ljb wrote:
> > 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
>
>



Re: vbscript binary tree - help needed by ljbartel

ljbartel
Mon Jan 31 11:16:18 CST 2005

One small correction to the Binary Tree code shown here:
mfAddDupes should be mfAddDups
Notice one with "e" and the other without.