利用委托和泛型实现树的常用操作
在日常開發中,經常遇到對樹的操作,我們可以利用泛型和委托對這些樹進行操作,這樣就不需要每有一個樹就要實現相應的功能了。
源碼在http://files.cnblogs.com/haiconc/LangTest.zip
首先,使用類泛型聲明:
?public class TreeOperator<T,K>
在類內聲明三個委托
??????? public delegate K GetTreeKey(T tree);//取得樹的Key
??????? GetTreeKey GTK;
??????? public delegate K GetParentKey(T tree);//取得樹的父節點
??????? GetParentKey GPK;
??????? public delegate bool IsKeyEqual(K keyA, K keyB);//比較Key是否相等
??????? IsKeyEqual IKE;
數據源:
?????? List<T> source;
構造函數:
?public int MaxDeep = 100;//防止在遞歸過程中進入無限死循環
?public TreeOperator(GetTreeKey gti, GetParentKey gtp, IsKeyEqual ike , List<T> source, int deep)
??????? {
??????????? this.GTK = gti;
??????????? this.GPK = gtp;
??????????? this.IKE = ike;
??????????? this.source = source;
??????????? this.MaxDeep = deep;
??????? }
實現第一個功能
??????? /// <summary>
??????? /// 取得從根樹到某個樹的路徑
??????? /// </summary>
??????? /// <param name="tree">某個樹</param>
??????? /// <param name="RootTree">根樹</param>
??????? /// <returns></returns>
??????? public List<T> GetTreePath(T tree, T RootTree)
??????? {
??????????? List<T> list = new List<T>();
??????????? T temp = tree;
??????????? int i = 0;
??????????? while (!IKE(GTK(temp), GTK(RootTree)))
??????????? {
??????????????? list.Add(temp);
??????????????? temp = GetParentTreeByTree(temp);
??????????????? /* 防止進入死循環 */
??????????????? i++;
??????????????? while (i > MaxDeep)
??????????????? {
??????????????????? break;
??????????????? }
??????????? }
??????????? list.Add(RootTree);
??????????? list.Reverse();
??????????? return list;
??????? }
??????? private T GetTreeByKey(K key)
??????? {
??????????? foreach (T tee in source)
??????????? {
??????????????? if(IKE(GTK(tee),key))
??????????????????? return tee;
??????????? }
??????????? return default(T);
??????? }
??????? private T GetParentTreeByTree(T tree)
??????? {
??????????? K key = GPK(tree);
??????????? return GetTreeByKey(key);
??????? }
第二個功能實現:
?? /// <summary>
??????? /// 取得某個樹的所有子孫樹
??????? /// </summary>
??????? /// <param name="tree"></param>
??????? /// <param name="source"></param>
??????? /// <returns></returns>
??????? public List<T> GetAllSubTree(T tree)
??????? {
??????????? List<T> list = new List<T>();
??????????? int deep = 0;
??????????? foreach (T tee in source)
??????????? {
??????????????? if(IKE(GPK(tee),GTK(tree)))
??????????????? {
??????????????????? list.Add(tee);
??????????????????? AddSub(tee, source, list, deep + 1);
??????????????? }
??????????? }
??????????? return list;
??????? }
??????? private void AddSub(T tree, List<T> source,List<T> list ,int deep )
??????? {
??????????? if (deep > MaxDeep) return;
??????????? foreach (T tee in source)
??????????? {
??????????????? if (IKE(GPK(tee), GTK(tree)))
??????????????? {
??????????????????? list.Add(tee);
??????????????????? AddSub(tee, source, list, deep + 1);
??????????????? }
??????????? }
??????? }
程序匆忙寫成,有待改進。
源碼在http://files.cnblogs.com/haiconc/LangTest.zip
轉載于:https://www.cnblogs.com/haiconc/archive/2012/02/12/2347769.html
總結
以上是生活随笔為你收集整理的利用委托和泛型实现树的常用操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一鸣真鲜奶吧加盟费多少钱?资料在哪里获取
- 下一篇: 北京秋天去哪里看枫叶呢?