明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 857|回复: 4

C#能实现这种情况吗

[复制链接]
发表于 2018-1-18 14:39 | 显示全部楼层 |阅读模式
本帖最后由 fangmin723 于 2018-1-20 12:31 编辑

{{{1,2},{3,4},{5,6}} ,{{1,2},{3,4},{5 ,6}},{{1,2},{3,4},{5,6}}}

发表于 2018-1-22 09:10 | 显示全部楼层
三维数组?List<二维数组>? List<List<List<>>>?
 楼主| 发表于 2018-1-23 07:59 | 显示全部楼层
sieben 发表于 2018-1-22 09:10
三维数组?List? List?

三维数组需要确定长度,我这长度不固定,List<二维数组>和 List<List<List<>>>都试过了,都达不到想要的效果
发表于 2018-1-23 10:08 | 显示全部楼层
1、自己实现一个Tree
可以参考我的ResultTree类
2、你的问题实在不清晰,描述要准确
发表于 2018-1-23 10:10 | 显示全部楼层
还是贴上一个简化的版本吧
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace NFox.Collections
  6. {
  7.     /// <summary>
  8.     /// 深度优先遍历模式
  9.     /// </summary>
  10.     public enum DepthSearchModeType
  11.     {
  12.         /// <summary>
  13.         /// 前序遍历
  14.         /// </summary>
  15.         Preorder,

  16.         /// <summary>
  17.         /// 中序遍历
  18.         /// </summary>
  19.         //Inorder,

  20.         /// <summary>
  21.         /// 后序遍历
  22.         /// </summary>
  23.         Postorder
  24.     }

  25.     public class Tree<T> : List<Tree<T>>
  26.     {

  27.         public T Value { get; set; }

  28.         public Tree<T> Parent
  29.         { private set; get; }

  30.         public bool IsRoot
  31.         {
  32.             get { return Parent == null; }
  33.         }

  34.         public bool IsLeaf
  35.         {
  36.             get { return Count == 0; }
  37.         }

  38.         public Tree<T> Root
  39.         {
  40.             get
  41.             {
  42.                 var node = this;
  43.                 while (node.Parent != null)
  44.                 {
  45.                     node = node.Parent;
  46.                 }
  47.                 return node;
  48.             }
  49.         }

  50.         public Tree(){ }

  51.         public Tree(T value)
  52.         {
  53.             Value = value;
  54.         }

  55.         public bool IsAncestorOf(Tree<T> other)
  56.         {
  57.             Tree<T> node = other;
  58.             while (node.Parent != null)
  59.             {
  60.                 node = node.Parent;
  61.                 if (this == node)
  62.                     return true;
  63.             }
  64.             return false;
  65.         }

  66.         public bool IsChildOf(Tree<T> other)
  67.         {
  68.             return other.IsAncestorOf(this);
  69.         }

  70.         public List<Tree<T>> Path
  71.         {
  72.             get
  73.             {
  74.                 List<Tree<T>> lst = new List<Tree<T>> { this };
  75.                 Tree<T> node = this;
  76.                 while (node.Parent != null)
  77.                 {
  78.                     node = node.Parent;
  79.                     lst.Insert(0, node);
  80.                 }
  81.                 return lst;
  82.             }
  83.         }

  84.         public int Depth
  85.         {
  86.             get { return Path.Count - 1; }
  87.         }

  88.         public new void Insert(int index, Tree<T> node)
  89.         {
  90.             node.Parent = this;
  91.             base.Insert(index, node);
  92.         }

  93.         public void InsertBefore(Tree<T> node, T value)
  94.         {
  95.             Insert(IndexOf(node), new Tree<T>(value));
  96.         }

  97.         public void InsertAfter(Tree<T> node, T value)
  98.         {
  99.             Insert(IndexOf(node) + 1, new Tree<T>(value));
  100.         }

  101.         public new void Add(Tree<T> node)
  102.         {
  103.             node.Parent = this;
  104.             base.Add(node);
  105.         }

  106.         public void Add(T value)
  107.         {
  108.             Add(new Tree<T>(value));
  109.         }

  110.         public new void AddRange(IEnumerable<Tree<T>> collection)
  111.         {
  112.             foreach (var tree in collection)
  113.                 tree.Parent = this;

  114.             base.AddRange(collection);
  115.         }

  116.         public void AddRange(IEnumerable<T> collection)
  117.         {
  118.             AddRange(collection.Select(value => new Tree<T>(value)));
  119.         }

  120.         public void RemoveAll(Predicate<T> match)
  121.         {
  122.             base.RemoveAll(tree => match(tree.Value));
  123.         }

  124.         //深度优先
  125.         public void SearchByDepthFirst(DepthSearchModeType mode, Action<T> action)
  126.         {

  127.             if (mode == DepthSearchModeType.Preorder)
  128.                 action(this.Value);

  129.             foreach (Tree<T> node in this)
  130.                 node.SearchByDepthFirst(mode, action);

  131.             if (mode == DepthSearchModeType.Postorder)
  132.                 action(this.Value);


  133.         }

  134.         //广度优先
  135.         public void SearchByBreadthFirst(Action<T> action)
  136.         {
  137.             Queue<Tree<T>> q = new Queue<Tree<T>>();
  138.             q.Enqueue(this);
  139.             while (q.Count > 0)
  140.             {
  141.                 Tree<T> node = q.Dequeue();
  142.                 action(node.Value);
  143.                 node.ForEach(child => q.Enqueue(child));
  144.             }
  145.         }

  146.     }
  147. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-26 23:27 , Processed in 1.695386 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表