Compare commits
4 Commits
afb273d714
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
ebf707276b
|
|||
|
92803f6bc5
|
|||
|
a53268bb3e
|
|||
|
99055cd188
|
8
08-labBST/.idea/.gitignore
generated
vendored
Normal file
8
08-labBST/.idea/.gitignore
generated
vendored
Normal 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
|
||||||
202
08-labBST/Assignment.java
Normal file
202
08-labBST/Assignment.java
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
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
|
||||||
|
Node newNode = new Node();
|
||||||
|
newNode.val = toInsert;
|
||||||
|
if (root == null) {
|
||||||
|
root = newNode;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Node parent = root;
|
||||||
|
while (newNode.parent == null) {
|
||||||
|
if (toInsert < parent.val) {
|
||||||
|
if (parent.left == null) {
|
||||||
|
parent.left = newNode;
|
||||||
|
newNode.parent = parent;
|
||||||
|
} else {
|
||||||
|
parent = parent.left;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (parent.right == null) {
|
||||||
|
parent.right = newNode;
|
||||||
|
newNode.parent = parent;
|
||||||
|
} else {
|
||||||
|
parent = parent.right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(int toDelete) {
|
||||||
|
// Delete the provided element from the tree
|
||||||
|
Node toDeleteNode = root;
|
||||||
|
|
||||||
|
while (toDeleteNode != null && toDeleteNode.val != toDelete) {
|
||||||
|
if (toDelete < toDeleteNode.val) {
|
||||||
|
toDeleteNode = toDeleteNode.left;
|
||||||
|
} else {
|
||||||
|
toDeleteNode = toDeleteNode.right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// case 0: Node not found or tree is empty
|
||||||
|
if (toDeleteNode == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// case 1: Node has no children
|
||||||
|
if (toDeleteNode.left == null && toDeleteNode.right == null) {
|
||||||
|
if (toDeleteNode.parent == null) {
|
||||||
|
root = null;
|
||||||
|
} else if (toDeleteNode.parent.left == toDeleteNode) {
|
||||||
|
toDeleteNode.parent.left = null;
|
||||||
|
} else {
|
||||||
|
toDeleteNode.parent.right = null;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// case 2: Node has one child
|
||||||
|
if (toDeleteNode.left == null || toDeleteNode.right == null) {
|
||||||
|
Node child = toDeleteNode.left == null ? toDeleteNode.right : toDeleteNode.left;
|
||||||
|
if (toDeleteNode.parent == null) {
|
||||||
|
root = child;
|
||||||
|
} else if (toDeleteNode.parent.left == toDeleteNode) {
|
||||||
|
toDeleteNode.parent.left = child;
|
||||||
|
} else {
|
||||||
|
toDeleteNode.parent.right = child;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// case 3: Node has two children
|
||||||
|
Node successor = toDeleteNode.right;
|
||||||
|
while (successor.left != null) {
|
||||||
|
successor = successor.left;
|
||||||
|
}
|
||||||
|
// remove successor from its current position
|
||||||
|
if (successor.parent.left == successor) {
|
||||||
|
successor.parent.left = successor.right;
|
||||||
|
} else {
|
||||||
|
successor.parent.right = successor.right;
|
||||||
|
}
|
||||||
|
// replace toDeleteNode with successor
|
||||||
|
successor.parent = toDeleteNode.parent;
|
||||||
|
successor.left = toDeleteNode.left;
|
||||||
|
successor.right = toDeleteNode.right;
|
||||||
|
if (toDeleteNode.parent == null) {
|
||||||
|
root = successor;
|
||||||
|
} else if (toDeleteNode.parent.left == toDeleteNode) {
|
||||||
|
toDeleteNode.parent.left = successor;
|
||||||
|
} else {
|
||||||
|
toDeleteNode.parent.right = successor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 on each line
|
||||||
|
if (curr == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
inOrder(curr.left);
|
||||||
|
System.out.println(curr.val);
|
||||||
|
inOrder(curr.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void preOrder(Node curr) {
|
||||||
|
// Print the elements in the tree one on each line
|
||||||
|
if (curr == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
System.out.println(curr.val);
|
||||||
|
preOrder(curr.left);
|
||||||
|
preOrder(curr.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void postOrder(Node curr) {
|
||||||
|
// Print the elements in the tree one on each line
|
||||||
|
if (curr == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
postOrder(curr.left);
|
||||||
|
postOrder(curr.right);
|
||||||
|
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
103
08-labBST/TestAll.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
08-labBST/inputs/test01.txt
Normal file
14
08-labBST/inputs/test01.txt
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
i5
|
||||||
|
i3
|
||||||
|
i2
|
||||||
|
i7
|
||||||
|
i9
|
||||||
|
i8
|
||||||
|
i6
|
||||||
|
i1
|
||||||
|
i4
|
||||||
|
oin
|
||||||
|
opre
|
||||||
|
opost
|
||||||
|
e
|
||||||
|
|
||||||
18
08-labBST/inputs/test02.txt
Normal file
18
08-labBST/inputs/test02.txt
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
i8
|
||||||
|
i4
|
||||||
|
i6
|
||||||
|
i2
|
||||||
|
i15
|
||||||
|
i40
|
||||||
|
i30
|
||||||
|
i17
|
||||||
|
i9
|
||||||
|
i11
|
||||||
|
i12
|
||||||
|
i14
|
||||||
|
i21
|
||||||
|
oin
|
||||||
|
opre
|
||||||
|
opost
|
||||||
|
e
|
||||||
|
|
||||||
16
08-labBST/inputs/test03.txt
Normal file
16
08-labBST/inputs/test03.txt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
i5
|
||||||
|
i3
|
||||||
|
i4
|
||||||
|
i1
|
||||||
|
i8
|
||||||
|
i9
|
||||||
|
oin
|
||||||
|
d8
|
||||||
|
i11
|
||||||
|
i2
|
||||||
|
d3
|
||||||
|
d4
|
||||||
|
opost
|
||||||
|
opre
|
||||||
|
e
|
||||||
|
|
||||||
19
08-labBST/inputs/test04.txt
Normal file
19
08-labBST/inputs/test04.txt
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
i15
|
||||||
|
i7
|
||||||
|
i8
|
||||||
|
i5
|
||||||
|
i11
|
||||||
|
i9
|
||||||
|
i14
|
||||||
|
i3
|
||||||
|
i4
|
||||||
|
i1
|
||||||
|
opre
|
||||||
|
d15
|
||||||
|
d4
|
||||||
|
opre
|
||||||
|
i2
|
||||||
|
i6
|
||||||
|
d8
|
||||||
|
opre
|
||||||
|
e
|
||||||
20
08-labBST/inputs/test05.txt
Normal file
20
08-labBST/inputs/test05.txt
Normal 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
|
||||||
27
08-labBST/outputs/test01.txt
Normal file
27
08-labBST/outputs/test01.txt
Normal 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
|
||||||
39
08-labBST/outputs/test02.txt
Normal file
39
08-labBST/outputs/test02.txt
Normal 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
|
||||||
16
08-labBST/outputs/test03.txt
Normal file
16
08-labBST/outputs/test03.txt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
1
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
8
|
||||||
|
9
|
||||||
|
2
|
||||||
|
1
|
||||||
|
11
|
||||||
|
9
|
||||||
|
5
|
||||||
|
5
|
||||||
|
1
|
||||||
|
2
|
||||||
|
9
|
||||||
|
11
|
||||||
27
08-labBST/outputs/test04.txt
Normal file
27
08-labBST/outputs/test04.txt
Normal 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
|
||||||
25
08-labBST/outputs/test05.txt
Normal file
25
08-labBST/outputs/test05.txt
Normal 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
|
||||||
105
10-labMST/.gitignore
vendored
Normal file
105
10-labMST/.gitignore
vendored
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
### Intellij template
|
||||||
|
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||||
|
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||||
|
|
||||||
|
# User-specific stuff
|
||||||
|
.idea/**/workspace.xml
|
||||||
|
.idea/**/tasks.xml
|
||||||
|
.idea/**/usage.statistics.xml
|
||||||
|
.idea/**/dictionaries
|
||||||
|
.idea/**/shelf
|
||||||
|
|
||||||
|
# AWS User-specific
|
||||||
|
.idea/**/aws.xml
|
||||||
|
|
||||||
|
# Generated files
|
||||||
|
.idea/**/contentModel.xml
|
||||||
|
|
||||||
|
# Sensitive or high-churn files
|
||||||
|
.idea/**/dataSources/
|
||||||
|
.idea/**/dataSources.ids
|
||||||
|
.idea/**/dataSources.local.xml
|
||||||
|
.idea/**/sqlDataSources.xml
|
||||||
|
.idea/**/dynamic.xml
|
||||||
|
.idea/**/uiDesigner.xml
|
||||||
|
.idea/**/dbnavigator.xml
|
||||||
|
|
||||||
|
# Gradle
|
||||||
|
.idea/**/gradle.xml
|
||||||
|
.idea/**/libraries
|
||||||
|
|
||||||
|
# Gradle and Maven with auto-import
|
||||||
|
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||||
|
# since they will be recreated, and may cause churn. Uncomment if using
|
||||||
|
# auto-import.
|
||||||
|
# .idea/artifacts
|
||||||
|
# .idea/compiler.xml
|
||||||
|
# .idea/jarRepositories.xml
|
||||||
|
# .idea/modules.xml
|
||||||
|
# .idea/*.iml
|
||||||
|
# .idea/modules
|
||||||
|
# *.iml
|
||||||
|
# *.ipr
|
||||||
|
|
||||||
|
# CMake
|
||||||
|
cmake-build-*/
|
||||||
|
|
||||||
|
# Mongo Explorer plugin
|
||||||
|
.idea/**/mongoSettings.xml
|
||||||
|
|
||||||
|
# File-based project format
|
||||||
|
*.iws
|
||||||
|
|
||||||
|
# IntelliJ
|
||||||
|
out/
|
||||||
|
|
||||||
|
# mpeltonen/sbt-idea plugin
|
||||||
|
.idea_modules/
|
||||||
|
|
||||||
|
# JIRA plugin
|
||||||
|
atlassian-ide-plugin.xml
|
||||||
|
|
||||||
|
# Cursive Clojure plugin
|
||||||
|
.idea/replstate.xml
|
||||||
|
|
||||||
|
# SonarLint plugin
|
||||||
|
.idea/sonarlint/
|
||||||
|
|
||||||
|
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||||
|
com_crashlytics_export_strings.xml
|
||||||
|
crashlytics.properties
|
||||||
|
crashlytics-build.properties
|
||||||
|
fabric.properties
|
||||||
|
|
||||||
|
# Editor-based Rest Client
|
||||||
|
.idea/httpRequests
|
||||||
|
|
||||||
|
# Android studio 3.1+ serialized cache file
|
||||||
|
.idea/caches/build_file_checksums.ser
|
||||||
|
|
||||||
|
### Java template
|
||||||
|
# Compiled class file
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Log file
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# BlueJ files
|
||||||
|
*.ctxt
|
||||||
|
|
||||||
|
# Mobile Tools for Java (J2ME)
|
||||||
|
.mtj.tmp/
|
||||||
|
|
||||||
|
# Package Files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.nar
|
||||||
|
*.ear
|
||||||
|
*.zip
|
||||||
|
*.tar.gz
|
||||||
|
*.rar
|
||||||
|
|
||||||
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
|
hs_err_pid*
|
||||||
|
replay_pid*
|
||||||
|
|
||||||
10
10-labMST/.idea/.gitignore
generated
vendored
Normal file
10
10-labMST/.idea/.gitignore
generated
vendored
Normal file
@@ -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
|
||||||
149
10-labMST/Assignment.java
Normal file
149
10-labMST/Assignment.java
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
package labMST;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Assignment {
|
||||||
|
|
||||||
|
static class Graph {
|
||||||
|
private int n; // No. of vertices
|
||||||
|
private List<List<Integer>> adj; // An array of adjacency lists
|
||||||
|
private Map<String, Integer> weightMap;
|
||||||
|
|
||||||
|
public Graph(int n) {
|
||||||
|
this.n = n;
|
||||||
|
this.adj = new ArrayList<>(n);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
this.adj.add(new LinkedList<>());
|
||||||
|
}
|
||||||
|
this.weightMap = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addEdge(int u, int v) {
|
||||||
|
this.adj.get(u).add(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addEdgeWeight(int u, int v, int w) {
|
||||||
|
String e = u + "," + v;
|
||||||
|
this.weightMap.put(e, w);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWeight(int u, int v) {
|
||||||
|
String e = u + "," + v;
|
||||||
|
return this.weightMap.get(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> getAdjacent(int u) {
|
||||||
|
return this.adj.get(u);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLength() {
|
||||||
|
return this.n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void show() {
|
||||||
|
for (int v = 0; v < n; v++) {
|
||||||
|
System.out.print(v + ":");
|
||||||
|
for (int i : this.adj.get(v)) {
|
||||||
|
System.out.print(i + ";");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class QNode {
|
||||||
|
int id;
|
||||||
|
int key;
|
||||||
|
boolean isValid;
|
||||||
|
int pi;
|
||||||
|
|
||||||
|
public QNode() {
|
||||||
|
id = -1;
|
||||||
|
key = -1;
|
||||||
|
isValid = true;
|
||||||
|
pi = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implement Prim's Algorithm for finding the Minimum Spanning Tree.
|
||||||
|
// Return the QNode for each vertex in the graph, indicating the parent (pi)
|
||||||
|
// of every vertex in the resulting tree. Other fields of QNode are optional
|
||||||
|
// but may be useful in your implementation.
|
||||||
|
public static List<QNode> Prim(Graph g) {
|
||||||
|
// Initialize the QNode list
|
||||||
|
List<QNode> qNodes = new ArrayList<>(g.getLength());
|
||||||
|
for (int i = 0; i < g.getLength(); i++) {
|
||||||
|
qNodes.add(new QNode());
|
||||||
|
qNodes.get(i).id = i;
|
||||||
|
// Set key to max value for lowest priority
|
||||||
|
qNodes.get(i).key = Integer.MAX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start from the first vertex
|
||||||
|
qNodes.get(0).key = 0;
|
||||||
|
|
||||||
|
// Priority queue to select the vertex with the minimum key as the next to be added to the MST
|
||||||
|
PriorityQueue<QNode> pq = new PriorityQueue<>(Comparator.comparingInt(qNode -> qNode.key));
|
||||||
|
// Add the first vertex to the priority queue
|
||||||
|
pq.add(qNodes.get(0));
|
||||||
|
|
||||||
|
while (!pq.isEmpty()) {
|
||||||
|
// Get the vertex with the minimum key
|
||||||
|
QNode u = pq.poll();
|
||||||
|
// Mark it as part of the MST
|
||||||
|
u.isValid = false;
|
||||||
|
|
||||||
|
// For each adjacent vertex,
|
||||||
|
// if it is not already in the MST and the weight is less than the current key
|
||||||
|
// update the key and parent (pi)
|
||||||
|
for (int v : g.getAdjacent(u.id)) {
|
||||||
|
if (qNodes.get(v).isValid && g.getWeight(u.id, v) < qNodes.get(v).key) {
|
||||||
|
qNodes.get(v).key = g.getWeight(u.id, v);
|
||||||
|
qNodes.get(v).pi = u.id;
|
||||||
|
pq.add(qNodes.get(v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return qNodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void run(String inputPath) {
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(inputPath))) {
|
||||||
|
|
||||||
|
// Read graph
|
||||||
|
// n is # of nodes. nodes are indexed by 0, 1, 2, ..., n -1
|
||||||
|
int n = Integer.parseInt(br.readLine().trim());
|
||||||
|
|
||||||
|
// # of edges.
|
||||||
|
int m = Integer.parseInt(br.readLine().trim());
|
||||||
|
|
||||||
|
// Build the graph based on the edges on remaining input lines
|
||||||
|
Graph g = new Graph(n);
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
String[] parts = br.readLine().trim().split(" ");
|
||||||
|
int s = Integer.parseInt(parts[0]);
|
||||||
|
int t = Integer.parseInt(parts[1]);
|
||||||
|
g.addEdge(s, t);
|
||||||
|
int w = Integer.parseInt(parts[2]);
|
||||||
|
g.addEdgeWeight(s, t, w);
|
||||||
|
g.addEdge(t, s);
|
||||||
|
g.addEdgeWeight(t, s, w);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Your implementation must return a list of QNode. One QNode for
|
||||||
|
// each vertex, in the order 0, 1, 2, ..., n-1
|
||||||
|
List<QNode> pQNode = Prim(g);
|
||||||
|
|
||||||
|
// The correctness of your implementation will be judged by printing
|
||||||
|
// the parent of each vertex, line by line
|
||||||
|
for (int i = 1; i < n; i++) {
|
||||||
|
System.out.println(pQNode.get(i).pi);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
103
10-labMST/TestAll.java
Normal file
103
10-labMST/TestAll.java
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
package labMST;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
10-labMST/inputs/test01.txt
Normal file
16
10-labMST/inputs/test01.txt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
9
|
||||||
|
14
|
||||||
|
0 1 40
|
||||||
|
0 7 85
|
||||||
|
1 2 80
|
||||||
|
1 7 110
|
||||||
|
2 3 70
|
||||||
|
2 5 45
|
||||||
|
2 8 22
|
||||||
|
3 4 90
|
||||||
|
3 5 140
|
||||||
|
4 5 100
|
||||||
|
5 6 25
|
||||||
|
6 7 10
|
||||||
|
6 8 60
|
||||||
|
7 8 75
|
||||||
16
10-labMST/inputs/test02.txt
Normal file
16
10-labMST/inputs/test02.txt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
9
|
||||||
|
14
|
||||||
|
0 1 13
|
||||||
|
0 7 6
|
||||||
|
1 2 1
|
||||||
|
1 7 12
|
||||||
|
2 3 10
|
||||||
|
2 5 5
|
||||||
|
2 8 7
|
||||||
|
3 4 4
|
||||||
|
3 5 11
|
||||||
|
4 5 3
|
||||||
|
5 6 2
|
||||||
|
6 7 8
|
||||||
|
6 8 14
|
||||||
|
7 8 9
|
||||||
16
10-labMST/inputs/test03.txt
Normal file
16
10-labMST/inputs/test03.txt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
9
|
||||||
|
14
|
||||||
|
0 1 13
|
||||||
|
0 7 6
|
||||||
|
1 2 25
|
||||||
|
1 7 12
|
||||||
|
2 3 10
|
||||||
|
2 5 5
|
||||||
|
2 8 77
|
||||||
|
3 4 4
|
||||||
|
3 5 19
|
||||||
|
4 5 3
|
||||||
|
5 6 2
|
||||||
|
6 7 8
|
||||||
|
6 8 14
|
||||||
|
7 8 9
|
||||||
17
10-labMST/inputs/test04.txt
Normal file
17
10-labMST/inputs/test04.txt
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
9
|
||||||
|
15
|
||||||
|
0 1 13
|
||||||
|
0 7 6
|
||||||
|
1 2 8
|
||||||
|
1 7 12
|
||||||
|
2 3 10
|
||||||
|
2 5 5
|
||||||
|
2 8 7
|
||||||
|
3 4 4
|
||||||
|
3 5 11
|
||||||
|
3 7 20
|
||||||
|
4 5 3
|
||||||
|
5 6 2
|
||||||
|
6 7 1
|
||||||
|
6 8 14
|
||||||
|
7 8 9
|
||||||
17
10-labMST/inputs/test05.txt
Normal file
17
10-labMST/inputs/test05.txt
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
9
|
||||||
|
15
|
||||||
|
0 1 13
|
||||||
|
0 7 6
|
||||||
|
1 2 8
|
||||||
|
1 7 12
|
||||||
|
2 3 10
|
||||||
|
2 5 5
|
||||||
|
2 8 7
|
||||||
|
3 4 4
|
||||||
|
3 5 11
|
||||||
|
3 7 20
|
||||||
|
4 5 3
|
||||||
|
5 6 29
|
||||||
|
6 7 31
|
||||||
|
6 8 14
|
||||||
|
7 8 9
|
||||||
8
10-labMST/outputs/test01.txt
Normal file
8
10-labMST/outputs/test01.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
0
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
2
|
||||||
|
5
|
||||||
|
6
|
||||||
|
2
|
||||||
8
10-labMST/outputs/test02.txt
Normal file
8
10-labMST/outputs/test02.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
2
|
||||||
|
5
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
0
|
||||||
|
2
|
||||||
8
10-labMST/outputs/test03.txt
Normal file
8
10-labMST/outputs/test03.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
7
|
||||||
|
5
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
0
|
||||||
|
7
|
||||||
8
10-labMST/outputs/test04.txt
Normal file
8
10-labMST/outputs/test04.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
2
|
||||||
|
5
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
0
|
||||||
|
2
|
||||||
8
10-labMST/outputs/test05.txt
Normal file
8
10-labMST/outputs/test05.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
2
|
||||||
|
8
|
||||||
|
4
|
||||||
|
5
|
||||||
|
2
|
||||||
|
8
|
||||||
|
0
|
||||||
|
7
|
||||||
Reference in New Issue
Block a user