給你個例子希望可以幫到你
成都創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、三門網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5網(wǎng)站設計、成都做商城網(wǎng)站、集團公司官網(wǎng)建設、成都外貿網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為三門等各大城市提供網(wǎng)站開發(fā)制作服務。
遞歸系統(tǒng)目錄結構.
遞歸整個C盤目錄:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iDir As IO.Directory
Dim node As New TreeNode
'先把C盤添加到樹中
TreeView1.Nodes.Clear()
node.ImageIndex = 0
node.Text = "本地磁盤 C:"
node.SelectedImageIndex = -1
TreeView1.Nodes.Add(node)
Dim i As Integer
'獲取C:盤根目錄下的文件夾
Dim str() As String = IO.Directory.GetDirectories("D:\")
For i = 0 To str.GetUpperBound(0)
'調用遍歷過程
AddDirectory("C:", str(i), node)
Next
node = Nothing
iDir = Nothing
End Sub
Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)
Dim iDir As IO.Directory
'Dim iDirInfo As IO.DirectoryInfo
Dim i As Integer
Dim node As New TreeNode
'先添加本目錄,從文件夾路徑分析出文件夾名稱
node.Text = Strings.Replace(strPath, strFatherPath "\", "", , 1)
'為單個節(jié)點指定節(jié)點未被選中時顯示的圖標
node.ImageIndex = 1
'為單個節(jié)點指定節(jié)點被選中時顯示的圖標
node.SelectedImageIndex = 2
nodeFather.Nodes.Add(node)
Application.DoEvents()
Try
Dim str() As String = IO.Directory.GetDirectories(strPath)
'遍歷該目錄的子文件夾
For i = 0 To str.GetUpperBound(0)
AddDirectory(strPath, str(i), node)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
node = Nothing
iDir = Nothing
End Sub
嵌套是指一個過程調用另一個過程的使用,遞歸過程是這個過程中調用本過程的使用。遞歸是嵌套的一種特殊形式,遞歸過程必須有“歸點”,也就是終止調用本身的一個分支
這種不要用遞歸,知道關鍵字長度,知道節(jié)數(shù)用判斷就可以了,取前3位第一節(jié),第二節(jié)取3個為父節(jié)點,取全部為第二節(jié)關鍵字,第三節(jié)取前6個為父節(jié)點,取全部為第三節(jié)關鍵字.
并沒有要求培養(yǎng)字段,查詢時從len(ID)=3*N,每次循環(huán)時N都+1
select * from tablename where len(id)=3
select case N
case 1
treeview.node.add id,name
case 2
treeview.node(left(id,3)).node.add id,name
case 3
treeview.node(left(id,3)).node(left(id,6)).add id,name
end select
查詢結果排個升序就不會存在這樣的問題了,你剛才說的那種,如果中間沒有比001001001001001更短的關鍵字,那么就脫節(jié),這種是不可能添加到treeview中.
遞歸一般用在不知道節(jié)數(shù),沒有關鍵字的情況,比如系統(tǒng)目錄結構.
遞歸整個C盤目錄:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iDir As IO.Directory
Dim node As New TreeNode
'先把C盤添加到樹中
TreeView1.Nodes.Clear()
node.ImageIndex = 0
node.Text = "本地磁盤 C:"
node.SelectedImageIndex = -1
TreeView1.Nodes.Add(node)
Dim i As Integer
'獲取C:盤根目錄下的文件夾
Dim str() As String = IO.Directory.GetDirectories("D:\")
For i = 0 To str.GetUpperBound(0)
'調用遍歷過程
AddDirectory("C:", str(i), node)
Next
node = Nothing
iDir = Nothing
End Sub
Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)
Dim iDir As IO.Directory
'Dim iDirInfo As IO.DirectoryInfo
Dim i As Integer
Dim node As New TreeNode
'先添加本目錄,從文件夾路徑分析出文件夾名稱
node.Text = Strings.Replace(strPath, strFatherPath "\", "", , 1)
'為單個節(jié)點指定節(jié)點未被選中時顯示的圖標
node.ImageIndex = 1
'為單個節(jié)點指定節(jié)點被選中時顯示的圖標
node.SelectedImageIndex = 2
nodeFather.Nodes.Add(node)
Application.DoEvents()
Try
Dim str() As String = IO.Directory.GetDirectories(strPath)
'遍歷該目錄的子文件夾
For i = 0 To str.GetUpperBound(0)
AddDirectory(strPath, str(i), node)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
node = Nothing
iDir = Nothing
End Sub
首先VB是不推薦用遞歸算法的,效率太低,建議用循環(huán)做
然后你的dm是string,那么它咋就能作為二維數(shù)組的下標呢?
再說你的條件是 ri = TabA(i, 0) ,不是 ri=tab(i,dm)啊?