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 对象的列表
}

三、递归讲解

相关推荐
这里有鱼汤几秒前
上班族没时间炒股?不妨试试这个隔夜超短战法(附:Python量化源码)
前端
n12352357 分钟前
Chrome 插件开发入门指南:从基础到实践
前端·chrome
前端 贾公子14 分钟前
ElementUI 中 validateField 对部分表单字段数组进行校验时多次回调问题
前端·javascript·elementui
棒棒的唐14 分钟前
vue2 elementUI 登录页面实现回车提交登录的方法
前端·javascript·elementui
one99616 分钟前
WPF应用程序中的异常处理
c#·.net·wpf
前端小万17 分钟前
一次紧急的现场性能问题排查
前端·性能优化
excel33 分钟前
为什么相同卷积代码在不同层学到的特征完全不同——基于 tfjs-node 猫图像识别示例的逐层解析
前端
知识分享小能手34 分钟前
React学习教程,从入门到精通,React 使用属性(Props)创建组件语法知识点与案例详解(15)
前端·javascript·vue.js·学习·react.js·前端框架·vue
用户214118326360236 分钟前
dify案例分享-免费玩转即梦 4.0 多图生成!Dify 工作流从搭建到使用全攻略,附案例效果
前端
CodeSheep36 分钟前
稚晖君又开始摇人了,有点猛啊!
前端·后端·程序员