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);
- }
- }
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);
- }
- }
- }
C#递归树实现实例的基本内容就向你介绍到这里,希望对你了解和学习C#递归树的实现有所帮助。
【编辑推荐】
- C#递归算法理解的实例分析
- C#递归思路的使用实例详解
- C#递归函数应用实例解析
- DropDownList显示的C#递归实现浅析
- C#treeview递归操作数据库浅析