import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.lang.*; public class DSAP3 { static ArrayList> data = new ArrayList>(); public static class Node { static int id; static int val; static ArrayList app = new ArrayList(); private void add (Node mag) { app.add(mag); } private Node getChild (int pos) { return app.get(pos); } } public static void main(String[] args) { //read txt file BufferedReader br = null; try { int h = 0; //read in file String sCurrentLine; String file_path = args[0]; br = new BufferedReader(new FileReader(file_path)); h = Integer.parseInt(br.readLine()); //read lines int i = 0; ArrayList apps = new ArrayList(); String str; String[] stra; while ((sCurrentLine = br.readLine()) != null) { str = sCurrentLine.replaceAll("\\D+"," "); str = str + " x"; stra = str.split(" +"); while (!(stra[i]).equals("x")) { apps.add(Integer.parseInt(stra[i])); i++; } data.add(apps); apps = new ArrayList(); i = 0; } Node head = addMag(0); System.out.println(head.val); // System.out.println(head.getChild(0).val); // System.out.println(head.getChild(1).val); // System.out.println(head.getChild(2).val); // System.out.println(head.getChild(1).getChild(1).val); } catch (IOException e) { e.printStackTrace(); } } public static Node addMag(int x) { Node temp = new Node(); temp.id = data.get(x).get(0); temp.val = data.get(x).get(1); // System.out.print(temp.id + " "); // System.out.println(temp.val + " "); for (int c = 2; c < data.get(x).size(); c++) { temp.add(addMag(data.get(x).get(c)-1)); } // System.out.print(temp.id + " "); // System.out.println(temp.val + " "); // System.out.print("\n"); return temp; } }