Advertisement
YChalk

Java Visitor Pattern

Feb 25th, 2023
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. public static Tree solve() {
  2.         //read the tree from STDIN and return its root as a return value of this function
  3.         //List to contain all nodes/leaves
  4.         List<Tree> trees = new ArrayList<>();
  5.  
  6.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
  7.             //read in the amount of nodes/leaves
  8.             int t = Integer.parseInt(reader.readLine());
  9.  
  10.             //debug
  11.             System.out.println(t);
  12.  
  13.             //arrays for containing all the values/colours of each node/leaf; there will be 't' amount of values
  14.             int[] values = new int[t];
  15.             int[] colors = new int[t];
  16.  
  17.  
  18.  
  19.             //read the values/colours and then split them
  20.             String[] valueInput = reader.readLine().split("");
  21.             String[] colorInout = reader.readLine().split("");
  22.  
  23.             //convert the arrays of Strings of colours/values to int values and store them in the int[] arrays
  24.             for (int i = 0; i < t; i++) {
  25.                 values[i] = Integer.parseInt(valueInput[i]);
  26.                 colors[i] = Integer.parseInt(colorInout[i]);
  27.             }
  28.  
  29.             //debug
  30.             System.out.println(values);
  31.             System.out.println(colors);
  32.  
  33.             //read in the edges and store them in a List
  34.             List<String> edgeInputs = new ArrayList<>();
  35.  
  36.             String edge;
  37.  
  38.             while(!(edge = reader.readLine()).equals("")) {
  39.                 edgeInputs.add(edge);
  40.             }
  41.  
  42.             //split each edge into its component parts, store the parent in one List, and store the child in another List
  43.             //both should be at the same index in their relevant List
  44.             //store both Lists in a List<List<String>>
  45.             List<List<Integer>> edges = new ArrayList<>();
  46.             edges.add(new ArrayList<>());
  47.             edges.add(new ArrayList<>());
  48.  
  49.             edgeInputs.forEach(s -> {
  50.                 String[] nodes = s.split(" ");
  51.                 edges.get(0).add(Integer.parseInt(nodes[0]));
  52.                 edges.get(1).add(Integer.parseInt(nodes[1]));
  53.             });
  54.  
  55.             //checks if the root is a node or leaf, creates the correct tree object, and adds it to the List
  56.             if (isLeaf(1, edges)) {
  57.                 trees.add(new TreeLeaf(values[0], color(colors[0]), 0));
  58.             } else {
  59.                 trees.add(new TreeNode(values[0], color(colors[0]), 0));
  60.             }
  61.  
  62.             //iterates over the remaining values
  63.             //determines if they're nodes or leaves, and creates a corresponding Tree object
  64.             //finds the parent from the List of created Trees, adds the new Tree to the parent's List of children
  65.             //adds the new Tree to the List
  66.             for (int i = 1; i < values.length; i++) {
  67.                 Tree tree;
  68.                 int index = 1 + 1;
  69.                 TreeNode parent = (TreeNode) trees.get(getParentIndex(index, edges));
  70.                 int depth = parent.getDepth() + 1;
  71.  
  72.                 if (isLeaf(index, edges)) {
  73.                     tree = new TreeLeaf(values[i], color(colors[i]), depth);
  74.                 } else {
  75.                     tree = new TreeNode(values[i], color(colors[i]), depth);
  76.                 }
  77.  
  78.                 parent.addChild(tree);
  79.                 trees.add(tree);
  80.             }
  81.         } catch (Exception e) {}
  82.  
  83.         return trees.get(0);
  84.     }
  85.  
  86.     //checks if a Tree is a leaf
  87.     //searches for its index in the List containing parents
  88.     //if it doesn't appear, then it must be a leaf, not a node, return true
  89.     public static boolean isLeaf(int index, List<List<Integer>> edges) {
  90.         return !edges.get(0).contains(index);
  91.     }
  92.  
  93.     //generates the Color based on the value
  94.     //0 == Red / 1 == Green
  95.     public static Color color(int color) {
  96.         return color == 0 ? Color.RED : Color.GREEN;
  97.     }
  98.  
  99.     //finds where a node/leaf's parent is in the List containing parents
  100.     //by searching for
  101.     public static int getParentIndex(int index, List<List<Integer>> edges) {
  102.         int i = edges.get(1).indexOf(index);
  103.         return edges.get(0).get(i) - 1;
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement