Skip to content

Commit c5e73af

Browse files
authored
Merge pull request #26 from clormor/visitor
adding solution to visitor problem
2 parents 337d147 + 86ba50f commit c5e73af

File tree

15 files changed

+522
-0
lines changed

15 files changed

+522
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.github.clormor.hackerrank.advanced.visitor;
2+
3+
enum Color {
4+
RED, GREEN
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.clormor.hackerrank.advanced.visitor;
2+
3+
public class FancyVisitor extends TreeVis {
4+
5+
int greenLeafSum = 0;
6+
int evenDepthNodeSum = 0;
7+
8+
public int getResult() {
9+
return Math.abs(greenLeafSum - evenDepthNodeSum);
10+
}
11+
12+
public void visitNode(TreeNode node) {
13+
if (node.getDepth() % 2 == 0) {
14+
evenDepthNodeSum += node.getValue();
15+
}
16+
}
17+
18+
public void visitLeaf(TreeLeaf leaf) {
19+
if (leaf.getColor().equals(Color.GREEN)) {
20+
greenLeafSum += leaf.getValue();
21+
}
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.clormor.hackerrank.advanced.visitor;
2+
3+
import java.math.BigInteger;
4+
5+
public class ProductOfRedNodesVisitor extends TreeVis {
6+
7+
BigInteger result = new BigInteger("1");
8+
static final BigInteger BIG_INT_MOD = new BigInteger(
9+
Integer.toString((int) Math.pow(10, 9) + 7));
10+
11+
public int getResult() {
12+
return result.mod(BIG_INT_MOD).intValue();
13+
}
14+
15+
public void visitNode(TreeNode node) {
16+
processIfRed(node);
17+
}
18+
19+
public void visitLeaf(TreeLeaf leaf) {
20+
processIfRed(leaf);
21+
}
22+
23+
private void processIfRed(Tree t) {
24+
if (t.getColor().equals(Color.RED)) {
25+
result = result.multiply(new BigInteger(Integer.toString(t.getValue())));
26+
}
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.clormor.hackerrank.advanced.visitor;
2+
3+
public class SumInLeavesVisitor extends TreeVis {
4+
5+
int result = 0;
6+
7+
@Override
8+
public int getResult() {
9+
return result;
10+
}
11+
12+
@Override
13+
public void visitNode(TreeNode node) {
14+
// Only sum leaves, do nothing here.
15+
}
16+
17+
@Override
18+
public void visitLeaf(TreeLeaf leaf) {
19+
result += leaf.getValue();
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.clormor.hackerrank.advanced.visitor;
2+
3+
abstract class Tree {
4+
5+
private int value;
6+
private Color color;
7+
private int depth;
8+
9+
public Tree(int value, Color color, int depth) {
10+
this.value = value;
11+
this.color = color;
12+
this.depth = depth;
13+
}
14+
15+
public int getValue() {
16+
return value;
17+
}
18+
19+
public Color getColor() {
20+
return color;
21+
}
22+
23+
public int getDepth() {
24+
return depth;
25+
}
26+
27+
public abstract void accept(TreeVis visitor);
28+
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.clormor.hackerrank.advanced.visitor;
2+
3+
class TreeLeaf extends Tree {
4+
5+
public TreeLeaf(int value, Color color, int depth) {
6+
super(value, color, depth);
7+
}
8+
9+
public void accept(TreeVis visitor) {
10+
visitor.visitLeaf(this);
11+
}
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.clormor.hackerrank.advanced.visitor;
2+
3+
import java.util.ArrayList;
4+
5+
class TreeNode extends Tree {
6+
7+
private ArrayList<Tree> children = new ArrayList<>();
8+
9+
public TreeNode(int value, Color color, int depth) {
10+
super(value, color, depth);
11+
}
12+
13+
public void accept(TreeVis visitor) {
14+
visitor.visitNode(this);
15+
16+
for (Tree child : children) {
17+
child.accept(visitor);
18+
}
19+
}
20+
21+
public void addChild(Tree child) {
22+
children.add(child);
23+
}
24+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package io.github.clormor.hackerrank.advanced.visitor;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class TreeParser {
8+
9+
Map<Integer, Integer> values;
10+
Map<Integer, Color> colours;
11+
Map<Integer, Integer> depths;
12+
Map<Integer, Integer> edges;
13+
Map<Integer, Tree> trees;
14+
15+
public Tree solve(int n, String[] args) {
16+
// map the values and colours in helper methods
17+
mapValues(n, args[0]);
18+
mapColours(n, args[1]);
19+
20+
// keep track of max depth - determines if a tree is node or leaf
21+
int maxDepth = mapDepthsAndEdges(n, Arrays.copyOfRange(args, 2, n + 1));
22+
23+
// Create the tree objects
24+
createTrees(n, maxDepth);
25+
26+
// Create edges between trees
27+
createEdges(n);
28+
29+
// return the tree root
30+
return trees.get(0);
31+
}
32+
33+
private void mapValues(int n, String valuesLine) {
34+
values = new HashMap<>(n);
35+
String[] valuesString = valuesLine.split(" ");
36+
for (int i = 0; i < n; i++) {
37+
values.put(i, Integer.parseInt(valuesString[i]));
38+
}
39+
}
40+
41+
private void mapColours(int n, String coloursLine) {
42+
colours = new HashMap<>(n);
43+
String[] coloursString = coloursLine.split(" ");
44+
for (int i = 0; i < n; i++) {
45+
colours.put(i, (coloursString[i].equals("0")) ? Color.RED : Color.GREEN);
46+
}
47+
}
48+
49+
private int mapDepthsAndEdges(int n, String[] edgeLines) {
50+
int maxDepth = 0;
51+
depths = new HashMap<>(n);
52+
edges = new HashMap<>(n);
53+
54+
// Determine the depth of each node, and its edges
55+
depths.put(0, 0);
56+
for (int i = 0; i < n - 1; i++) {
57+
String[] edgeString = edgeLines[i].split(" ");
58+
int parent = Integer.parseInt(edgeString[0]) - 1;
59+
int child = Integer.parseInt(edgeString[1]) - 1;
60+
61+
int parentDepth = depths.get(parent);
62+
maxDepth = Math.max(maxDepth, parentDepth + 1);
63+
depths.put(child, parentDepth + 1);
64+
edges.put(child, parent);
65+
}
66+
67+
return maxDepth;
68+
}
69+
70+
private void createTrees(int n, int maxDepth) {
71+
trees = new HashMap<>(n);
72+
73+
for (int i = 0; i < n; i++) {
74+
// determine if node or leaf
75+
int depth = depths.get(i);
76+
if (depths.get(i) == maxDepth) {
77+
trees.put(i, new TreeLeaf(values.get(i), colours.get(i), depth));
78+
} else {
79+
trees.put(i, new TreeNode(values.get(i), colours.get(i), depth));
80+
}
81+
}
82+
}
83+
84+
private void createEdges(int n) {
85+
for (int i = 0; i < n; i++) {
86+
// find edges, add this tree to its parent
87+
int parent = edges.getOrDefault(i, -1);
88+
if (parent != -1) {
89+
TreeNode parentNode = (TreeNode) trees.get(parent);
90+
Tree child = trees.get(i);
91+
parentNode.addChild(child);
92+
}
93+
}
94+
}
95+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.clormor.hackerrank.advanced.visitor;
2+
3+
public abstract class TreeVis {
4+
5+
public abstract int getResult();
6+
7+
public abstract void visitNode(TreeNode node);
8+
9+
public abstract void visitLeaf(TreeLeaf leaf);
10+
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.clormor.hackerrank.advanced.visitor;
2+
3+
/**
4+
* For testing - Sums all node values to check we've visited the ones we expected to.
5+
*/
6+
public class SimpleCountTreeVisitor extends TreeVis {
7+
int result = 0;
8+
9+
@Override
10+
public int getResult() {
11+
return result;
12+
}
13+
14+
@Override
15+
public void visitNode(TreeNode node) {
16+
result += node.getValue();
17+
}
18+
19+
@Override
20+
public void visitLeaf(TreeLeaf leaf) {
21+
result += leaf.getValue();
22+
}
23+
}

0 commit comments

Comments
 (0)