Assignment 8: bst init

This commit is contained in:
Yuri Tatishchev 2025-03-31 20:23:41 -07:00
parent afb273d714
commit 99055cd188
Signed by: CaZzzer
GPG Key ID: E0EBF441EA424369
13 changed files with 434 additions and 0 deletions

8
08-labBST/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

102
08-labBST/Assignment.java Normal file
View File

@ -0,0 +1,102 @@
package labBST;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class Node {
int val;
Node left;
Node right;
Node parent;
}
public class Assignment {
public enum TraversalOrder {
InOrderTrav, PreOrderTrav, PostOrderTrav
}
public static class BST {
private Node root;
public BST() {
root = null;
}
public void insert(int toInsert) {
// Insert the provided element into the tree
}
public void delete(int toDelete) {
// Delete the provided element from the tree
}
public void print(TraversalOrder order) {
switch (order) {
case InOrderTrav:
inOrder(root);
break;
case PreOrderTrav:
preOrder(root);
break;
case PostOrderTrav:
postOrder(root);
break;
}
}
private void inOrder(Node curr) {
// Print the elements in the tree one one each line
System.out.println(curr.val);
}
private void preOrder(Node curr) {
// Print the elements in the tree one one each line
System.out.println(curr.val);
}
private void postOrder(Node curr) {
// Print the elements in the tree one one each line
System.out.println(curr.val);
}
}
public static void run(String inputPath) {
try (BufferedReader br = new BufferedReader(new FileReader(inputPath))) {
BST bst = new BST();
while (true) {
String instruction = br.readLine().trim();
if (instruction.length() < 1 || instruction.charAt(0) == 'e') {
break;
} else if (instruction.charAt(0) == 'i') {
int element = Integer.parseInt(instruction.substring(1));
bst.insert(element);
} else if (instruction.charAt(0) == 'd') {
int element = Integer.parseInt(instruction.substring(1));
bst.delete(element);
} else if (instruction.charAt(0) == 'o') {
String orderType = instruction.substring(1);
if (orderType.equals("in")) {
bst.print(TraversalOrder.InOrderTrav);
} else if (orderType.equals("pre")) {
bst.print(TraversalOrder.PreOrderTrav);
} else if (orderType.equals("post")) {
bst.print(TraversalOrder.PostOrderTrav);
} else {
System.out.println("Unknown Traversal Type: " + orderType);
}
} else {
System.out.println("Unknown instruction: " + instruction);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
run("");
}
}

103
08-labBST/TestAll.java Normal file
View File

@ -0,0 +1,103 @@
package labBST;
import java.io.File;
import java.net.URL;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintStream;
import java.io.ByteArrayOutputStream;
public class TestAll {
public static void main(String[] args) {
processInputFiles();
}
public static void processInputFiles() {
try {
// Get the inputs and outputs directories
URL input_url = TestAll.class.getResource("inputs");
URL output_url = TestAll.class.getResource("outputs");
if (input_url == null) {
System.out.println("Cannot find folder \"inputs\"");
return;
}
if (output_url == null) {
System.out.println("Cannot find folder \"outputs\"");
return;
}
// Get the names of all the files in either directory
File input_folder = new File(input_url.getPath());
File[] input_files = input_folder.listFiles();
File output_folder = new File(output_url.getPath());
if (input_files == null) {
System.out.printf("No files in folder: %s\n", input_folder.getPath());
return;
}
// No need to check if output files exist, can still run inputs
// Capture the print statements from the assignment into printstream
ByteArrayOutputStream captured_bytestream = new ByteArrayOutputStream();
PrintStream captured_printstream = new PrintStream(captured_bytestream);
// Replace System.out with a local variable to capture prints
PrintStream stdout = System.out;
System.setOut(captured_printstream);
Integer total_tests = 0, passed_tests = 0;
for (File input_file : input_files) {
if (!input_file.isFile()) {
continue;
}
// Print which test is currently being run
stdout.println("Processing file: " + input_file.getName());
// Run the assignment on this input file
Assignment.run(input_file.getAbsolutePath());
// Ensure all the output from assignment goes to local variable
System.out.flush();
// Get the output from assignment into a String
String assignment_output = captured_bytestream.toString();
captured_bytestream.reset();
// Get the file contents for comparison
File output_file = new File(output_folder, input_file.getName());
// Check if a corresponding output file exists
if (!output_file.exists()) {
// Print the output to the console without grading
stdout.println("No corresponding output, result shown:");
stdout.println(assignment_output);
continue;
}
// Read the contents of the corresponding output file
total_tests++;
BufferedReader buffered_reader = new BufferedReader(new FileReader(output_file.getAbsolutePath()));
StringBuilder string_builder = new StringBuilder();
String line;
while ((line = buffered_reader.readLine()) != null) {
string_builder.append(line).append(System.lineSeparator());
}
buffered_reader.close();
String correct_output = string_builder.toString();
if (assignment_output.equals(correct_output)) {
// This test passes
passed_tests++;
stdout.println("Test Pass");
} else {
// This test does not pass
stdout.println("Test Fail:");
stdout.println("Expected:");
stdout.println(correct_output);
stdout.println("Actual:");
stdout.println(assignment_output);
}
// Indicate this test is done
stdout.println("=====");
}
stdout.printf("Result: %d/%d Tests Passed\n", passed_tests, total_tests);
System.setOut(stdout);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,14 @@
i5
i3
i2
i7
i9
i8
i6
i1
i4
oin
opre
opost
e

View File

@ -0,0 +1,18 @@
i8
i4
i6
i2
i15
i40
i30
i17
i9
i11
i12
i14
i21
oin
opre
opost
e

View File

@ -0,0 +1,16 @@
i5
i3
i4
i1
i8
i9
oin
d8
i11
i2
d3
d4
opost
opre
e

View File

@ -0,0 +1,19 @@
i15
i7
i8
i5
i11
i9
i14
i3
i4
i1
opre
d15
d4
opre
i2
i6
d8
opre
e

View File

@ -0,0 +1,20 @@
i15
i7
i8
i5
i11
i9
opre
i14
i3
i4
i1
d15
d4
i4
opre
i2
i6
d8
opre
e

View File

@ -0,0 +1,27 @@
1
2
3
4
5
6
7
8
9
5
3
2
1
4
7
6
9
8
1
2
4
3
6
8
9
7
5

View File

@ -0,0 +1,39 @@
2
4
6
8
9
11
12
14
15
17
21
30
40
8
4
2
6
15
9
11
12
14
40
30
17
21
2
6
4
14
12
11
9
21
17
30
40
15
8

View File

@ -0,0 +1,16 @@
1
3
4
5
8
9
2
1
11
9
5
5
1
2
9
11

View File

@ -0,0 +1,27 @@
15
7
5
3
1
4
8
11
9
14
7
5
3
1
8
11
9
14
7
5
3
1
2
6
11
9
14

View File

@ -0,0 +1,25 @@
15
7
5
8
11
9
7
5
3
1
4
8
11
9
14
7
5
3
1
2
4
6
11
9
14