这期内容当中小编将会给大家带来有关C#中怎么实现一个递归树,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。C#递归树实现实例:从父结点加字节点,注释的是把字节点向父结点上加//将数据填充到dataTable
这期内容当中小编将会给大家带来有关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#中怎么实现一个递归树了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程网精选频道。
--结束END--
本文标题: C#中怎么实现一个递归树
本文链接: https://lsjlt.com/news/293904.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0