C#高级:递归2-根据ID反向递归求其所有的祖先节点信息

目录

一、实现demo

二、封装方法

【ID=>祖先ID】

【ID=>祖先实体】

三、递归讲解


一、实现demo

cs 复制代码
class MainClass
{
    static List<Person> PersonList = new List<Person>()
    {
        new Person(){ Id=1,ParentID=null,Name="小明曾祖父",},
        new Person(){ Id=2,ParentID=1,Name="小明爷爷",},
        new Person(){ Id=3,ParentID=1,Name="小明二爷爷",},
        new Person(){ Id=4,ParentID=1,Name="小明三爷爷",},
        new Person(){ Id=5,ParentID=2,Name="小明爸爸",},
        new Person(){ Id=6,ParentID=2,Name="小明叔叔",},
        new Person(){ Id=7,ParentID=5,Name="小明本人",},
        new Person(){ Id=8,ParentID=5,Name="小明妹妹",},
        new Person(){ Id=9,ParentID=7,Name="小明儿子",}
    };

    public static List<int> GetAncestors(int id)
    {
        var result = new List<int>(); // 初始化一个结果列表,用于存储所有父级 ID
        var parent = PersonList.FirstOrDefault(p => p.Id == id)?.ParentID; // 查找当前 ID 的父节点 ID

        if (parent.HasValue) // 如果有父节点
        {
            result.Add(parent.Value); // 将父节点 ID 添加到结果列表中
            result.AddRange(GetAncestors(parent.Value)); // 递归查找并添加父节点的所有上级 ID
        }

        return result; // 返回包含所有父级 ID 的列表
    }


    public static List<Person> GetPersonAncestors(int id)
    {
        var result = new List<Person>(); // 初始化一个结果列表,用于存储所有父级 Person 对象
        var person = PersonList.FirstOrDefault(p => p.Id == id); // 查找当前 ID 的 Person 对象

        if (person != null && person.ParentID.HasValue) // 如果找到 Person 对象且有父节点
        {
            var parent = PersonList.FirstOrDefault(p => p.Id == person.ParentID.Value); // 查找父节点 Person 对象
            if (parent != null)
            {
                result.Add(parent); // 将父节点 Person 对象添加到结果列表中
                result.AddRange(GetPersonAncestors(parent.Id)); // 递归查找并添加父节点的所有上级 Person 对象
            }
        }

        return result; // 返回包含所有父级 Person 对象的列表
    }


    static void Main(string[] args)
    {
        //要求:根据ID求其所有父级ID
        var ids = GetAncestors(9);
        var persons =GetPersonAncestors (9);
        ;
    }
}
class Person
{
    public int Id;
    public string Name;
    public int? ParentID;
}

二、封装方法

【ID=>祖先ID】

cs 复制代码
public static List<int> GetAncestors(int id)
{
    var result = new List<int>(); // 初始化一个结果列表,用于存储所有父级 ID
    var parent = PersonList.FirstOrDefault(p => p.Id == id)?.ParentID; // 查找当前 ID 的父节点 ID

    if (parent.HasValue) // 如果有父节点
    {
        result.Add(parent.Value); // 将父节点 ID 添加到结果列表中
        result.AddRange(GetAncestors(parent.Value)); // 递归查找并添加父节点的所有上级 ID
    }

    return result; // 返回包含所有父级 ID 的列表
}

【ID=>祖先实体】

cs 复制代码
public static List<Person> GetPersonAncestors(int id)
{
    var result = new List<Person>(); // 初始化一个结果列表,用于存储所有父级 Person 对象
    var person = PersonList.FirstOrDefault(p => p.Id == id); // 查找当前 ID 的 Person 对象

    if (person != null && person.ParentID.HasValue) // 如果找到 Person 对象且有父节点
    {
        var parent = PersonList.FirstOrDefault(p => p.Id == person.ParentID.Value); // 查找父节点 Person 对象
        if (parent != null)
        {
            result.Add(parent); // 将父节点 Person 对象添加到结果列表中
            result.AddRange(GetPersonAncestors(parent.Id)); // 递归查找并添加父节点的所有上级 Person 对象
        }
    }

    return result; // 返回包含所有父级 Person 对象的列表
}

三、递归讲解

相关推荐
风骏时光牛马2 小时前
后端服务接口开发与性能优化
前端
kyriewen2 小时前
我让 4 个 AI 互相 review 代码,最温柔的那个下手最狠
前端·程序员·ai编程
计算机魔术师2 小时前
493家公司白砸AI钱?Apollo这组数据戳破了幻觉
前端
吴可可1232 小时前
C#调用摄像头拍照的三种API方案
c#
IT_陈寒2 小时前
Java静态变量初始化坑得我头皮发麻
前端·人工智能·后端
柳杉3 小时前
用 Codex 完成智慧高速 3D 可视化大屏,从 18 张行业设计稿到可交互的运行监测中心
前端·ai编程·数据可视化
review445433 小时前
拿到 codex 第一步
前端·agent·vibecoding
鶴哥只手遮天3 小时前
jQuery 4.0 源码架构与工程实践-第 01 章 · 全景与工程结构:一个 20 岁库的自省
前端·javascript·jquery
徐小夕3 小时前
doc2docx方案,开源!
前端·vue.js·github
徐小夕3 小时前
JitOFD:一款面向信创与档案场景的 Web 端 OFD 编辑器
前端·vue.js·github