中级农民
- 积分
- 259
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2020-7-22
- 最后登录
- 1970-1-1
|
本帖最后由 rainly 于 2020-9-11 08:44 编辑
多谢lz,没怎么用过Function, 看都费劲,我就用一堆get,也很简洁啊,麻烦lz看看对么?
- import java.util.*;
- public class FamilyTree {
- public static void main(String[] args) {
- String[] input = {"a1,b1->a2,b2","a2,c2->a3,c3","a3,d3->a4,d4","a4,e4->a5,e5","b2,d2->b3,d3"};
- FamilyTree tree=new FamilyTree(input);
- for(Person p:tree.getChildren(tree.getPerson("a1"))) p.print();
- for(Person p:tree.getParents(tree.getPerson("a2"))) p.print();
- for(Person p:tree.getSiblings(tree.getPerson("a2"))) p.print();
- for(Person p:tree.getUncleAunt(tree.getPerson("a3"))) p.print();
- for(Person p:tree.getCousins(tree.getPerson("a3"))) p.print();
- System.out.println("grandChildren");
- for(Person p:tree.getGrandChildren(tree.getPerson("a1"))) p.print();
- for(Person p:tree.getGreatGreatGrandChildren(tree.getPerson("a1"))) p.print();
- }
- class Person {
- String name;
- Set<Person> children;
- Set<Person> parents;
- Person(String name) {
- this.name=name;
- children=new HashSet<>();
- parents=new HashSet<>();
- }
- public Set<Person> getParents() {
- return parents;
- }
- public Set<Person> getChildren() {
- return children;
- }
- public void addParent(Person p){
- parents.add(p);
- }
- public void addChild(Person c) {
- children.add(c);
- }
- public void print() {
- System.out.println(name);
- System.out.println();
- }
- }
- Map<String,Person> map;
- FamilyTree(String[] relation) {
- map=new HashMap<>();
- for(String s:relation) {
- String[] pairs=s.split("->");
- String[] parents=pairs[0].split(","), children= pairs[1].split(",");
- Person p1=map.getOrDefault(parents[0], new Person(parents[0]));
- Person p2=map.getOrDefault(parents[1], new Person(parents[1]));
- map.putIfAbsent(parents[0],p1);
- map.putIfAbsent(parents[1],p2);
- for(String child:children) {
- Person c=map.getOrDefault(child,new Person(child));
- map.putIfAbsent(child,c);
- c.addParent(p1);
- c.addParent(p2);
- p1.addChild(c);
- p2.addChild(c);
- }
-
- }
- }
- public Person getPerson(String person) {
- return map.get(person);
- }
- public Set<Person> getChildren(Person p) {
- return p.getChildren();
- }
- public Set<Person> getParents(Person p) {
- return p.getParents();
- }
- public Set<Person> getSiblings(Person p) {
- Set<Person> siblings=new HashSet<>();
- for(Person parent : p.getParents()) {
- siblings.addAll(parent.getChildren());
- }
- siblings.remove(p);
- return siblings;
- }
- public Set<Person> getUncleAunt(Person p) {
- Set<Person> uncleAunt=new HashSet<>();
- for(Person parent : p.getParents()) {
- uncleAunt.addAll(getSiblings(parent));
- }
- return uncleAunt;
- }
- public Set<Person> getCousins(Person p) {
- Set<Person> cousins=new HashSet<>();
- for(Person parent : p.getParents()) {
- for(Person sibling:getSiblings(parent))
- cousins.addAll(sibling.getChildren());
- }
- return cousins;
- }
- public Set<Person> getGrandChildren(Person p) {
- Set<Person> grandChildren=new HashSet<>();
- for(Person child : p.getChildren()) {
- grandChildren.addAll(child.getChildren());
- }
- return grandChildren;
- }
- public Set<Person> getGreatGreatGrandChildren(Person p) {
- Set<Person> gggChildren=new HashSet<>();
- for(Person gChild : getGrandChildren(p)) {
- gggChildren.addAll(getGrandChildren(gChild));
- }
- return gggChildren;
- }
- }
复制代码
|
|