'类Tree
Private l As Tree
Private r As Tree
Private v As Variant
Public Property Get LeftLink() As Tree
Set LeftLink = l
End Property
Public Property Set LeftLink(NewValue As Tree)
Set l = NewValue
End Property
Public Property Get RightLink() As Tree
Set RightLink = r
End Property
Public Property Set RightLink(NewValue As Tree)
Set r = NewValue
End Property
Public Property Get Value() As Variant
Value = v
End Property
Public Property Let Value(NewValue As Variant)
v = NewValue
End Property
'测试代码
Dim Root As Tree
Private Sub Command1_Click()
Dim a
a = Array(30, 12, 23, 42, 13, 32, 22, 53, 25)
Dim i As Long
If Root Is Nothing Then Set Root = New Tree
For i = 0 To UBound(a)
Ins Root, a(i)
Next
Disp Root
Remove Root
End Sub
Sub Ins(t As Tree, v As Variant)
Dim s As Tree
If IsEmpty(t.Value) Then
t.Value = v
ElseIf v < t.Value Then
If t.LeftLink Is Nothing Then Set t.LeftLink = New Tree
Ins t.LeftLink, v
Else
If t.RightLink Is Nothing Then Set t.RightLink = New Tree
Ins t.RightLink, v
End If
End Sub
Sub Disp(t As Tree)
If t Is Nothing Then
Else
Disp t.LeftLink
Debug.Print t.Value
Disp t.RightLink
End If
End Sub
Sub Remove(t As Tree)
If Not t.LeftLink Is Nothing Then Remove t.LeftLink
If Not t.RightLink Is Nothing Then Remove t.RightLink
If Not t Is Nothing Then Set t = Nothing
End Sub
'QQ:24821781
'还有更经典的,有兴趣请发EMAIL至:cowknife@163.com
不用那么累,看我的代码,这是一个类模块,类名TREE
Private l As Tree
Private r As Tree
Private v As Variant
Public Property Get LeftLink() As Tree
Set LeftLink = l
End Property
Public Property Set LeftLink(NewValue As Tree)
Set l = NewValue
End Property
Public Property Get RightLink() As Tree
Set RightLink = r
End Property
Public Property Set RightLink(NewValue As Tree)
Set r = NewValue
End Property
Public Property Get Value() As Variant
Value = v
End Property
Public Property Let Value(NewValue As Variant)
v = NewValue
End Property
这是测试代码:
Dim Root As Tree
Private Sub Command1_Click()
Dim a
a = Array(30, 12, 23, 42, 13, 32, 22, 53, 25)
Dim i As Long
If Root Is Nothing Then Set Root = New Tree
For i = 0 To UBound(a)
Ins Root, a(i)
Next
Disp Root
Remove Root
End Sub
Sub Ins(t As Tree, v As Variant)
Dim s As Tree
If IsEmpty(t.Value) Then
t.Value = v
ElseIf v < t.Value Then
If t.LeftLink Is Nothing Then Set t.LeftLink = New Tree
Ins t.LeftLink, v
Else
If t.RightLink Is Nothing Then Set t.RightLink = New Tree
Ins t.RightLink, v
End If
End Sub
Sub Disp(t As Tree)
If t Is Nothing Then
Else
Disp t.LeftLink
Text1.Text = t.Value
Disp t.RightLink
End If
End Sub
Sub Remove(t As Tree)
If Not t.LeftLink Is Nothing Then Remove t.LeftLink
If Not t.RightLink Is Nothing Then Remove t.RightLink
If Not t Is Nothing Then Set t = Nothing
End Sub