Skip to content
Snippets Groups Projects
Commit 93627f44 authored by @thx's avatar @thx
Browse files

Add project files

parents
No related branches found
No related tags found
No related merge requests found
# 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*
.idea/
# Verify AVL
## Setting up
Clone this repo, edit it locally, and push and upload to Gradescope, exactly as in P1. You should use Java 11 when
working on this.
You should only edit `verifyavl.VerifyAVL.java` and do not change the method signature. Though, feel free to add private
methods as you see fit.
## The assignment
Write Java code for an Θ(n) worst-case algorithm that verifies that a tree is actually an AVL tree
in `src/main/java/verifyavl.VerifyAVL.java`.
You may assume the nodes of the tree have the following definition given in `src/main/java/verifyavl/AVLNode.java`.
You must verify the BST Property, the AVL Balance Condition, and the correctness of the height information. If it
fails *any* of these properties, return `false`. Otherwise, return `true`.
You can assume that the keys and heights will not exceed `Integer.MAX_VALUE`.
## Grading
We will be running an autograder with **many hidden test cases** to grade your submission. You will receive points for
each test case you pass. The points will be weighted more heavily on our stress tests, so note that the majority of the
points you can get require that your code works without question.
## Testing
We've given you a testing harness in `src/test/java/VerifyAVLTests.java`. It reads in `tests.json` and tests your
verifyavl.VerifyAVL method on the given tree.
The `tests.json` file contains as many test cases as you wish to add, where each case is on a separate line with the
following format:
`{"answer": true, "tree": [5,1,[2,0,null,null], [6,0,null,null]]}`
Note that the array representation of an verifyavl.AVLNode is as follows:
`[int key, int height, [verifyavl.AVLNode left], [verifyavl.AVLNode right]]`
So the example above is of a tree with a root node with key 5, a left child node with a key 2, and a right child node
with a key 6.
We have provided you with 4 basic test cases:
- A correct AVL tree: `[5,1,[2,0,null,null], [6,0,null,null]]`
![Correct AVL](./img/correct.png)
- A tree that violates the BST property: `[1,1,[2,0,null,null],null]`
![Incorrect BST](./img/bst.png)
- A tree with incorrect height labels: `[2,1,[1,0,null,null],[4,1,[3,0,null,null],null]]`
![Incorrect heights](./img/height.png)
- A tree that violates the AVL balance
property: `[2,3,[1,0,null,null],[4,2,[3,0,null,null],[5,1,null,[6,0,null,null]]]]`
![Incorrect AVL](./img/avl.png)
**Note: You should add as many test cases as you need to convince yourself that your solution works. Think about edge
cases! Your tests will not be graded, but if your code does not work, testing will help you find the problems before we
grade it.**
\ No newline at end of file
img/avl.png

121 KiB

img/bst.png

38.9 KiB

img/correct.png

50 KiB

img/height.png

70.6 KiB

pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.washington.cs.cse332</groupId>
<artifactId>verifyavl-public-21au</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
package verifyavl;
public class AVLNode {
public AVLNode left;
public AVLNode right;
public int key; // The data element stored at this node.
public int height;
public AVLNode(int key, int height, AVLNode left, AVLNode right) {
this.key = key;
this.height = height;
this.left = left;
this.right = right;
}
}
package verifyavl;
public class VerifyAVL {
public static boolean verifyAVL(AVLNode root) {
/* TODO: Edit this with your code */
throw new IllegalStateException();
}
/* TODO: Add private methods if you want (recommended) */
}
\ No newline at end of file
import verifyavl.AVLNode;
import verifyavl.VerifyAVL;
import org.json.simple.*;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Test;
import static org.junit.Assert.*;
public class VerifyAVLTests {
@Test
public void verifyAVLTest() throws IOException, ParseException {
// Parse the tests.json file as a json object
Object data = new JSONParser().parse(new FileReader("tests.json"));
// Cast it to a JSONArray
JSONArray jsonArray = (JSONArray) data;
// For each element in the JSON array,
// i.e. For each test case,
for (Object o : jsonArray) {
// Cast it to a JSONObject
JSONObject test = (JSONObject) o;
// Get the actual answer from
boolean expected = (boolean) test.get("answer");
JSONArray tree = (JSONArray) test.get("tree");
AVLNode root = makeAvl(tree);
boolean actual = VerifyAVL.verifyAVL(root);
assertEquals("Failed: " + tree + " is a " + (expected ? "correct" : "wrong") + " tree but returned " + (actual ? "correct" : "wrong"), expected, actual);
}
}
public static AVLNode makeAvl(JSONArray data) {
if (data == null) {
return null;
}
AVLNode left = makeAvl((JSONArray) data.get(2));
AVLNode right = makeAvl((JSONArray) data.get(3));
int key = (int) (long) data.get(0);
int height = (int) (long) data.get(1);
return new AVLNode(key, height, left, right);
}
}
[
{
"answer": true,
"tree": [
5,
1,
[
2,
0,
null,
null
],
[
6,
0,
null,
null
]
]
},
{
"answer": false,
"tree": [
1,
1,
[
2,
0,
null,
null
],
null
]
},
{
"answer": false,
"tree": [
2,
1,
[
1,
0,
null,
null
],
[
4,
1,
[
3,
0,
null,
null
],
null
]
]
},
{
"answer": false,
"tree": [
2,
3,
[
1,
0,
null,
null
],
[
4,
2,
[
3,
0,
null,
null
],
[
5,
1,
null,
[
6,
0,
null,
null
]
]
]
]
}
]
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment