diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java index 30e10ecb..a71e53f4 100644 --- a/src/main/java/NumberTriangle.java +++ b/src/main/java/NumberTriangle.java @@ -1,4 +1,6 @@ import java.io.*; +import java.util.ArrayList; +import java.util.List; /** * This is the provided NumberTriangle class to be used in this coding task. @@ -88,7 +90,18 @@ public boolean isLeaf() { * */ public int retrieve(String path) { - // TODO implement this method + if (path.isEmpty()) { + return this.root; + } + char direction = path.charAt(0); + if ( direction == 'l' && this.left != null) { + return this.left.retrieve(path.substring(1)); + } + + else if (direction == 'r' && this.right != null) { + return this.right.retrieve(path.substring(1)); + } + return -1; } @@ -110,19 +123,37 @@ public static NumberTriangle loadTriangle(String fname) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); - // TODO define any variables that you want to use to store things - // will need to return the top of the NumberTriangle, // so might want a variable for that. NumberTriangle top = null; + List prevRow = new ArrayList(); String line = br.readLine(); while (line != null) { - // remove when done; this line is included so running starter code prints the contents of the file - System.out.println(line); - - // TODO process the line + line = line.trim(); + + if (!line.isEmpty()) { + String[] nums = line.split("\\s+"); + List curRow = new ArrayList<>(); + + for (int i = 0; i < nums.length; i++) { + if (!nums[i].equals("")) { + int value = Integer.parseInt(nums[i]); + curRow.add(new NumberTriangle(value)); + } + } + + if (top == null) { + top = curRow.get(0); + } else { + for (int j = 0; j < prevRow.size(); j++) { + prevRow.get(j).setLeft(curRow.get(j)); + prevRow.get(j).setRight(curRow.get(j + 1)); + } + } + prevRow = curRow; + } //read the next line line = br.readLine();