클래스모듈(TreeItem)
Public Value As Variant
Public LeftChild As TreeItem
Public RightChild As TreeItem
' tiHead is an anchor for the entire data structure.
Dim tiHead As TreeItem
' These private variables are used when adding new nodes.
Private mfAddDupes As Boolean
Private mvarItemToAdd As Variant
Public Sub Add(varNewItem As Variant)
' 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 As Variant)
' 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 As TreeItem) As TreeItem
' 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
End Sub
Public LeftChild As TreeItem
Public RightChild As TreeItem
' tiHead is an anchor for the entire data structure.
Dim tiHead As TreeItem
' These private variables are used when adding new nodes.
Private mfAddDupes As Boolean
Private mvarItemToAdd As Variant
Public Sub Add(varNewItem As Variant)
' 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 As Variant)
' 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 As TreeItem) As TreeItem
' 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
End Sub
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 As TreeItem)
If Not ti Is Nothing Then
Call InOrder(ti.LeftChild)
Debug.Print ti.Value; " ";
Call InOrder(ti.RightChild)
End If
End Sub
Private Sub PreOrder(ti As TreeItem)
If Not ti Is Nothing Then
Debug.Print ti.Value; " ";
Call PreOrder(ti.LeftChild)
Call PreOrder(ti.RightChild)
End If
End Sub
Private Sub PostOrder(ti As TreeItem)
If Not ti Is Nothing Then
Call PostOrder(ti.LeftChild)
Call PostOrder(ti.RightChild)
Debug.Print ti.Value; " ";
End If
End Sub
Source : http://msdn.microsoft.com/en-us/library/aa227540(VS.60).aspx
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 As TreeItem)
If Not ti Is Nothing Then
Call InOrder(ti.LeftChild)
Debug.Print ti.Value; " ";
Call InOrder(ti.RightChild)
End If
End Sub
Private Sub PreOrder(ti As TreeItem)
If Not ti Is Nothing Then
Debug.Print ti.Value; " ";
Call PreOrder(ti.LeftChild)
Call PreOrder(ti.RightChild)
End If
End Sub
Private Sub PostOrder(ti As TreeItem)
If Not ti Is Nothing Then
Call PostOrder(ti.LeftChild)
Call PostOrder(ti.RightChild)
Debug.Print ti.Value; " ";
End If
End Sub
댓글 없음:
댓글 쓰기