| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 461 人关注过本帖
标题:[求助] 关于二叉树
只看楼主 加入收藏
jssz06
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-5-4
收藏
 问题点数:0 回复次数:0 
[求助] 关于二叉树

小弟在做二叉树的演示在做二叉树的演示

从网上找到了如下的代码,但不能执行, 该怎么修改啊?

'二叉树的结点类
Option Explicit
Private mNodeValue As String '结点值
Private mLeftNode As clsBiTreeNode '左结点
Private mRightNode As clsBiTreeNode '右结点

'得到结点的值
Public Property Get NodeValue() As String
NodeValue = mNodeValue
End Property

'设置结点的值
Public Property Let NodeValue(ByVal pNodeValue As String)
mNodeValue = pNodeValue
End Property

'得到左结点
Public Property Get LeftNode() As clsBiTreeNode
Set LeftNode = mLeftNode
End Property

'设置左结点
Public Property Let LeftNode(ByVal pLeftNode As clsBiTreeNode)
Set mLeftNode = pLeftNode
End Property

'得到右结点
Public Property Get RightNode() As clsBiTreeNode
Set RightNode = mRightNode
End Property

'设置右结点
Public Property Let RightNode(ByVal pRightNode As clsBiTreeNode)
Set mRightNode = pRightNode
End Property


'一个二叉树的类
Option Explicit
Private mRoot As clsBiTreeNode

'建立一棵二叉树,同时以strValue为值建立根结点
Public Sub CreateTree(strValue As String)
Set mRoot = New clsBiTreeNode
mRoot.NodeValue = strValue
End Sub

'求二叉树的根结点
Public Function Root() As clsBiTreeNode
Set Root = mRoot
End Function

'求二叉树中值为strValue的结点的双亲
Public Function Parent(strValue As String) As clsBiTreeNode
Dim NodeTemp As clsBiTreeNode
FindNode mRoot, strValue, NodeTemp
Set Parent = NodeTemp
End Function

'求二叉树T中值为strValue的结点的左孩子
Public Function LeftChild(strValue As String) As clsBiTreeNode
Dim NodeTemp As clsBiTreeNode
Set LeftChild = Nothing
Set NodeTemp = FindNode(mRoot, strValue)
If Not NodeTemp Is Nothing Then Set LeftChild = NodeTemp.LeftNode
End Function

'求二叉树T中值为strValue的结点的右孩子
Public Function RightChild(strValue As String) As clsBiTreeNode
Dim NodeTemp As clsBiTreeNode
Set RightChild = Nothing
Set NodeTemp = FindNode(mRoot, strValue)
If Not NodeTemp Is Nothing Then Set RightChild = NodeTemp.RightNode
End Function

'遍历二叉树:采用前序遍历*******************
Public Sub TraverseF(ByRef Node As clsBiTreeNode)
If Node Is Nothing Then Exit Sub
Debug.Print Node.NodeValue & ":"
Traverse Node.LeftNode
Traverse Node.RightNode
End Sub

'遍历二叉树:采用中序遍历*****************
Public Sub TraverseM(ByRef Node As clsBiTreeNode)
If Node Is Nothing Then Exit Sub
Traverse Node.LeftNode
Debug.Print Node.NodeValue & ":"
Traverse Node.RightNode
End Sub

'遍历二叉树:采用后序遍历***************
Public Sub TraverseB(ByRef Node As clsBiTreeNode)
If Node Is Nothing Then Exit Sub
Traverse Node.LeftNode
Traverse Node.RightNode
Debug.Print Node.NodeValue & ":"
End Sub

'查找值为strValue的(第一个)结点:采用前序遍历
'参数说明:
'Node为树的根结点
'strValue要查找的结点的值
'myParentNode为要查找的结点的双亲结点
'pParentNode为第一个参数Node结点的双亲结点,是为了保存双亲结点而设置
Private Function FindNode(ByRef Node As clsBiTreeNode, ByVal strValue As String,
Optional ByRef myParentNode As clsBiTreeNode, Optional ByRef pParentNode As
clsBiTreeNode) As clsBiTreeNode
Dim NodeTemp As clsBiTreeNode
Set FindNode = Nothing
'如果为空则退出
If Node Is Nothing Then
Exit Function
End If
If Node.NodeValue = strValue Then
Set FindNode = Node
Set myParentNode = pParentNode
Exit Function
Else
Set NodeTemp = FindNode(Node.LeftNode, strValue, myParentNode, Node)
If Not NodeTemp Is Nothing Then
Set FindNode = NodeTemp
Set pParentNode = Node
Exit Function
End If
Set NodeTemp = FindNode(Node.RightNode, strValue, myParentNode, Node)
If Not NodeTemp Is Nothing Then
Set FindNode = NodeTemp
Set pParentNode = Node
Exit Function
End If
End If
End Function

'在二叉树中,将值为strChild的结点作为值为strParetn的结点的左孩子插入:返回""表示成功,否则返回错误信息
Public Function AddLeftChild(ByVal strParent As String, ByVal strChild As
String) As String
Dim NodeTemp As clsBiTreeNode
AddLeftChild = ""
Set NodeTemp = FindNode(mRoot, strParent)
If NodeTemp Is Nothing Then
AddLeftChild = "没有找到值为" & strParent & "的结点"
End If
If NodeTemp.LeftNode Is Nothing Then
Dim NewNode As New clsBiTreeNode
NewNode.NodeValue = strChild
NodeTemp.LeftNode = NewNode
Else
AddLeftChild = "值为" & strParent & "的结点的左结点已经存在"
End If

End Function


'在二叉树中,将值为strChild的结点作为值为strParetn的结点的右孩子插入:返回""表示成功,否则返回错误信息
Public Function AddRightChild(ByVal strParent As String, ByVal strChild As
String) As String
Dim NodeTemp As clsBiTreeNode
AddRightChild = ""
Set NodeTemp = FindNode(mRoot, strParent)
If NodeTemp Is Nothing Then
AddRightChild = "没有找到值为" & strParent & "的结点"
End If
If NodeTemp.RightNode Is Nothing Then
Dim NewNode As New clsBiTreeNode
NewNode.NodeValue = strChild
NodeTemp.RightNode = NewNode
Else
AddRightChild = "值为" & strParent & "的结点的右结点已经存在"
End If

End Function

'在二叉树中,删除值为strValue的结点的左孩子:返回""表示成功,否则返回错误信息
Public Function DelLeftChild(ByVal strValue As String) As String
Dim NodeTemp As clsBiTreeNode
DelLeftChild = ""
Set NodeTemp = FindNode(mRoot, strValue)
If NodeTemp Is Nothing Then
DelLeftChild = "没有找到值为" & strValue & "的结点"
End If
If Not NodeTemp.LeftNode Is Nothing Then
NodeTemp.LeftNode = Nothing
End If

End Function

'在二叉树中,删除值为strValue的结点的右孩子:返回""表示成功,否则返回错误信息
Public Function DelRightChild(ByVal strValue As String) As String
Dim NodeTemp As clsBiTreeNode
DelRightChild = ""
Set NodeTemp = FindNode(mRoot, strValue)
If NodeTemp Is Nothing Then
DelRightChild = "没有找到值为" & strValue & "的结点"
End If
If Not NodeTemp.RightNode Is Nothing Then
NodeTemp.RightNode = Nothing
End If

End Function

使用TreeView,要在部件中添加microsoft window common control 6.0组件。

搜索更多相关主题的帖子: 二叉树 
2007-05-28 16:47
快速回复:[求助] 关于二叉树
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058903 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved