From a81397b19b099bca21868f3287c6cfc0c25d8ffd Mon Sep 17 00:00:00 2001 From: Shivam Bavaria Date: Mon, 6 Oct 2025 16:34:12 -0400 Subject: [PATCH 1/4] Feat: Implement loadTriangle method from file The method now reads a file line-by-line, builds the triangle structure by linking parent and child nodes, and returns the top node. Closes #1. --- src/main/java/NumberTriangle.java | 62 +++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java index 30e10ecb..4f32c3f9 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,46 @@ 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 { + // 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 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)); - - // 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; - 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) { + int value = Integer.parseInt(numStr); + currentRow.add(new NumberTriangle(value)); + } + + 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(); From 0568785c401f8b79eede8c88caaa4bdb51a3f088 Mon Sep 17 00:00:00 2001 From: Shivam Bavaria Date: Mon, 6 Oct 2025 16:48:35 -0400 Subject: [PATCH 2/4] Fix: Prevent NullPointerException in loadTriangle The method now checks if the resource file was found and throws a FileNotFoundException if it's missing. This prevents the method from returning null when the file path is invalid. --- src/main/java/NumberTriangle.java | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java index 4f32c3f9..6333369d 100644 --- a/src/main/java/NumberTriangle.java +++ b/src/main/java/NumberTriangle.java @@ -119,9 +119,17 @@ public static NumberTriangle loadTriangle(String fname) throws IOException { java.util.List previousRow = new java.util.ArrayList<>(); NumberTriangle top = null; - // open the file and get a BufferedReader object whose methods - // are more convenient to work with when reading the file contents. + // open the file and get a BufferedReader object InputStream inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream(fname); + + // --- 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 --- + BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); String line = br.readLine(); @@ -132,8 +140,16 @@ public static NumberTriangle loadTriangle(String fname) throws IOException { // Create NumberTriangle objects for each number in the current line. for (String numStr : numbers) { - int value = Integer.parseInt(numStr); - currentRow.add(new NumberTriangle(value)); + 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) { From 18b2fb8185a65e202b1f0f0374938cf895a622eb Mon Sep 17 00:00:00 2001 From: Shivam Bavaria Date: Mon, 6 Oct 2025 18:55:31 -0400 Subject: [PATCH 3/4] Your descriptive commit message here --- .idea/.gitignore | 3 +++ .idea/encodings.xml | 7 +++++++ .idea/misc.xml | 12 ++++++++++++ .idea/vcs.xml | 6 ++++++ 4 files changed, 28 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml 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 From f3da24fa6dc59f92863fa97d526894b0c8284f91 Mon Sep 17 00:00:00 2001 From: Shivam Bavaria Date: Mon, 6 Oct 2025 19:15:02 -0400 Subject: [PATCH 4/4] message --- src/main/java/NumberTriangle.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java index 6333369d..5c8defbd 100644 --- a/src/main/java/NumberTriangle.java +++ b/src/main/java/NumberTriangle.java @@ -184,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()); } }