求一算法思路(生成树)
现在表结构如下Sid SParentId
1 0
2 0
3 1
4 0
5 1
6 5
现在要生成下面的树,请教达人们怎么样可以实现
根----1__
| |__3
| |__5
| |__6
2
4 --------------------编程问答-------------------- 递归 --------------------编程问答-------------------- 根据SParentId向上查找,直至该字段值为0.
建议可以将分类改为字符串,以"1|2|"的形式存储.这样比较容易查询 --------------------编程问答-------------------- /// <summary>创建树的根目录
/// 创建树的根目录
/// </summary>
/// <param name="as_GroupName"></param>
private void of_CreateTreeView(string as_GroupName,System.Web.UI.WebControls.TreeView atv_1)
{
string ls_TreeSql = "select FunctionName,CallPage,ListIcon from oa_FunctionList where GroupName = '" + as_GroupName + "' order by FunctionId";
string ls_FunctionName, ls_CallPage, ls_ListIcon;
DataTable ldt_DataTable1;
DataRow[] ldr_DataRow1;
DataAccess da_Function = new DataAccess();
ldt_DataTable1 = da_Function.FillData(ls_TreeSql);
if (ldt_DataTable1.Rows.Count > 0)
{
ldr_DataRow1 = ldt_DataTable1.Select();
foreach (DataRow ldr1 in ldr_DataRow1)
{
TreeNode ltv_Root = new TreeNode();
ls_FunctionName = ldr1["FunctionName"].ToString();
ls_CallPage = ldr1["CallPage"].ToString();
ls_ListIcon = ldr1["ListIcon"].ToString();
ltv_Root.Text = ls_FunctionName;
ltv_Root.Value = ls_FunctionName;
ltv_Root.ImageUrl = "../images/" + ls_ListIcon;
ltv_Root.Expanded = false;
if (!(ls_CallPage == "" || ls_CallPage == null))
{
ltv_Root.Target = "mainFrame";
ltv_Root.NavigateUrl = ls_CallPage;
}
else
{
ltv_Root.SelectAction = TreeNodeSelectAction.Expand;
}
atv_1.Nodes.Add(ltv_Root);
of_CreateTreeView1(ls_FunctionName, ltv_Root);
}
}
}
/// <summary>递归创建树
/// 递归创建树
/// </summary>
/// <param name="as_GroupName"></param>
/// <param name="tn_TreeNode1"></param>
private void of_CreateTreeView1(string as_GroupName, TreeNode tn_TreeNode1)
{
string ls_TreeSql = "select FunctionName,CallPage,ListIcon from oa_FunctionList where GroupName = '" + as_GroupName + "' order by FunctionId";
string ls_FunctionName, ls_CallPage, ls_ListIcon;
DataTable ldt_DataTable1;
DataRow[] ldr_DataRow1;
DataAccess da_Function = new DataAccess();
ldt_DataTable1 = da_Function.FillData(ls_TreeSql);
if (ldt_DataTable1.Rows.Count > 0)
{
ldr_DataRow1 = ldt_DataTable1.Select();
foreach (DataRow ldr1 in ldr_DataRow1)
{
TreeNode ltv_Root = new TreeNode();
ls_FunctionName = ldr1["FunctionName"].ToString();
ls_CallPage = ldr1["CallPage"].ToString();
ls_ListIcon = ldr1["ListIcon"].ToString();
ltv_Root.Text = ls_FunctionName;
ltv_Root.Value = ls_FunctionName;
ltv_Root.ImageUrl = "../images/" + ls_ListIcon;
if (!(ls_CallPage == "" || ls_CallPage == null))
{
ltv_Root.Target = "mainFrame";
ltv_Root.NavigateUrl = ls_CallPage;
}
else
{
ltv_Root.SelectAction = TreeNodeSelectAction.Expand;
}
tn_TreeNode1.ChildNodes.Add(ltv_Root);
of_CreateTreeView1(ls_FunctionName, ltv_Root);
}
}
}
补充:.NET技术 , C#