乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 75|回复: 4

C#能实现这种情况吗

[复制链接]

34

主题

223

帖子

17

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
352
发表于 2018-1-18 14:39:00 | 显示全部楼层 |阅读模式
{{{1,2},{3,4},{5,6}} ,{{1,2},{3,4},{5 ,6}},{{1,2},{3,4},{5,6}}}
回复

使用道具 举报

32

主题

651

帖子

8

银币

中流砥柱

Rank: 25

铜币
779
发表于 2018-1-22 09:10:00 | 显示全部楼层
三维数组?List? List>>?
回复

使用道具 举报

34

主题

223

帖子

17

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
352
发表于 2018-1-23 07:59:00 | 显示全部楼层

三维数组需要确定长度,我这长度不固定,List和 List>>都试过了,都达不到想要的效果
回复

使用道具 举报

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2018-1-23 10:08:00 | 显示全部楼层
1、自己实现一个Tree
可以参考我的ResultTree类
2、你的问题实在不清晰,描述要准确
回复

使用道具 举报

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2018-1-23 10:10:00 | 显示全部楼层
还是贴上一个简化的版本吧
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace NFox.Collections
  6. {
  7.     ///
  8.     /// 深度优先遍历模式
  9.     ///
  10.     public enum DepthSearchModeType
  11.     {
  12.         ///
  13.         /// 前序遍历
  14.         ///
  15.         Preorder,
  16.         ///
  17.         /// 中序遍历
  18.         ///
  19.         //Inorder,
  20.         ///
  21.         /// 后序遍历
  22.         ///
  23.         Postorder
  24.     }
  25.     public class Tree : List>
  26.     {
  27.         public T Value { get; set; }
  28.         public Tree 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 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 other)
  56.         {
  57.             Tree 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 other)
  67.         {
  68.             return other.IsAncestorOf(this);
  69.         }
  70.         public List> Path
  71.         {
  72.             get
  73.             {
  74.                 List> lst = new List> { this };
  75.                 Tree 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 node)
  89.         {
  90.             node.Parent = this;
  91.             base.Insert(index, node);
  92.         }
  93.         public void InsertBefore(Tree node, T value)
  94.         {
  95.             Insert(IndexOf(node), new Tree(value));
  96.         }
  97.         public void InsertAfter(Tree node, T value)
  98.         {
  99.             Insert(IndexOf(node) + 1, new Tree(value));
  100.         }
  101.         public new void Add(Tree node)
  102.         {
  103.             node.Parent = this;
  104.             base.Add(node);
  105.         }
  106.         public void Add(T value)
  107.         {
  108.             Add(new Tree(value));
  109.         }
  110.         public new void AddRange(IEnumerable> collection)
  111.         {
  112.             foreach (var tree in collection)
  113.                 tree.Parent = this;
  114.             base.AddRange(collection);
  115.         }
  116.         public void AddRange(IEnumerable collection)
  117.         {
  118.             AddRange(collection.Select(value => new Tree(value)));
  119.         }
  120.         public void RemoveAll(Predicate match)
  121.         {
  122.             base.RemoveAll(tree => match(tree.Value));
  123.         }
  124.         //深度优先
  125.         public void SearchByDepthFirst(DepthSearchModeType mode, Action action)
  126.         {
  127.             if (mode == DepthSearchModeType.Preorder)
  128.                 action(this.Value);
  129.             foreach (Tree node in this)
  130.                 node.SearchByDepthFirst(mode, action);
  131.             if (mode == DepthSearchModeType.Postorder)
  132.                 action(this.Value);
  133.         }
  134.         //广度优先
  135.         public void SearchByBreadthFirst(Action action)
  136.         {
  137.             Queue> q = new Queue>();
  138.             q.Enqueue(this);
  139.             while (q.Count > 0)
  140.             {
  141.                 Tree node = q.Dequeue();
  142.                 action(node.Value);
  143.                 node.ForEach(child => q.Enqueue(child));
  144.             }
  145.         }
  146.     }
  147. }
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 02:38 , Processed in 0.798728 second(s), 73 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表