diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000..aa00ffab --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..d917c349 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java index 30e10ecb..5c8defbd 100644 --- a/src/main/java/NumberTriangle.java +++ b/src/main/java/NumberTriangle.java @@ -88,8 +88,19 @@ public boolean isLeaf() { * */ public int retrieve(String path) { - // TODO implement this method - return -1; + // Start the traversal from the current node (`this`). + NumberTriangle currentNode = this; + + // Iterate through each character in the path string. + for (char direction : path.toCharArray()) { + if (direction == 'l') { + currentNode = currentNode.left; + } else if (direction == 'r') { + currentNode = currentNode.right; + } + } + // After following the path, return the root value of the final node. + return currentNode.getRoot(); } /** Read in the NumberTriangle structure from a file. @@ -104,27 +115,62 @@ public int retrieve(String path) { * @throws IOException may naturally occur if an issue reading the file occurs */ public static NumberTriangle loadTriangle(String fname) throws IOException { - // open the file and get a BufferedReader object whose methods - // are more convenient to work with when reading the file contents. - InputStream inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream(fname); - BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); + // Use a list to keep track of the nodes in the previous row we processed. + java.util.List previousRow = new java.util.ArrayList<>(); + NumberTriangle top = null; + // open the file and get a BufferedReader object + InputStream inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream(fname); - // TODO define any variables that you want to use to store things + // --- START OF THE FIX --- + // Add a check to ensure the file was actually found. + if (inputStream == null) { + // If not found, throw an exception with a helpful message. + throw new FileNotFoundException("Resource file not found: " + fname); + } + // --- END OF THE FIX --- - // will need to return the top of the NumberTriangle, - // so might want a variable for that. - NumberTriangle top = null; + BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); 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 - - //read the next line + // A list to store the nodes for the current row we are building. + java.util.List currentRow = new java.util.ArrayList<>(); + String[] numbers = line.trim().split("\\s+"); + + // Create NumberTriangle objects for each number in the current line. + for (String numStr : numbers) { + if (!numStr.isEmpty()) { // Avoid errors on empty strings + int value = Integer.parseInt(numStr); + currentRow.add(new NumberTriangle(value)); + } + } + + // This logic prevents a crash if a line is blank + if (currentRow.isEmpty()) { + line = br.readLine(); + continue; // Skip to the next iteration + } + + if (top == null) { + // This is the first line of the file, so this is the top of our triangle. + top = currentRow.get(0); + } else { + // Link the nodes from the previous row to the nodes in the current row. + for (int i = 0; i < previousRow.size(); i++) { + NumberTriangle parent = previousRow.get(i); + NumberTriangle leftChild = currentRow.get(i); + NumberTriangle rightChild = currentRow.get(i + 1); + + parent.setLeft(leftChild); + parent.setRight(rightChild); + } + } + + // The current row now becomes the previous row for the next iteration. + previousRow = currentRow; + + // Read the next line line = br.readLine(); } br.close(); @@ -138,7 +184,5 @@ public static void main(String[] args) throws IOException { // [not for credit] // you can implement NumberTriangle's maxPathSum method if you want to try to solve // Problem 18 from project Euler [not for credit] - mt.maxSumPath(); - System.out.println(mt.getRoot()); } }