Skip to content

Instantly share code, notes, and snippets.

@danmux
Created November 25, 2015 01:40
Show Gist options
  • Save danmux/145dc39121e6b7906dcd to your computer and use it in GitHub Desktop.
Save danmux/145dc39121e6b7906dcd to your computer and use it in GitHub Desktop.
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 {
ArrayList<ArrayList<Integer>> data = new ArrayList<ArrayList<Integer>>();
public class Node {
int id;
int val;
ArrayList<Node> app = new ArrayList<Node>();
private void add (Node mag) {
app.add(mag);
}
private Node getChild (int pos) {
return app.get(pos);
}
}
public 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;
}
public void load(String file_path) {
//read txt file
BufferedReader br = null;
try {
int h = 0;
//read in file
String sCurrentLine;
br = new BufferedReader(new FileReader(file_path));
h = Integer.parseInt(br.readLine());
//read lines
int i = 0;
ArrayList<Integer> apps = new ArrayList<Integer>();
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<Integer>();
i = 0;
}
Node head = addMag(0);
System.out.println(head.id + " " + 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 void main(String[] args) {
DSAP3 dsap3 = new DSAP3();
dsap3.load(args[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment