2010년 10월 29일 금요일

클래스 모듈로 트리 데이터구조 만들기

완성된 모듈은 아니지만, 추후 연구자료로 사용하기 위해 임시로 남겨둔다.

클래스모듈(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 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

댓글 없음:

댓글 쓰기

2.1 벡터(Vector)

  R의 자료구조 : 벡터, factor, 행렬, 배열, 데이터프레임, 리스트 벡터(Vector)는 동일한 형태(예, 숫자)의 데이터 구성인자가 1개 이상이면서 1차원으로 구성되어 있는 데이터 구조입니다. w <- c(1, 2, 3, 4, ...