Java Visitor Pattern

  • + 1 comment

    class SumInLeavesVisitor extends TreeVis { public int getResult() { //implement this return leavesValuesCounter; }

    public void visitNode(TreeNode node) {
        //implement this
    }
    
    public void visitLeaf(TreeLeaf leaf) {
        //implement this
    
        if (leaf == null) {
            return;
        }
    
        leavesValuesCounter += leaf.getValue();
    }
    

    // private private int leavesValuesCounter = 0; }

    class ProductOfRedNodesVisitor extends TreeVis { public int getResult() { //implement this return productOfRedNodesCounter; }

    public void visitNode(TreeNode node) {
        //implement this
        if (node == null)
            return;
    
        if (node.getColor() == Color.RED) {
            int value = node.getValue();
            value = (value == 0) ? 1 : value;
    
            productOfRedNodesCounter = (int)(((long)productOfRedNodesCounter * value) % MODULE);
        }
    }
    
    public void visitLeaf(TreeLeaf leaf) {
        //implement this
    
        if (leaf == null)
            return;
    
        if (leaf.getColor() == Color.RED) {
            int value = leaf.getValue();
            value = (value == 0) ? 1 : value;
    
            productOfRedNodesCounter = (int)(((long)productOfRedNodesCounter * value) % MODULE);
        }
    }
    
    private int productOfRedNodesCounter = 1;
    final int MODULE = 1000000007;
    

    }

    class FancyVisitor extends TreeVis { public int getResult() { //implement this return Math.abs(sumGreenLeafNodes - sumNonLeafNodes); }

    public void visitNode(TreeNode node) {
        //implement this
        if (node == null)
            return;
    
        if (node.getDepth() % 2 == 0) {
            sumNonLeafNodes += node.getValue();
        }
    }
    
    public void visitLeaf(TreeLeaf leaf) {
        //implement this
    
        if (leaf == null)
            return;
    
        if (leaf.getColor() == Color.GREEN) {
            sumGreenLeafNodes += leaf.getValue();
        }
    }
    
    private int sumNonLeafNodes = 0;
    private int sumGreenLeafNodes = 0;
    

    }

    public class Solution {

    private static boolean isALeaf(List<List<Integer>> adjacencyTable,
                                boolean[] visited, int elementPosition) {
        if (adjacencyTable.get(elementPosition).isEmpty()) {
            return true;
        }
    
        for (int i = 0; i < adjacencyTable.get(elementPosition).size(); i++) {
            Integer recordedValue = adjacencyTable.get(elementPosition).get(i);
            if (!visited[recordedValue]) {
                return false;
            }
        }
    
        return true;
    }
    
    
    private static Tree createTheTree(List<List<Integer>> adjacencyTable,
                                     boolean[] visited, int[] nodeCosts, int[] colors, int parentDepth,
                                     int elementPosition) {
    
        int localDepth = parentDepth + 1;
        if (isALeaf(adjacencyTable, visited, elementPosition)) {
            return new TreeLeaf(nodeCosts[elementPosition],
                    colors[elementPosition] == 1 ? Color.GREEN : Color.RED,
                    localDepth );
        }
    
        TreeNode currentNode = new TreeNode(nodeCosts[elementPosition],
                colors[elementPosition] == 1 ? Color.GREEN : Color.RED,
                localDepth);
    
        visited[elementPosition] = true;
    
        for (int i = 0; i < adjacencyTable.get(elementPosition).size(); i++) {
            Integer recordedValue = adjacencyTable.get(elementPosition).get(i);
            if (!visited[recordedValue]) {
                Tree child = createTheTree(adjacencyTable, visited, nodeCosts, colors,
                        localDepth, recordedValue);
                currentNode.addChild(child);
            }
        }
    
        return currentNode;
    }
    
    
    public static Tree solve() {
        //read the tree from STDIN and return its root as a return value of this function
        Scanner myObj = new Scanner(System.in);
        int nodesCount = myObj.nextInt();
    
        int[] nodeCosts = new int[nodesCount];
        int[] colors = new int[nodesCount];
    
        for (int i = 0; i < nodesCount; i++) {
            nodeCosts[i] = myObj.nextInt();
        }
    
        for (int i = 0; i < nodesCount; i++) {
            colors[i] = myObj.nextInt();
        }
    
        List<List<Integer>> adjacencyTable = new ArrayList<>(nodesCount);
        for (int i = 0; i < nodesCount; i++) {
            adjacencyTable.add(new ArrayList<Integer>());
        }
    
        boolean[] visited = new boolean[nodesCount];
    
        int decrement = nodesCount - 1;
        while (decrement > 0) {
            int u = myObj.nextInt();
            int v = myObj.nextInt();
    
            adjacencyTable.get(u - 1).add(v - 1);
            adjacencyTable.get(v - 1).add(u - 1);
    
            decrement--;
        }
    
        Tree result = createTheTree(adjacencyTable, visited, nodeCosts, colors, -1, 0);
        return result;
    }