首页 > 其他分享 >遍历转树结构

遍历转树结构

时间:2024-01-29 21:56:19浏览次数:30  
标签:Layer 遍历 temp 树结构 Far item new Foo

遍历转树结构
{

    var list = new List<Foo>{
    new Foo("111",1),
    new Foo("112",2),
    new Foo("113",2),
    new Foo("114",2),
    new Foo("115",3),
    new Foo("116",1),
    new Foo("117",1),
    new Foo("118",2),
    new Foo("119",3),
    new Foo("120",4),

};
    var result = new List<Far>();

    foreach (var item in list)
    {
        if (result.Count == 0)
        {
            result.Add(new Far(item.Id, item.Layer, null));
            continue;
        }

        var temp = result.Last();

        while (true)
        {
            if (temp == null)
            {
                result.Add(new Far(item.Id, item.Layer, temp));
                break;
            }
            if (temp.Layer == item.Layer - 1)
            {
                temp.Children.Add(new Far(item.Id, item.Layer, temp));
                break;
            }

            if (temp.Layer == item.Layer)
            {
                if (temp.Parent == null)
                    result.Add(new Far(item.Id, item.Layer, null));

                else
                    temp.Parent.Children.Add(new Far(item.Id, item.Layer, null));

                break;
            }

            temp = temp.Children.LastOrDefault();
        }

    }
    Console.WriteLine("");
}

class Far
{
    public string Id { get; set; }
    public int Layer { get; set; }
    public Far Parent { get; set; }
    public List<Far> Children { get; set; }

    public Far(string id, int layer, Far parent)
    {
        Id = id;
        Layer = layer;
        Parent = parent;
        Children = new List<Far>();
    }
}


class Foo
{
    public string Id { get; set; }
    public int Layer { get; set; }

    public Foo(string id, int layer)
    {
        Id = id;
        Layer = layer;
    }
}

标签:Layer,遍历,temp,树结构,Far,item,new,Foo
From: https://www.cnblogs.com/Bo-H/p/17995451

相关文章

  • SqlServer中使用游标遍历数据集合
    具体代码如下所示:/***************************************** 实例:打印输出数据表BUS_Test中的Name和Age字段的值*****************************************/--声明遍历@Name和@AgeDECLARE@NameNVARCHAR(50),@AgeINT--声明游标C_UserDECLAREC_UserCURSORFAST_FOR......
  • 图的表示与遍历
    讲解           代码 P5318【深基18.例3】查找文献 #include<bits/stdc++.h>usingnamespacestd;constintN=100010;vector<int>a[N];intn,m,x,y,vis[N];queue<int>q;voiddfs(ints){ if(vis[s]==1)return; vis[s]=......
  • golang 遍历目录的两种方式、删除目录的两种方式
    funcmain(){ directory:="/Users/mike/Downloads" //不会递归只会读取当前的单层目录 directories,err:=os.ReadDir(directory) iferr!=nil{ fmt.Println(err) } for_,d:=rangedirectories{ fmt.Println(d.Name(),d.IsDir()) } //会递归遍历所......
  • 树的遍历
    二叉树的遍历前序遍历#include<bits/stdc++.h>usingnamespacestd;intn;structs{ intl,r,d;}a[10005];voidf(intt)//前序{ if(t==0)return; cout<<t<<''; f(a[t].l); f(a[t].r);}intmain(){cin>>n;for(inti=1;i<......
  • 简单树结构生成
    实体:@Data@EqualsAndHashCode(callSuper=false)@Accessors(chain=true)@ApiModel(value="EduSubject对象",description="课程科目")publicclassEduSubjectimplementsSerializable{privatestaticfinallongserialVersionUID=1L;@ApiMode......
  • KY11 二叉树遍历C++
    这个题目思路其实就是先序遍历的变形。相当于沿着先序遍历的顺序跟着构建二叉树就行。然后中序遍历这个树。#include<iostream>#include<string>usingnamespacestd;structtnode{chardata;structtnode*left;structtnode*right;};typedefstructt......
  • KY212 二叉树遍历C++
    思路是先构造出树,然后在后序遍历整个树。#include<iostream>#include<string>usingnamespacestd;structTnode{chardata;structTnode*left;structTnode*right;};typedefstructTnodeTree;Tree*build(stringpre,inth1,intt1,stringin,inth2......
  • 2024-1-24案例(地区查询)以及遍历方法
    目录案例(地区查询)步骤解析案例里面的map方法该案例的最后一个将数据插入到页面上案例(地区查询)需求:根据输入的省份名字和城市名字,查询地区并渲染列表步骤首先:确定URL网址和参数说明查询某个省内某个城市的所有地区参数名:pname:省份名字或直辖市名字,比如北京、福建省、辽......
  • 树结构及前中后续遍历
    publicclassTree{publicstaticvoidmain(String[]args){Treeroot=newTree(50);Tree.insert(root,30);Tree.insert(root,60);Tree.insert(root,70);Tree.insert(root,100);Tree.insert(root,80);......
  • 遍历删除集合元素
    1publicclassTest{2publicstaticvoidmain(String[]args){3List<String>list=newArrayList<>();4list.add("张三");5list.add("张三");6list.add("李四");7......