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

三、递归讲解

相关推荐
u***276134 分钟前
C#数据库操作系列---SqlSugar完结篇
网络·数据库·c#
N***73851 小时前
Vue网络编程详解
前端·javascript·vue.js
e***71671 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
程序猿小蒜1 小时前
基于springboot的的学生干部管理系统开发与设计
java·前端·spring boot·后端·spring
银空飞羽1 小时前
让Trae CN SOLO自主发挥,看看能做出一个什么样的项目
前端·人工智能·trae
Eshine、2 小时前
解决前端项目中,浏览器无法正常加载带.gz名称的文件
前端·vue3·.gz·.gz名称的js文件无法被加载
笑非不退2 小时前
C# c++ 实现程序开机自启动
开发语言·c++·c#
用户47949283569152 小时前
别再当 AI 的"人肉定位器"了:一个工具让 React 组件秒定位
前端·aigc·ai编程
WYiQIU3 小时前
面了一次字节前端岗,我才知道何为“造火箭”的极致!
前端·javascript·vue.js·react.js·面试
qq_316837753 小时前
uniapp 观察列表每个元素的曝光时间
前端·javascript·uni-app