|
| 1 | +package ood.design; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class FamilyTree2 { |
| 6 | + |
| 7 | + private Person root; |
| 8 | + private Map<Integer, Set<Person>> parentMap; |
| 9 | + |
| 10 | + |
| 11 | + public FamilyTree2(Person r){ |
| 12 | + this.root = r; |
| 13 | + this.parentMap = new TreeMap<>(); |
| 14 | + buildParentMap(this.root,this.parentMap); |
| 15 | + |
| 16 | + } |
| 17 | + |
| 18 | + private void buildParentMap(Person parent, Map<Integer, Set<Person>> map){ |
| 19 | + if(parent == null || parent.children.isEmpty()) |
| 20 | + return; |
| 21 | + |
| 22 | + for(Person child : parent.children){ |
| 23 | + map.putIfAbsent(child.id,new HashSet<>()); |
| 24 | + map.get(child.id).add(parent); |
| 25 | + if(parent.partner !=null) |
| 26 | + map.get(child.id).add(parent.partner); |
| 27 | + buildParentMap(child,map); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public List<Person> getGrandChilds(Person person){ |
| 32 | + List<Person> gc = new ArrayList<>(); |
| 33 | + if(person.children.isEmpty()) |
| 34 | + return gc; |
| 35 | + |
| 36 | + for(Person c1 : person.children){ |
| 37 | + if(c1.children.isEmpty()) |
| 38 | + continue; |
| 39 | + gc.addAll(c1.children); |
| 40 | + } |
| 41 | + return gc; |
| 42 | + } |
| 43 | + |
| 44 | + public Set<Person> getParent(Person person){ |
| 45 | + return parentMap.get(person.id); |
| 46 | + } |
| 47 | + |
| 48 | + public Set<Person> getGreatParent(Person person){ |
| 49 | + Set<Person> result = new HashSet<>(); |
| 50 | + Set<Person> parents = parentMap.get(person.id); |
| 51 | + if(parents.isEmpty()) |
| 52 | + return result; |
| 53 | + for(Person parent1 : parents){ |
| 54 | + Set<Person> parent2 = parentMap.get(parent1.id); |
| 55 | + result.addAll(parent2); |
| 56 | + } |
| 57 | + return result; |
| 58 | + } |
| 59 | + |
| 60 | + public List<Person> getChildren(Person person){ |
| 61 | + return person.children; |
| 62 | + } |
| 63 | + |
| 64 | + public Person findPerson(Person current , Person target){ |
| 65 | + if(current == null) |
| 66 | + return null; |
| 67 | + if(current.id == target.id) |
| 68 | + return current; |
| 69 | + |
| 70 | + for(Person child : current.children){ |
| 71 | + Person foundChild = findPerson(child , target); |
| 72 | + if(foundChild != null) |
| 73 | + return foundChild; |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + public List<Person> findPerson(Person target){ |
| 79 | + System.out.println( |
| 80 | + findPerson(root,target) |
| 81 | + ); |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + public static class Person{ |
| 86 | + public String name; |
| 87 | + public int id; |
| 88 | + public List<Person> children; |
| 89 | + public Person partner; |
| 90 | + |
| 91 | + public Person(int id,String n){ |
| 92 | + this.id = id; |
| 93 | + this.name= n; |
| 94 | + children = new ArrayList<>(); |
| 95 | + } |
| 96 | + |
| 97 | + public void setPartner(Person p){ |
| 98 | + this.partner = p; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String toString(){ |
| 103 | + return "Person{"+ |
| 104 | + "name='"+name+'\''+ |
| 105 | + ", id="+id+ |
| 106 | + '}'; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public static void main(String[] args){ |
| 111 | + Person root = new Person(0,"a"); |
| 112 | + Person root1 = new Person(01,"a1"); |
| 113 | + |
| 114 | + root.setPartner(root1); |
| 115 | + root1.setPartner(root); |
| 116 | + |
| 117 | + Person p1 = new Person(1,"b"); |
| 118 | + Person p2 = new Person(2,"c"); |
| 119 | + Person p3 = new Person(3,"d"); |
| 120 | + |
| 121 | + p1.setPartner(p3); |
| 122 | + p3.setPartner(p1); |
| 123 | + |
| 124 | + Person p4 = new Person(4,"e"); |
| 125 | + Person p5 = new Person(5,"f"); |
| 126 | + Person p6 = new Person(6,"g"); |
| 127 | + |
| 128 | + root.children.add(p1); |
| 129 | + root.children.add(p2); |
| 130 | + |
| 131 | + p1.children.add(p4); |
| 132 | + p2.children.add(p5); |
| 133 | + p3.children.add(p6); |
| 134 | + |
| 135 | + FamilyTree2 tree = new FamilyTree2(root); |
| 136 | + |
| 137 | + System.out.println( |
| 138 | + tree.getParent(p4) |
| 139 | + ); |
| 140 | + } |
| 141 | +} |
0 commit comments