From cec91ea4bfbd6797740677adcfc1efdc5c7ab655 Mon Sep 17 00:00:00 2001 From: Izay Ansari Date: Mon, 6 Oct 2025 23:33:49 -0400 Subject: [PATCH 1/2] implemented loadtriangle method: --- src/main/java/NumberTriangle.java | 36 +++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java index 30e10ecb..3358e807 100644 --- a/src/main/java/NumberTriangle.java +++ b/src/main/java/NumberTriangle.java @@ -88,9 +88,7 @@ public boolean isLeaf() { * */ public int retrieve(String path) { - // TODO implement this method - return -1; - } + /** Read in the NumberTriangle structure from a file. * @@ -110,19 +108,45 @@ 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 + List> rows = new ArrayList<>(); // will need to return the top of the NumberTriangle, // so might want a variable for that. NumberTriangle top = null; String line = br.readLine(); + int rowNum = 0; + 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[] numbers = line.split("\\s+"); + List currentRow = new ArrayList<>(); + + for (String numStr : numbers) { + int value = Integer.parseInt(numStr); + currentRow.add(new NumberTriangle(value)); + } + rows.add(currentRow); + + if (rowNum == 0) { + top = currentRow.get(0); + } + + if (rowNum > 0) { + List prevRow = rows.get(rowNum - 1); + for (int i = 0; i < prevRow.size(); i++) { + NumberTriangle parent = prevRow.get(i); + parent.setLeft(currentRow.get(i)); + parent.setRight(currentRow.get(i + 1)); + } + } + + rowNum++; + } //read the next line line = br.readLine(); From ec7dc78e009619665d2b97c9ed5b7b52f615b854 Mon Sep 17 00:00:00 2001 From: Izay Ansari Date: Mon, 6 Oct 2025 23:41:56 -0400 Subject: [PATCH 2/2] implemented retrieve method --- src/main/java/NumberTriangle.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java index 3358e807..6c4f8558 100644 --- a/src/main/java/NumberTriangle.java +++ b/src/main/java/NumberTriangle.java @@ -88,7 +88,16 @@ public boolean isLeaf() { * */ public int retrieve(String path) { - + NumberTriangle current = this; + for (int i = 0; i < path.length(); i++) { + if (path.charAt(i) == 'l') { + current = current.left; + } else if (path.charAt(i) == 'r') { + current = current.right; + } + } + return current.getRoot(); + } /** Read in the NumberTriangle structure from a file. *