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

三、递归讲解

相关推荐
怎么就重名了10 分钟前
Kivy的属性系统
java·前端·数据库
chao18984426 分钟前
基于C#实现Modbus通信及CRC校验
java·开发语言·c#
hxjhnct40 分钟前
JavaScript Promise 的常用API
开发语言·前端·javascript
web小白成长日记1 小时前
前端让我明显感受到了信息闭塞的恐怖......
前端·javascript·css·react.js·前端框架·html
xiaowu0801 小时前
C# 嵌入资源加载 + 外部配置文件的兜底配置
开发语言·c#
马达加斯加D1 小时前
Web系统设计 --- 接口防抖
c#
GIS之路1 小时前
GDAL 实现创建几何对象
前端
liulilittle2 小时前
CLANG 交叉编译
linux·服务器·开发语言·前端·c++
wen__xvn2 小时前
C++ 中 std::set 的用法
java·c++·c#
自信阿杜2 小时前
跨标签页数据同步完全指南:如何选择最优通信方案
前端·javascript