Hello! 欢迎来到小浪资源网!

Java如何使用递归实现树形结构的亲属关系查询并返回完整路径?


Java如何使用递归实现树形结构的亲属关系查询并返回完整路径?

Java如何用递归返回树结构的结果?

问题阐述:
需要实现一种算法,以树结构的形式返回给定名字的亲属关系链路。例如,以“秦朗”为搜索目标,结果应该返回“三国-曹操-秦朗”。

示例代码:

// 省略person类定义  public static void main(string[] args) {     // 构建人物关系树     person sunce = new person("孙策");     person sunquan = new person("孙权");     person sunjian = new person("孙坚");     sunjian.setchildren(arrays.aslist(sunce, sunquan));      person caopi = new person("曹丕");     person caozhi = new person("曹植");     person caozhang = new person("曹彰");     person qinlang = new person("秦朗");      person caocao = new person("曹操");     caocao.setchildren(arrays.aslist(caopi, caozhi, caozhang, qinlang));       person liufeng = new person("刘封");     person liushan = new person("刘禅");      person liubei = new person("刘备");     liubei.setchildren(arrays.aslist(liufeng, liushan));      person liuxun = new person("刘循");     person liuzhang = new person("刘璋");     liuzhang.setchildren(arrays.aslist(liuxun));     person liuyan = new person("刘焉");     liuyan.setchildren(arrays.aslist(liuzhang));      person sanguo = new person("三国");     sanguo.setchildren(arrays.aslist(sunjian, caocao, liubei, liuyan));      // 使用递归算法查询并打印结果     list<person> p1 = query(sanguo, "刘");     system.out.println(p1);     list<person> p2 = query(sanguo, "秦");     system.out.println(p2);     list<person> p3 = query(sanguo, "孙策");     system.out.println(p3); }  // 递归查询返回亲属关系链路 public static list<person> query(person person, string name) {     list<person> result = new arraylist<>();      // 判空     if(person.getname().contains(name)) {         return arrays.aslist(person);     } else {         if(person.getchildren() != null) {             for (person child : person.getchildren()) {                 result.addall(query(child, name));             }         }     }      return result; }

改进要求:

立即学习Java免费学习笔记(深入)”;

用户提出希望返回的树结构中包含命中和未命中结果,且去掉未命中结果的子节点。

修改后代码:

// 省略person类定义  public static void main(string[] args) {     // 构建人物关系树     // ... 同上 ...      // 使用递归算法生成树形结果并打印     personvo sanguo = createtree(sanguo, -1);     list<personvo> resulttree = query(sanguo, "秦朗");     system.out.println(json.tojsonstring(resulttree)); }  // 生成树形结构 private static personvo createtree(person person, int parentid) {     personvo personvo = new personvo(person.getid(), parentid, person.getname());     if (person.getchildren() != null) {         for (person child : person.getchildren()) {             personvo.getchildren().add(createtree(child, person.getid()));         }     }     return personvo; }  // 递归查询返回亲属关系链路 private static list<personvo> query(personvo person, string name) {     list<personvo> result = new arraylist<>();     if (person.getname().equals(name)) {         result.add(person);         return result;     } else {         for (personvo child : person.getchildren()) {             result.addall(query(child, name));         }     }     return result; }

改进后效果:

qinlang 的搜索结果:

[{     "id": 14     "parentid": 4,     "peoplename": "秦朗",     "children": [         {             "id": 4,             "parentid": 0,             "peoplename": "曹操",             "children": [                 {                     "id": 0,                     "parentid": -1,                     "peoplename": "三国"                 }             ]         }     ] }]

sunjian 的搜索结果:

[{     "id": 3,     "parentId": 0,     "peopleName": "孙坚",     "children": [         {            "id": 0,            "parentId": -1,            "peopleName": "三国"         }     ] }]

以上结果满足了用户提出的要求,返回的树形结构中包含所有命中和未命中的节点,且未命中的节点的子节点已被剔除。

相关阅读