這種不要用遞歸,知道關鍵字長度,知道節(jié)數用判斷就可以了,取前3位第一節(jié),第二節(jié)取3個為父節(jié)點,取全部為第二節(jié)關鍵字,第三節(jié)取前6個為父節(jié)點,取全部為第三節(jié)關鍵字.
創(chuàng)新互聯是一家專業(yè)提供聊城企業(yè)網站建設,專注與做網站、成都網站設計、H5頁面制作、小程序制作等業(yè)務。10年已為聊城眾多企業(yè)、政府機構等服務。創(chuàng)新互聯專業(yè)網站建設公司優(yōu)惠進行中。
并沒有要求培養(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é)數,沒有關鍵字的情況,比如系統(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
給你個例子希望可以幫到你
遞歸系統(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
嵌套是指一個過程調用另一個過程的使用,遞歸過程是這個過程中調用本過程的使用。遞歸是嵌套的一種特殊形式,遞歸過程必須有“歸點”,也就是終止調用本身的一個分支
預先準備三個圖標文件,用于樹型控件中顯示磁盤符號和文件夾的圖像之用。
1、窗體上添加控件如下:
組合框控件 ComboBox1,樹型控件 TreeView1,列表框控件 ListBox1,圖像列表控件 ImageList1。
選中TreeView1,設置其ImageList屬性為ImageList1。
2、設置屬性
選中圖像列表控件 ImageList1,在屬性窗口里,選中屬性Images,單擊三個小點按鈕,出現圖像集合編輯器窗口,單擊[添加按鈕],一一把準備好的圖標文件進行添加,注意先后次序,如果不符合要求可以通過上下移動按鈕重新改變次序。完成后單擊[確定]。
運行圖如下:
完整代碼如下:
Imports?System.IO
Public?Class?Form1
Private?Sub?Form1_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
'添加系統(tǒng)所有磁盤目錄符號
For?Each?MyDrive?As?String?In?Environment.GetLogicalDrives()
ComboBox1.Items.Add(MyDrive)
Next
'顯示第一個磁盤符號
ComboBox1.Text?=?ComboBox1.Items(0)
End?Sub
'遞歸過程添加目錄樹
Public?Sub?AddDirectory(ByVal?strFatherPath?As?String,?ByVal?strPath?As?String,?ByVal?nodeFather?As?TreeNode)
Dim?i?As?Integer
Dim?Mynode?As?New?TreeNode
'先添加本目錄
Mynode.Text?=?Strings.Replace(strPath,?strFatherPath??"\",?"",?,?1)
'為節(jié)點指定未被選中時顯示的圖標
Mynode.ImageIndex?=?1
'為節(jié)點指定被選中時顯示的圖標
Mynode.SelectedImageIndex?=?2
nodeFather.Nodes.Add(Mynode)
Application.DoEvents()
Try
Dim?str()?As?String?=?Directory.GetDirectories(strPath)
'遞歸遍歷該目錄的子文件夾
For?i?=?0?To?str.GetUpperBound(0)
AddDirectory(strPath,?str(i),?Mynode)
Next
Catch?ex?As?Exception
Debug.WriteLine(ex.Message)
End?Try
Mynode?=?Nothing
End?Sub
'根據給出的盤符添加目錄樹
Private?Sub?AddRootDirectory(ByVal?DiscSymbol?As?String)
Dim?Nynode?As?New?TreeNode
'先把磁盤盤符添加到樹中
TreeView1.Nodes.Clear()
Nynode.ImageIndex?=?0
Nynode.Text?=?DiscSymbol
Nynode.SelectedImageIndex?=?-1
TreeView1.Nodes.Add(Nynode)
Dim?i?As?Integer
'獲取磁盤根目錄下的文件夾
Dim?str()?As?String?=?Directory.GetDirectories(DiscSymbol??"\")
For?i?=?0?To?str.GetUpperBound(0)
'調用遞歸過程遍歷該文件夾里的所有子文件夾,并添加到樹型控件
AddDirectory(DiscSymbol,?str(i),?Nynode)
Next
Nynode?=?Nothing
End?Sub
Private?Sub?ComboBox1_SelectedIndexChanged(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?ComboBox1.SelectedIndexChanged
'根據磁盤符號的變更,顯示根目錄里的文件
ListBox1.Items.Clear()
For?Each?MyFile?As?String?In?System.IO.Directory.GetFiles(ComboBox1.Text)
ListBox1.Items.Add(MyFile)
Next
'根據磁盤符號的變更,重新顯示目錄樹
Dim?DiscSymbol?As?String
DiscSymbol?=?Microsoft.VisualBasic.Left(ComboBox1.Text,?Len(ComboBox1.Text)?-?1)
Call?AddRootDirectory(DiscSymbol)
End?Sub
'遞歸過程根據子目錄尋找上級目錄名--從而構成完整的目錄路徑
Private?Sub?AllPath(ByVal?ThisNode?As?TreeNode,?ByRef?MyPathName?As?String)
If?ThisNode.Level??1?Then
'該節(jié)點層數大于1,其父節(jié)點不是磁盤根目錄
MyPathName?=?ThisNode.Parent.Text??"\"??MyPathName
Dim?MyNode?As?TreeNode?=?ThisNode.Parent
Call?AllPath(MyNode,?MyPathName)
Else
'該節(jié)點層數等于1,其父節(jié)點就是磁盤根目錄
MyPathName?=?ComboBox1.Text??MyPathName
End?If
End?Sub
Private?Sub?TreeView1_AfterSelect(ByVal?sender?As?System.Object,?ByVal?e?As?System.Windows.Forms.TreeViewEventArgs)?Handles?TreeView1.AfterSelect
'為了搜索選中的節(jié)點對應目錄的文件,需要組成全路徑
Dim?MyAllPathName?As?String?=?TreeView1.SelectedNode.Text
Dim?MyNode?As?TreeNode?=?TreeView1.SelectedNode
If?TreeView1.SelectedNode.Level?=?0?Then
'如果選中的是根節(jié)點
MyAllPathName?=?ComboBox1.Text
Else
'如果選中的是非根節(jié)點,調用遞歸過程組成全路徑
Call?AllPath(MyNode,?MyAllPathName)
MyAllPathName?=?MyAllPathName??"\"
End?If
'根據路徑,搜索文件名并顯示
ListBox1.Items.Clear()
For?Each?MyFile?As?String?In?System.IO.Directory.GetFiles(MyAllPathName)
ListBox1.Items.Add(MyFile)
Next
End?Sub
End?Class