拉拉裤一般多少钱一包:怎么用vb建立二叉树,并实现先.中.后序遍历和线索化?

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/06 02:40:40
最好能给出程序,谢谢
不要用网上搜的C语言程序糊弄我= =+
Treeview控件VB自带的么?

'二叉树的结点类
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组件。

二叉树在数据结构中是很重要的,常规的建树是用一个队列来建,语句比较复杂,我在学习数据结构时编了一个递归建树的算法,语句比较简洁,算法如下:

typedef struct node //二叉树结点的结构
{
char ch;
struct node *lchild, *rchild; 左指针和右指针
}bitree;

bitree * CreateBitree()
{
char ch;
bitree *b;
ch = getchar();

if(ch == '@') //表示该结点为空结点
{
b = NULL;
}
else
{
b = (bitree *)malloc(sizeof(bitree));
b

没有必要为了骗分,直接复制其他语言代码充数吧....
真的,树在VB中根本没有意义
像Borland公司生成Delphi8开始不支持树(指针),因为回造成不稳定

在VB中不能用指针,因此只能用别的方法建立树,比如可以试试用集合建立。
如果规模不大,也可以用Treeview控件来建立树,还能直接看见,多好!

建树是用一个队列来建,一个递归建树的算法,语句比较简洁,算法如下:
typedef struct node //二叉树结点的结构
{
char ch;
struct node *lchild, *rchild; 左指针和右指针
}bitree;

bitree * CreateBitree()
{
char ch;
bitree *b;
ch = getchar();

if(ch == '@') //表示该结点为空结点
{
b = NULL;
}
else
{
b = (bitree *)malloc(sizeof(bitree));
b

在VB中不能用指针,因此只能用别的方法建立树,比如可以试试用集合建立。
如果规模不大,也可以用Treeview控件来建立树,还能直接看见,多好!