diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 00000000..7bc07ec2
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,10 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Environment-dependent path to Maven home directory
+/mavenHomeManager.xml
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.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..90fdf053
--- /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..4fd3ee56 100644
--- a/src/main/java/NumberTriangle.java
+++ b/src/main/java/NumberTriangle.java
@@ -88,10 +88,21 @@ public boolean isLeaf() {
*
*/
public int retrieve(String path) {
- // TODO implement this method
- return -1;
+ NumberTriangle current = this;
+ for (char c : path.toCharArray()) {
+ if (c == 'l') {
+ current = current.left;
+ } else if (c == 'r') {
+ current = current.right;
+ } else {
+ return -1;
+ }
+ if (current == null) {
+ return -1;
+ }
+ }
+ return current.root;
}
-
/** Read in the NumberTriangle structure from a file.
*
* You may assume that it is a valid format with a height of at least 1,
@@ -103,34 +114,51 @@ public int retrieve(String path) {
* @return the topmost NumberTriangle object in the NumberTriangle structure read from the specified file
* @throws IOException may naturally occur if an issue reading the file occurs
*/
+ //minor update for PR
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));
-
-
- // TODO define any variables that you want to use to store things
+ if (inputStream == null) {
+ throw new FileNotFoundException("File not found: " + fname);
+ }
- // will need to return the top of the NumberTriangle,
- // so might want a variable for that.
+ BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
+ java.util.List> levels = new java.util.ArrayList<>();
NumberTriangle top = null;
String line = br.readLine();
- while (line != null) {
+ while (line != null && !line.trim().isEmpty()) {
- // remove when done; this line is included so running starter code prints the contents of the file
- System.out.println(line);
+ String[] parts = line.trim().split("\\s+");
- // TODO process the line
+ java.util.List currentRow = new java.util.ArrayList<>();
- //read the next line
+ for (String p : parts) {
+ currentRow.add(new NumberTriangle(Integer.parseInt(p)));
+ }
+
+ if (!levels.isEmpty()) {
+ java.util.List prevRow = levels.get(levels.size() - 1);
+ for (int i = 0; i < prevRow.size(); i++) {
+ prevRow.get(i).setLeft(currentRow.get(i));
+ prevRow.get(i).setRight(currentRow.get(i + 1));
+ }
+ }
+
+ levels.add(currentRow);
line = br.readLine();
}
br.close();
+
+ if (!levels.isEmpty()) {
+ top = levels.get(0).get(0);
+ }
+
return top;
}
+
public static void main(String[] args) throws IOException {
NumberTriangle mt = NumberTriangle.loadTriangle("input_tree.txt");
@@ -142,3 +170,4 @@ public static void main(String[] args) throws IOException {
System.out.println(mt.getRoot());
}
}
+