您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關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#中怎么實現一個遞歸樹了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。