大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
int nestAdd(int n) {
创新互联公司是创新、创意、研发型一体的综合型网站建设公司,自成立以来公司不断探索创新,始终坚持为客户提供满意周到的服务,在本地打下了良好的口碑,在过去的十载时间我们累计服务了上千家以及全国政企客户,如成都花箱等企业单位,完善的项目管理流程,严格把控项目进度与质量监控加上过硬的技术实力获得客户的一致夸奖。
if (n 1) return n + nestAdd(n - 1);
else return n;
}
给你个例子希望可以帮到你
递归系统目录结构.
递归整个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)
'为单个节点指定节点未被选中时显示的图标
node.ImageIndex = 1
'为单个节点指定节点被选中时显示的图标
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
说实话一般不是你这样存储的,一般是用一个字段存储ID号,然后用另一个字段存储上级单位的ID号,然后用递归函数生成TreeView。建议你增加一个字段,然后将编号解析出的上级单位存储在里面。以下是我的一个程序用的递归函数:
表名是unit,其中包含了单位名称unit_name,编号unit_id,Unit_upper就是上级单位的id号。
调用方式是 addtree(ds1, TreeView1.Nodes, 0),其中ds1是一个DataSet,对应单位的表,treeview1是控件名称,0是固定值,与表中根节点部门的Unit_upper一致(这个单位没有上级)。
Public Function addtree(ByVal ds As DataSet, ByVal treename As TreeNodeCollection, ByVal x1 As Integer) As Boolean
Dim dt As DataTable = ds.Tables("unit")
Dim dm As BindingManagerBase = Me.BindingContext(ds, "unit")
Dim dr As DataRow() = dt.Select("unit_upper=" x1.ToString)
Dim dr1 As DataRow
Dim nd As TreeNode
Dim nd1 As TreeNode
Dim x2 As Integer
If dr.GetLength(0) 0 Then
For Each dr1 In dr
nd = treename.Add(dr1("unit_name"))
nd.Tag = (dr1("unit_id"))
x2 = treename.IndexOf(nd)
addtree(ds, treename.Item(x2).Nodes, dr1("unit_id"))
Next
End If
Return True
End Function
这种不要用递归,知道关键字长度,知道节数用判断就可以了,取前3位第一节,第二节取3个为父节点,取全部为第二节关键字,第三节取前6个为父节点,取全部为第三节关键字.
并没有要求培养字段,查询时从len(ID)=3*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更短的关键字,那么就脱节,这种是不可能添加到treeview中.
递归一般用在不知道节数,没有关键字的情况,比如系统目录结构.
递归整个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)
'为单个节点指定节点未被选中时显示的图标
node.ImageIndex = 1
'为单个节点指定节点被选中时显示的图标
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