C# 树/集合算法

标签 c# algorithm tree collections

我需要用一些项目填充一个树状 UI 控件(当然是在parent-child1-child2-...childN 关系中),在继续之前我想确保保存内容的集合已正确排序如下:

我的集合(未排序的 ObservableCollection)中的每个对象(在本例中是我的 Category 类的实例)都有一个公共(public)属性(ParentCategoryID 作为字符串),该属性指向另一个“Category”,该属性将成为我的集合中的父级。树。树中的一个节点可以有 0 个或任意数量的子节点。

因此,在填充树时,要显示的每个“类别”对象在集合中已经有其“父类别”(基于 ParentCategoryID)。

在添加树中的元素之前,我应该使用什么算法来确保我的集合按这种方式排序?

最佳答案

我不确定这是否是您正在寻找的内容,但希望它会有所帮助:

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Program
{
   public static void Main(string[] args)
   {
      /* Tree Structure

         a
          d
           e   
         b
          f
          i
           j
         c
          g
           h
      */

      var a = new Category("a", null);
      var b = new Category("b", null);
      var c = new Category("c", null);
      var d = new Category("d", "a");
      var e = new Category("e", "d");
      var f = new Category("f", "b");
      var g = new Category("g", "c");
      var h = new Category("h", "g");
      var i = new Category("i", "b");
      var j = new Category("j", "i");
      var k = new Category("k", "z");

      var list = new CategoryCollection { k, j, i, h, g, f, e, d, c, b, a };
      foreach (var category in list.SortForTree())
      {
         Console.WriteLine("Name: {0}; Parent: {1}", category.Name, category.ParentCategoryID);
      }
   }
}

class Category
{
   public string ParentCategoryID { get; set; }
   public string Name { get; set; }
   public Category(string name, string parentCategoryID)
   {
      Name = name;
      ParentCategoryID = parentCategoryID;
   }
}

class CategoryCollection : IEnumerable<Category>
{
   private List<Category> list = new List<Category>();

   public void Add(Category category)
   {
      list.Add(category);
   }

   public IEnumerable<Category> SortForTree()
   {
      var target = new Dictionary<string, Category>();

      SortForTree(list, target);

      return target.Values;
   }

   private void SortForTree(List<Category> source, Dictionary<string, Category> target)
   {
      var temp = new List<Category>();

      foreach (var c in source)
      {
         if (c.ParentCategoryID == null || (target.ContainsKey(c.ParentCategoryID) && !target.ContainsKey(c.Name)))
         {
            target.Add(c.Name, c);
         }
         else
         {
            if (source.Exists(o => o.Name == c.ParentCategoryID))
            {
               temp.Add(c);
            }
         }
      }

      if (temp.Count > 0) SortForTree(temp, target);
   }

   #region IEnumerable<Category> Members

   public IEnumerator<Category> GetEnumerator()
   {
      return list.GetEnumerator();
   }

   #endregion

   #region IEnumerable Members

   IEnumerator IEnumerable.GetEnumerator()
   {
      return list.GetEnumerator();
   }

   #endregion
}

关于C# 树/集合算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/873140/

相关文章:

c# - Lambda 返回另一个 lambda

c# - 重新排序集合我做错了什么?

javascript - 如何从 JavaScript 数组值中找到所有长度的所有排列?

mysql - 物化路径和SQL查询解释

c# - 在树结构中通过 ID 属性定位实例

c# - Linq to Sql 对一组数据进行计数和平均(无分组)

c# - 将 BitmapImage 或 IRandomAccessStream 转换为 Windows 10 UAP 中的字节数组

这个动画 Java 小程序背后的算法

algorithm - 寻找矩阵 B 中的最大元素,比 O(n) 更好?实践技术面试

python - 创建树时Python中的对象引用问题