|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +class Tree { |
| 4 | + private final String label; |
| 5 | + private final List<Tree> children; |
| 6 | + private Map<String, List<String>> graph; |
| 7 | + |
| 8 | + public Tree(String label) { |
| 9 | + this(label, new ArrayList<>()); |
| 10 | + } |
| 11 | + |
| 12 | + public Tree(String label, List<Tree> children) { |
| 13 | + this.label = label; |
| 14 | + this.children = children; |
| 15 | + } |
| 16 | + |
| 17 | + public static Tree of(String label) { |
| 18 | + return new Tree(label); |
| 19 | + } |
| 20 | + |
| 21 | + public static Tree of(String label, List<Tree> children) { |
| 22 | + return new Tree(label, children); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public boolean equals(Object o) { |
| 27 | + if (this == o) { |
| 28 | + return true; |
| 29 | + } |
| 30 | + if (o == null || getClass() != o.getClass()) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + Tree tree = (Tree) o; |
| 34 | + return label.equals(tree.label) |
| 35 | + && children.size() == tree.children.size() |
| 36 | + && children.containsAll(tree.children) |
| 37 | + && tree.children.containsAll(children); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public int hashCode() { |
| 42 | + return Objects.hash(label, children); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String toString() { |
| 47 | + return "Tree{" + label + |
| 48 | + ", " + children + |
| 49 | + "}"; |
| 50 | + } |
| 51 | + |
| 52 | + public Tree fromPov(String fromNode) { |
| 53 | + if (this.graph == null) { |
| 54 | + this.graph = new HashMap<>(); |
| 55 | + convertToGraph(this.label, this.children); |
| 56 | + } |
| 57 | + |
| 58 | + if (!this.graph.containsKey(fromNode)) { |
| 59 | + throw new UnsupportedOperationException("Tree could not be reoriented"); |
| 60 | + } |
| 61 | + |
| 62 | + return reconstructTreeFromNode(fromNode, "emptyParentForRoot"); |
| 63 | + } |
| 64 | + |
| 65 | + public List<String> pathTo(String fromNode, String toNode) { |
| 66 | + if (this.graph == null) { |
| 67 | + this.graph = new HashMap<>(); |
| 68 | + convertToGraph(this.label, this.children); |
| 69 | + } |
| 70 | + |
| 71 | + if (!this.graph.containsKey(fromNode) || !this.graph.containsKey(toNode)) { |
| 72 | + throw new UnsupportedOperationException("No path found"); |
| 73 | + } |
| 74 | + |
| 75 | + Tree root = fromPov(fromNode); |
| 76 | + |
| 77 | + List<String> path = new ArrayList<>(); |
| 78 | + generatePath(root, toNode, path); |
| 79 | + return path; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Method to convert tree to graph recursively and populate the private class attribute graph |
| 84 | + * |
| 85 | + * @param label label String of root node |
| 86 | + * @param children children list of root node |
| 87 | + */ |
| 88 | + private void convertToGraph(String label, List<Tree> children) { |
| 89 | + this.graph.putIfAbsent(label, new ArrayList<>()); |
| 90 | + if (children != null) { |
| 91 | + for (Tree child : children) { |
| 92 | + this.graph.get(label).add(child.label); |
| 93 | + this.graph.putIfAbsent(child.label, new ArrayList<>()); |
| 94 | + this.graph.get(child.label).add(label); |
| 95 | + convertToGraph(child.label, child.children); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Method to create Tree object recursively with node as root. |
| 102 | + * It utilizes the previously populated class attribute graph |
| 103 | + * |
| 104 | + * @param node string label of current node |
| 105 | + * @param parent string label of parent node of current node |
| 106 | + * @return Tree object of root node |
| 107 | + */ |
| 108 | + private Tree reconstructTreeFromNode(String node, String parent) { |
| 109 | + List<Tree> children = new ArrayList<>(); |
| 110 | + for (String nbr : this.graph.get(node)) { |
| 111 | + if (!nbr.equals(parent)) { |
| 112 | + children.add(reconstructTreeFromNode(nbr, node)); |
| 113 | + } |
| 114 | + } |
| 115 | + return Tree.of(node, children); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Method to traverse the tree and populate the path object |
| 120 | + * |
| 121 | + * @param node Root node Tree object |
| 122 | + * @param target String label of target node |
| 123 | + * @param path List of String, to be populated |
| 124 | + * @return boolean value representing if path was found in the subtree |
| 125 | + */ |
| 126 | + private boolean generatePath(Tree node, String target, List<String> path) { |
| 127 | + path.add(node.label); |
| 128 | + if (node.label.equals(target)) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + |
| 132 | + for (Tree child : node.children) { |
| 133 | + if (generatePath(child, target, path)) { |
| 134 | + return true; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + path.remove(path.size() - 1); |
| 139 | + return false; |
| 140 | + } |
| 141 | +} |
0 commit comments