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