多分支树的类模块(原创)

来源:互联网 发布:oracle数据库文本类型 编辑:程序博客网 时间:2024/04/29 13:50

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Branch"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Collection" ,"Branch"
Attribute VB_Ext_KEY = "Member0" ,"Branch"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit

Public Key As String
Public Value As Variant
Public Parent As Branch

'局部变量,保存集合
Private mCol As Collection

Public Function AddChild(Optional Value, Optional sKey As String) As Branch
    Set AddChild = New Branch
    With AddChild
        If IsObject(Value) Then
            Set .Value = Value
        Else
            .Value = Value
        End If
        .Key = sKey
        Set .Parent = Me
    End With
    If Key <> "" Then
        mCol.Add AddChild, sKey
    Else
        mCol.Add AddChild
    End If
End Function

Public Property Get Children(vntIndexKey As Variant) As Branch
Attribute Children.VB_UserMemId = 0
  Set Children = mCol(vntIndexKey)
End Property

Public Property Get Count() As Long
    Count = mCol.Count
End Property

Public Sub Remove(vntIndexKey As Variant)
    mCol.Remove vntIndexKey
End Sub

Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
    Set NewEnum = mCol.[_NewEnum]
End Property

Private Sub Class_Initialize()
    Set mCol = New Collection
End Sub

Private Sub Class_Terminate()
    Set mCol = Nothing
    Set Value = Nothing
End Sub

Public Function FindChild(ByVal sKey As String) As Branch
    If Key = sKey Then
        Set FindChild = Me
    Else
        Dim i As Long
        For i = 1 To mCol.Count
            FindChild = mCol(i).FindChild(sKey)
            If Not FindChild Is Nothing Then Exit Function
        Next i
    End If
End Function

Public Property Get Root() As Branch
    Set Root = Me
    Do While Not Root.Parent Is Nothing
        Set Root = Root.Parent
    Loop
End Property