请问各位高手,如何不使用递归遍历treeview中某个节点的所有字节点啊?

intersun 2004-04-13 09:36:16
请问各位高手,如何不使用递归遍历treeview中某个节点的所有字节点啊?
注意,不使用递归!
...全文
136 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
supergreenbean 2004-04-19
  • 打赏
  • 举报
回复
递归会有堆栈溢出的潜在危险,所以并不是什么时候用递归都好……

为了方便的关系,我用代码模拟了一个堆栈
'------------- 堆栈类 CStack.cls ---------------
Option Explicit
'本模块名称
Private Const THIS_MODULE_NAME As String = "CStack"

Private m_colStack As Collection
Public Function Push(v As Variant) As Boolean
On Error Resume Next
m_colStack.Add v
Push = (Err.Number = 0)
End Function

Public Function Pop(v As Variant) As Boolean
On Error Resume Next
With m_colStack
v = .Item(.Count)
.Remove .Count
Pop = (Err.Number = 0) And (.Count > 0)
End With
End Function

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

Private Sub Class_Terminate()
Set m_colStack = Nothing
End Sub


'------------- 窗体 Form1.frm -------------
Option Explicit
'本模块名称
Private Const THIS_MODULE_NAME As String = "Form1"

Private Sub Form_Load()
Dim oNode As Node
Dim i As Long
With TreeView1
.Nodes.Add , , "r1", "r1"
.Nodes.Add , , "r2", "r2"
.Nodes.Add "r2", tvwChild, "r3", "r3"
.Nodes.Add "r2", tvwChild, "r4", "r4"
.Nodes.Add "r4", tvwChild, "r5", "r5"
.Nodes.Add "r5", tvwChild, "r11", "r11"
.Nodes.Add "r4", tvwChild, "r6", "r6"
.Nodes.Add "r2", tvwChild, "r7", "r7"
.Nodes.Add "r7", tvwChild, "r10", "r10"

.Nodes.Add , , "r8", "r8"
Set oNode = .Nodes.Add("r8", tvwChild, "r9", "r9")

Debug.Print "遍历节点r2及其子节点"
WalkNode2 TreeView1, .Nodes("r2")
Debug.Print "遍历TreeView中所有节点"
WalkNode2 TreeView1, .Nodes(1), False
End With
End Sub

Sub WalkNode2(ByVal tv As TreeView, ByVal oNode As Node, Optional fFlag As Boolean = True)
Dim lLevel As Long, sKey As String
Dim fChild As Boolean, fEnd As Boolean
Dim oTmpNode As Node
Dim oChild As Node, oSibling As Node
Dim sText As String, lCount As Long
Dim oStackFlag As New CStack
Dim oStackNode As New CStack

lLevel = 0
Set oTmpNode = oNode
sText = ""
bgChild:
fChild = False
'----------- 对每个节点干点什么吧 -----------
lCount = lCount + 1
sText = Space(lLevel * 2) & oTmpNode.Text
Debug.Print sText, lCount
'----------- 对每个节点干点什么吧 -----------

Set oChild = oTmpNode.Child
If Not oChild Is Nothing Then
fChild = True
oStackNode.Push oTmpNode.Key
oStackFlag.Push fChild
Set oTmpNode = oChild
lLevel = lLevel + 1
GoTo bgChild
End If

bgSibling:
If lLevel = 0 And fFlag Then GoTo ed
Set oSibling = oTmpNode.Next
If Not oSibling Is Nothing Then
If lLevel = 0 And Not fEnd Then
oStackNode.Push oTmpNode.Key
oStackFlag.Push fChild
End If
Set oTmpNode = oSibling
GoTo bgChild
Else
If lLevel = 0 Then
fEnd = (lLevel = 0)
GoTo ed
End If
End If


If oStackNode.Pop(sKey) And oStackFlag.Pop(fChild) Then
Set oTmpNode = tv.Nodes(sKey)
If Not fChild Then
GoTo bgChild
Else
If lLevel > 0 Then
lLevel = lLevel - 1
End If
GoTo bgSibling
End If
End If
ed:
End Sub
tangxiaosan001 2004-04-16
  • 打赏
  • 举报
回复
能不能不用源去放电视啊.
ganzhiruogy 2004-04-16
  • 打赏
  • 举报
回复
干吗不用递归,
要不然要递归干吗
cslf 2004-04-16
  • 打赏
  • 举报
回复
递归是在方便的方法。
tfafei 2004-04-15
  • 打赏
  • 举报
回复
不行,如果你的层次没有确定的话,那最好还有用递归,这是我调试好的代码:Public Function WalkNode(oNode As Node, key As String, iflag As Integer) As Integer
'树视图中节点是否存在
On Error GoTo tferror2
If oNode.key = key Then '结点存在
iflag = 1
End If

If iflag <> 1 And Not oNode.Child Is Nothing Then
WalkNode oNode.Child, key, iflag
End If
If iflag <> 1 And Not oNode.Next Is Nothing Then
WalkNode oNode.Next, key, iflag
End If
WalkNode = iflag
Exit Function

tferror2:
Exit Function
End Function
Public Sub Create_Nodes(ByVal Nodes As Node, ByVal id_key As String, ByVal preid_key As String, ByVal source_key As String)

'''将数据加入树节点中,若其父节点不存在,则递归建立其父节点''2004.4.12'''''''''''''''''''''''''''
Dim strsql As String
Dim rs As ADODB.Recordset
Dim iflag As Integer
iflag = WalkNode(Nodex, preid_key, 0)
If iflag = 1 Then '父节点存在
Set Nodes = TreeView1.Nodes.Add(preid_key, tvwChild, id_key, source_key)
Else
strsql = "select * from table2 where id='" & preid_key & " '"
Set rs = TransactSQL(strsql)

id_key = rs.Fields("id")
preid_key = rs.Fields("preid")
source_key = rs.Fields("source")
Create_Nodes Nodes.Parent, id_key, preid_key, source_key
End If
End Sub

xiamei 2004-04-14
  • 打赏
  • 举报
回复
可能下面的代码也不满足你的要求,不过看看吧。
private bub treeview_childnode(tvw as treeview,byval vkey as string)
dim nNode as Node ,nNode1 as Node,nkey as string,i as integer
set nNode = tvw.nodes(vkey)
if nNode.children = 0 then exit sub
set nNode1 = nNode.child
i = 0
do while not nNode1 is nothing
redim preserve nkey(i)
nkey(i) = nNode1.key
set nNode1 = nNode1.next
i = i + 1
loop
end sub
intersun 2004-04-13
  • 打赏
  • 举报
回复
注意,不是遍历整棵树的节点,而是遍历某个节点的所有字节点。
intersun 2004-04-13
  • 打赏
  • 举报
回复
for ...next 循环变量值是多少呢?
要是遍历整棵树,倒还可以,可是如果一棵树中的节点是按层级加载的,也就是先加载完第三层在加载所有第四层的话,如何得知某个节点下面所有子节点的Index值呢?
daisy8675 2004-04-13
  • 打赏
  • 举报
回复
请问各位高手,如何不使用递归遍历treeview中某个节点的所有字节点啊?

for ...next 算不算

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧