commit
c08ed85ed7
3 changed files with 513 additions and 0 deletions
-
68BST.java
-
44BSTNode.java
-
401TestBST.java
@ -0,0 +1,68 @@ |
|||
// Binary Search Tree class |
|||
// Xiwei Wang |
|||
|
|||
import java.util.*; |
|||
|
|||
public class BST |
|||
{ |
|||
// instance variables |
|||
private BSTNode m_root; |
|||
private int m_size; |
|||
|
|||
// constructor |
|||
public BST() |
|||
{ |
|||
m_root = null; |
|||
m_size = 0; |
|||
} |
|||
|
|||
// This method returns the number of elements in the tree. |
|||
// Do not make any changes to this method! |
|||
public int size() |
|||
{ |
|||
return m_size; |
|||
} |
|||
|
|||
// This method clears the content of the tree. |
|||
// Do not make any changes to this method! |
|||
public void clear() |
|||
{ |
|||
m_root = null; |
|||
m_size = 0; |
|||
} |
|||
|
|||
// This non-recursive method takes a string and inserts it into the binary |
|||
// search tree, keeping the tree ordered. |
|||
public void add(String value) |
|||
{ |
|||
// TODO: implement this method using a non-recursive solution |
|||
} |
|||
|
|||
// This non-recursive method returns a string that represents the in-order traversal |
|||
// of the binary search tree. |
|||
public String inOrder() |
|||
{ |
|||
// TODO: implement this method using a non-recursive solution |
|||
|
|||
return ""; // replace this statement with your own return |
|||
} |
|||
|
|||
// This method returns the smallest element in the binary search tree. You |
|||
// are not allowed to create any additional structures, including but not |
|||
// limited to arrays, stacks, queues, or other trees. |
|||
public String min() |
|||
{ |
|||
// TODO: implement this method using a non-recursive solution |
|||
|
|||
return ""; // replace this statement with your own return |
|||
} |
|||
|
|||
// This method takes a reference to the root of the expression, evaluates |
|||
// the tree, and returns the result as an int. |
|||
public int evaluate(BSTNode node) |
|||
{ |
|||
// TODO: implement this method using a non-recursive solution |
|||
|
|||
return -1; // replace this statement with your own return |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
// Binary Search Tree Node class |
|||
// Xiwei Wang |
|||
|
|||
public class BSTNode |
|||
{ |
|||
// data members |
|||
private String m_value; |
|||
private BSTNode m_left; |
|||
private BSTNode m_right; |
|||
|
|||
// constuctor |
|||
public BSTNode(String value) |
|||
{ |
|||
m_value = value; |
|||
m_left = null; |
|||
m_right = null; |
|||
} |
|||
|
|||
// member methods |
|||
public String getInfo() |
|||
{ |
|||
return m_value; |
|||
} |
|||
|
|||
public BSTNode getLeft() |
|||
{ |
|||
return m_left; |
|||
} |
|||
|
|||
public BSTNode getRight() |
|||
{ |
|||
return m_right; |
|||
} |
|||
|
|||
public void setLeft(BSTNode left) |
|||
{ |
|||
m_left = left; |
|||
} |
|||
|
|||
public void setRight(BSTNode right) |
|||
{ |
|||
m_right = right; |
|||
} |
|||
} |
|||
@ -0,0 +1,401 @@ |
|||
// BST class test driver |
|||
// Xiwei Wang |
|||
|
|||
import java.util.Scanner; |
|||
|
|||
public class TestBST |
|||
{ |
|||
public static void main(String[] args) |
|||
{ |
|||
BST mytree = new BST(); |
|||
int numPassedTests = 0; |
|||
int numTotalTests = 0; |
|||
String testResult; |
|||
|
|||
// Test 1 |
|||
numTotalTests++; |
|||
String sReturn = ""; |
|||
testResult = "[Failed]"; |
|||
String eMsg = "N/A"; |
|||
try |
|||
{ |
|||
// insert values |
|||
mytree.add("5"); |
|||
mytree.add("1"); |
|||
mytree.add("3"); |
|||
mytree.add("9"); |
|||
mytree.add("6"); |
|||
mytree.add("7"); |
|||
mytree.add("2"); |
|||
mytree.add("0"); |
|||
|
|||
sReturn = mytree.inOrder().trim(); |
|||
|
|||
if (sReturn.equals("0 1 2 3 5 6 7 9")) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": inOrder() ==> " + testResult + "\n Expected: 0 1 2 3 5 6 7 9"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + sReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 2 |
|||
numTotalTests++; |
|||
int iReturn = -1; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
iReturn = mytree.size(); |
|||
|
|||
if (iReturn == 8) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 8"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + iReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 3 |
|||
numTotalTests++; |
|||
iReturn = -1; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
sReturn = mytree.min(); |
|||
|
|||
if (sReturn.equals("0")) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": min() ==> " + testResult + "\n Expected: 0"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + sReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 4 |
|||
numTotalTests++; |
|||
sReturn = ""; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
mytree.clear(); |
|||
|
|||
// insert values |
|||
mytree.add("ab"); |
|||
mytree.add("bc"); |
|||
mytree.add("ac"); |
|||
mytree.add("de"); |
|||
mytree.add("ae"); |
|||
mytree.add("ck"); |
|||
mytree.add("dg"); |
|||
mytree.add("bp"); |
|||
mytree.add("eh"); |
|||
mytree.add("ck"); |
|||
|
|||
sReturn = mytree.inOrder().trim(); |
|||
|
|||
if (sReturn.equals("ab ac ae bc bp ck de dg eh")) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": inOrder() ==> " + testResult + "\n Expected: ab ac ae bc bp ck de dg eh"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + sReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 5 |
|||
numTotalTests++; |
|||
iReturn = -1; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
iReturn = mytree.size(); |
|||
|
|||
if (iReturn == 9) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 9"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + iReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 6 |
|||
numTotalTests++; |
|||
iReturn = -1; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
sReturn = mytree.min(); |
|||
|
|||
if (sReturn.equals("ab")) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": min() ==> " + testResult + "\n Expected: ab"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + sReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 7 |
|||
numTotalTests++; |
|||
sReturn = ""; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
mytree.clear(); |
|||
|
|||
// insert values |
|||
mytree.add("@"); |
|||
mytree.add("?"); |
|||
mytree.add("="); |
|||
mytree.add("/"); |
|||
mytree.add("-"); |
|||
mytree.add("+"); |
|||
mytree.add("*"); |
|||
mytree.add("%"); |
|||
|
|||
sReturn = mytree.inOrder().trim(); |
|||
|
|||
if (sReturn.equals("% * + - / = ? @")) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": inOrder() ==> " + testResult + "\n Expected: % * + - / = ? @"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + sReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 8 |
|||
numTotalTests++; |
|||
iReturn = -1; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
iReturn = mytree.size(); |
|||
|
|||
if (iReturn == 8) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": size() ==> " + testResult + "\n Expected: 8"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + iReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 9 |
|||
numTotalTests++; |
|||
iReturn = -1; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
sReturn = mytree.min(); |
|||
|
|||
if (sReturn.equals("%")) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": min() ==> " + testResult + "\n Expected: %"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + sReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 10 |
|||
numTotalTests++; |
|||
iReturn = -1; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
// build a binary expression tree |
|||
BSTNode root = new BSTNode("*"); |
|||
root.setRight(new BSTNode("3")); |
|||
root.setLeft(new BSTNode("+")); |
|||
root.getLeft().setLeft(new BSTNode("15")); |
|||
root.getLeft().setRight(new BSTNode("21")); |
|||
|
|||
iReturn = mytree.evaluate(root.getLeft()); |
|||
|
|||
if (iReturn == 36) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": evaluate(root), where root points to \"+\" in the following tree ==> " + testResult + "\n"); |
|||
System.out.println(String.format("%8s\n%9s\n%10s\n%7s\n%9s\n", "*", "/ \\", "+ 3", "/ \\", "15 21") + "\n Expected: 36"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + iReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 11 |
|||
numTotalTests++; |
|||
iReturn = -1; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
// build a binary expression tree |
|||
BSTNode root = new BSTNode("*"); |
|||
root.setRight(new BSTNode("3")); |
|||
root.setLeft(new BSTNode("+")); |
|||
root.getLeft().setLeft(new BSTNode("15")); |
|||
root.getLeft().setRight(new BSTNode("21")); |
|||
|
|||
iReturn = mytree.evaluate(root); |
|||
|
|||
if (iReturn == 108) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": evaluate(root), where root points to \"*\" in the following tree ==> " + testResult + "\n"); |
|||
System.out.println(String.format("%8s\n%9s\n%10s\n%7s\n%9s\n", "*", "/ \\", "+ 3", "/ \\", "15 21") + "\n Expected: 108"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + iReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
// Test 12 |
|||
numTotalTests++; |
|||
iReturn = -1; |
|||
testResult = "[Failed]"; |
|||
eMsg = "N/A"; |
|||
try |
|||
{ |
|||
// build a binary expression tree |
|||
BSTNode root = new BSTNode("/"); |
|||
root.setRight(new BSTNode("+")); |
|||
root.setLeft(new BSTNode("*")); |
|||
BSTNode left = root.getLeft(); |
|||
BSTNode right = root.getRight(); |
|||
|
|||
left.setLeft(new BSTNode("-")); |
|||
left.setRight(new BSTNode("+")); |
|||
|
|||
right.setLeft(new BSTNode("8")); |
|||
right.setRight(new BSTNode("15")); |
|||
|
|||
left.getLeft().setLeft(new BSTNode("33")); |
|||
left.getLeft().setRight(new BSTNode("44")); |
|||
|
|||
left.getRight().setLeft(new BSTNode("27")); |
|||
left.getRight().setRight(new BSTNode("19")); |
|||
|
|||
iReturn = mytree.evaluate(root); |
|||
|
|||
if (iReturn == -22) |
|||
{ |
|||
numPassedTests++; |
|||
testResult = "[Passed]"; |
|||
} |
|||
} |
|||
catch (RuntimeException e) |
|||
{ |
|||
eMsg = "RuntimeException - \"" + e.getMessage() + "\""; |
|||
} |
|||
|
|||
System.out.println("Test " + numTotalTests + ": evaluate(root), where root points to \"/\" in the following tree ==> " + testResult + "\n"); |
|||
System.out.println(String.format("%14s\n%17s\n%18s\n%19s\n%20s\n%13s\n%14s\n", "/", "/ \\", "* +", "/ \\ / \\", "- + 8 15", "/ \\ / \\", "33 44 27 19") + "\n Expected: -22"); |
|||
if (eMsg.equals("N/A")) |
|||
System.out.println(" Yours: " + iReturn + "\n"); |
|||
else |
|||
System.out.println(" Yours: " + eMsg + "\n"); |
|||
|
|||
System.out.println("Total test cases: " + numTotalTests + "\nCorrect: " + numPassedTests + "\nWrong: " + (numTotalTests - numPassedTests)); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue