Java Visitor Pattern

  • + 0 comments

    I reviewed some comments, and find more efficient data structure to solve whever use recursive or non recursive way. Recursive:

    static int[] values;
        static Color[] colors;
        static Map<Integer, Set<Integer>> nodeMap = new HashMap<>();
        public static Tree solve() {
            Scanner sc = new Scanner(System.in);
            int n = Integer.parseInt(sc.nextLine());
    
            values = Arrays.stream(sc.nextLine().trim().split(" ")).mapToInt(Integer::parseInt).toArray();
            colors = Arrays.stream(sc.nextLine().trim().split(" ")).mapToInt(Integer::parseInt)
                    .mapToObj(c -> c == 0 ? Color.RED : Color.GREEN).toArray(Color[]::new);
    
            for (int i = 0; i < n - 1; ++i) {
                int u = sc.nextInt() - 1;
                int v = sc.nextInt() - 1;
    
                Set<Integer> linkedNodes = nodeMap.get(u);
                if (linkedNodes == null) {
                    linkedNodes = new HashSet<>();
                }
                linkedNodes.add(v);
                nodeMap.put(u, linkedNodes);
    
                linkedNodes = nodeMap.get(v);
                if (linkedNodes == null) {
                    linkedNodes = new HashSet<>();
                }
                linkedNodes.add(u);
                nodeMap.put(v, linkedNodes);
            }
    
            Tree root = new TreeNode(values[0], colors[0], 0);
            buildTree(root, 0);
    
            sc.close();
            return root;
        }
    
        private static void buildTree(Tree parentNode, int u) {
            Set<Integer> uLinkedNodes = nodeMap.get(u);
            uLinkedNodes.forEach(v -> {
                Set<Integer> vLinkedNodes = nodeMap.get(v);
                vLinkedNodes.remove(u);
                Tree childNode = null;
                if (vLinkedNodes.isEmpty()) {
                    childNode = new TreeLeaf(values[v], colors[v], parentNode.getDepth() + 1);
                } else {
                    childNode = new TreeNode(values[v], colors[v], parentNode.getDepth() + 1);
                }
                ((TreeNode) parentNode).addChild(childNode);
                buildTree(childNode, v);
            });
        }
    

    Non recutsive:

        static Color[] colors;
        static Map<Integer, Set<Integer>> nodeMap = new HashMap<>();
        static Tree[] tree;
        public static Tree solve() {
            Scanner sc = new Scanner(System.in);
            int n = Integer.parseInt(sc.nextLine());
    
            values = Arrays.stream(sc.nextLine().trim().split(" ")).mapToInt(Integer::parseInt).toArray();
            colors = Arrays.stream(sc.nextLine().trim().split(" ")).mapToInt(Integer::parseInt)
                    .mapToObj(c -> c == 0 ? Color.RED : Color.GREEN).toArray(Color[]::new);
            tree = new Tree[n];
            
            for (int i = 0; i < n - 1; ++i) {
                int u = sc.nextInt() - 1;
                int v = sc.nextInt() - 1;
                
                Set<Integer> linkedNodes = nodeMap.get(u);
                if (linkedNodes == null) {
                    linkedNodes = new HashSet<>();
                }
                linkedNodes.add(v);
                nodeMap.put(u, linkedNodes);
                
                linkedNodes = nodeMap.get(v);
                if (linkedNodes == null) {
                    linkedNodes = new HashSet<>();
                }
                linkedNodes.add(u);
                nodeMap.put(v, linkedNodes);
            }
            
            tree[0] = new TreeNode(values[0], colors[0], 0);
            
            Set<Integer> levelSet = new HashSet<>();
            levelSet.add(0);
            Set<Integer> nextLevelSet = new HashSet<>();
            while (!levelSet.isEmpty()) {
                for (int u : levelSet) {
                    Set<Integer> uLinkedNodes = nodeMap.get(u);
                    uLinkedNodes.forEach(v -> {
                        Set<Integer> vLinkedNodes = nodeMap.get(v);
                        vLinkedNodes.remove(u);
                        if (vLinkedNodes.isEmpty()) {
                            tree[v] = new TreeLeaf(values[v], colors[v], tree[u].getDepth() + 1);
                        } else {
                            tree[v] = new TreeNode(values[v], colors[v], tree[u].getDepth() + 1);
                        }
                        ((TreeNode) tree[u]).addChild(tree[v]);
                        nextLevelSet.add(v);
                    });
                }
                levelSet.clear();
                levelSet.addAll(nextLevelSet);
                nextLevelSet.clear();
            }
    
            sc.close();
            return tree[0];
        }