C#递归树实现实例简析

开发 后端
C#递归树实现实例主要向你介绍了通过递归算法的使用来实现数据库数据的两种具体的实现方法,那么本问就向你介绍具体的实现过程。

C#递归树实现实例是如何办到的呢?这里我们使用递归来实现对于数据的树形结构的呈现,那么关于递归的方向我们向你介绍两个,一个是从父节点出发,一个是反向的实现,让我们看看具体的实现步骤吧:

C#递归树实现实例:从父结点加字节点,注释的是把字节点向父结点上加

//将数据填充到dataTable  
DataTable mytable = new DataTable();  
//构建表结构  
DataRow myRow;  
DataColumn Id = new DataColumn("Id"typeof(System.Int32));  
mytable.Columns.Add(Id);  
DataColumn Name = new DataColumn("Name"typeof(System.String));  
mytable.Columns.Add(Name);  
DataColumn ParentId = new DataColumn("ParentId"typeof(System.Int32));  
mytable.Columns.Add(ParentId);  
DataColumn SiteInfo = new DataColumn("SiteInfo"typeof(SiteInfo));  
mytable.Columns.Add(SiteInfo);  
//填充数据  
//注意i是从1开始的,因为数据库收条没有意义,  
//数据库首条记录id为0,会引起死循环  
for (int i = 1; i <= cata.Length - 1; i++)  
{  
 myRow = mytable.NewRow();  
 myRow["Id"] = cata[i].Id;  
 myRow["Name"] = cata[i].Name;  
 myRow["ParentId"] = cata[i].ParentId;  
 myRow["SiteInfo"] = cata[i].SiteInfo;  
 
 mytable.Rows.Add(myRow);  
}  
 
 
this.InitTree(newNode.ChildNodes, 0, mytable);  
    
 
//Hashtable ht = new Hashtable();  
 
//for (int i = 1; i < cata.Length; i++)  
//{  
// ht.Add(cata[i].Id, cata[i]);  
//}  
 
 
//Hashtable allTreeNodes = new Hashtable();  
//Hashtable parentNodes = new Hashtable();  
////遍历树节点描述并拟向生成树结构  
//foreach (DictionaryEntry dict in ht)  
//{  
// CatalogInfo treeobj = (CatalogInfo)dict.Value;  
// if (allTreeNodes.Contains(treeobj.Id))  
//  continue;  
// TreeNode tn = new TreeNode();  
// tn.Text = treeobj.Name;  
// tn.Value = treeobj.Id.ToString();  
// tn.NavigateUrl = "main_right.aspx?catalogid=" + tn.Value;  
// tn.Target = "WorkArea";  
// generateParentTreeFromNode(  
tn, (CatalogInfo)treeobj, ht, parentNodes, allTreeNodes);  
//}  
 
////将所有根节点放到treeview上  
//foreach (DictionaryEntry dict in parentNodes)  
//{  
// newNode.ChildNodes.Add((TreeNode)dict.Value);  
//}  
  }  
}  
 private void InitTree(TreeNodeCollection Nds,   
int parentID, DataTable dt)//递归初始化树  
 {  
  TreeNode tmpNd;  
  //递归寻找子节点  
  DataRow[] rows = dt.Select("ParentID=" + parentID);  
  foreach (DataRow row in rows)  
  {  
tmpNd = new TreeNode();  
tmpNd.Value = row["Id"].ToString();  
tmpNd.Text = row["Name"].ToString();  
tmpNd.NavigateUrl = "main_right.aspx?catalogid=" 
+ row["Id"].ToString() + "&catalogName=  
" +Server.UrlEncode(row["Name"].ToString());  
tmpNd.Target = "WorkArea";  
Nds.Add(tmpNd);  
 
InitTree(tmpNd.ChildNodes, Convert.ToInt32(tmpNd.Value), dt);  
  }  
 }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.

C#递归树实现实例:从子节点出发考虑

/// <summary>  
/// 反向树生成算法  
/// </summary>  
/// <param name="subtr">子节点</param>  
/// <param name="subto">子节点描述对象</param>  
/// <param name="hashedTreeObject">被保存到哈希表中的子节点描述</param>  
/// <param name="hashedParents">被保存到哈希表中的父节点描述</param>  
/// <param name="allTreeNodes">被保存到哈希表中的所有节点描述</param>  
private void generateParentTreeFromNode(  
TreeNode subtr, CatalogInfo subto, Hashtable hashedTreeObject,   
Hashtable hashedParents, Hashtable allTreeNodes)  
{  
 
 TreeNode trParent;  
 if (null == hashedTreeObject)  
  return;  
 //确定父结点是否存在  
 if (!hashedTreeObject.Contains(  
subto.ParentId) || subto.ParentId == subto.Id )  
 {  
  //不存在则将当前节点作为根  
  if (hashedParents == null)  
hashedParents = new Hashtable();  
  if (!hashedParents.Contains(subto.ParentId))  
  {  
hashedParents.Add(subto.Id, subtr);  
if (!allTreeNodes.Contains(subto.Id))  
 allTreeNodes.Add(subto.Id, subtr);  
trParent = subtr;  
  }  
 }  
 else 
 {  
  //若存在,则获取父结点  
  CatalogInfo to = (CatalogInfo)hashedTreeObject[subto.ParentId];  
  //判断父结点是否已经被保存到树节点的哈希表中  
  if (allTreeNodes.Contains(subto.ParentId))  
  {  
//if (allTreeNodes.Contains(subto.Id))  
// return;  
trParent = (TreeNode)allTreeNodes[subto.ParentId];  
trParent.ChildNodes.Add(subtr);  
  }  
  else 
  {  
//父结点不存在于哈希表中,创建父结点并存放到叶子哈希表中  
trParent = new TreeNode();  
trParent.Text = to.Name;  
trParent.Value = to.Id.ToString();  
allTreeNodes.Add(subto.ParentId, trParent);  
trParent.ChildNodes.Add(subtr);  
//递归向上查找  
generateParentTreeFromNode(trParent, to,   
hashedTreeObject, hashedParents, allTreeNodes);  
if (!allTreeNodes.Contains(subto.Id))  
 allTreeNodes.Add(subto.Id, subtr);  
  }  
 
 }  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.

C#递归树实现实例的基本内容就向你介绍到这里,希望对你了解和学习C#递归树的实现有所帮助。

【编辑推荐】

  1. C#递归算法理解的实例分析
  2. C#递归思路的使用实例详解
  3. C#递归函数应用实例解析
  4. DropDownList显示的C#递归实现浅析
  5. C#treeview递归操作数据库浅析
责任编辑:仲衡 来源: 博客园
相关推荐

2009-08-13 14:36:40

C#结构体构造函数

2009-08-12 16:38:35

C#读取XML节点

2009-08-13 15:48:57

C#指针

2009-09-03 17:10:57

2009-09-01 16:59:25

C#画直线

2009-08-13 10:27:28

C#读取Excel数据

2009-08-27 18:09:49

C#接口的实现

2009-08-17 14:41:47

C#进度条实现

2009-09-01 13:59:01

C#操作Excel

2009-09-09 12:55:59

C# TextBox事

2009-08-20 09:58:06

C#操作文本文件

2009-09-02 16:14:21

C#动态创建数组

2009-09-03 17:06:17

C#回车切换焦点

2009-09-03 17:23:45

C#发送邮件

2009-08-12 10:07:51

C#运算符

2009-08-14 09:50:46

C#复制构造函数

2009-08-17 17:15:48

C# 进度条效果

2009-09-03 14:20:21

C#日期格式化

2009-08-31 12:31:45

C#创建文件夹

2009-09-03 12:52:50

C#打开记事本
点赞
收藏

51CTO技术栈公众号