mirror of
https://github.com/RustyRaptor/CS-272.git
synced 2026-12-03 15:55:22 -07:00
614 lines
15 KiB
Java
614 lines
15 KiB
Java
|
|
|
|
|
|
/**
|
|
* Node for Binary Tree
|
|
*
|
|
* @author Huiping Cao
|
|
*/
|
|
class BSTNode {
|
|
private int data; //the element value for this node
|
|
private BST left; //the left child of this node
|
|
private BST right; //the right child of this node
|
|
|
|
/**
|
|
* No-argument constructor
|
|
*/
|
|
public BSTNode() {
|
|
}
|
|
|
|
/**
|
|
* Constructor with the initial element
|
|
*
|
|
* @param initData: the initial element
|
|
*/
|
|
BSTNode(int initData) {
|
|
data = initData;
|
|
left = new BST();
|
|
right = new BST();
|
|
}
|
|
|
|
/**
|
|
* Constructor with the initial element, initial left and right children
|
|
*
|
|
* @param initData: the initial element
|
|
* initParent: the initial parent tree
|
|
* @param initLeft: left child
|
|
* @param initRight: right child
|
|
*/
|
|
public BSTNode(int initData, BST initLeft, BST initRight) {
|
|
data = initData;
|
|
left = initLeft;
|
|
right = initRight;
|
|
}
|
|
|
|
/**
|
|
* Evaluate whether this node is a leaf node or not
|
|
*
|
|
* @return true if it is a leaf node; otherwise, return false.
|
|
*/
|
|
public boolean isLeaf() {
|
|
return (left == null || left.root == null) && (right == null || right.root == null);
|
|
}
|
|
|
|
|
|
/**
|
|
* @return the data
|
|
*/
|
|
public int getData() {
|
|
return data;
|
|
}
|
|
|
|
/**
|
|
* @param data set the element in this node
|
|
*/
|
|
public void setData(int data) {
|
|
this.data = data;
|
|
}
|
|
|
|
/**
|
|
* @return the left child
|
|
*/
|
|
public BST getLeft() {
|
|
return left;
|
|
}
|
|
|
|
/**
|
|
* @param left the left child to be set
|
|
*/
|
|
public void setLeft(BST left) {
|
|
this.left = left;
|
|
}
|
|
|
|
/**
|
|
* @return the right child
|
|
*/
|
|
public BST getRight() {
|
|
return right;
|
|
}
|
|
|
|
/**
|
|
* @param right the right child to be set
|
|
*/
|
|
public void setRight(BST right) {
|
|
this.right = right;
|
|
}
|
|
|
|
|
|
/**
|
|
* Convert this BTNode to a string
|
|
*/
|
|
public String toString() {
|
|
String str = "";
|
|
|
|
if (left == null || left.root == null) str += "(null)";
|
|
else str += "(" + left.root.getData() + ")";
|
|
|
|
str += data;
|
|
|
|
if (right == null || right.root == null) str += "(null)";
|
|
else str += "(" + right.root.getData() + ")";
|
|
|
|
return str;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* The class for Binary Search Tree
|
|
*
|
|
* @author Huiping Cao
|
|
*/
|
|
public class BST {
|
|
protected BSTNode root; //the tree root
|
|
|
|
|
|
/**
|
|
* Get the left subtree of this tree
|
|
*
|
|
* @return the left subtree of this tree
|
|
*/
|
|
private BST getLeftSubtree() {
|
|
return root.getLeft();
|
|
}
|
|
|
|
/**
|
|
* Get the right subtree of this tree
|
|
*
|
|
* @return the right subtree of this tree
|
|
*/
|
|
private BST getRightSubtree() {
|
|
return root.getRight();
|
|
}
|
|
|
|
/**
|
|
* Print the tree using in-order traversal strategy
|
|
*/
|
|
private void inOrderPrt() {
|
|
inOrderPrt(0);
|
|
}
|
|
|
|
/**
|
|
* private, recursive function
|
|
* I slightly changed the method to print right subtree first
|
|
* It is to make the display more easier to read
|
|
* <p>
|
|
* node
|
|
*
|
|
* @param indentation branch
|
|
*/
|
|
private void inOrderPrt(int indentation) {
|
|
if (root != null) {
|
|
if (getRightSubtree() != null)
|
|
getRightSubtree().inOrderPrt(indentation + 1);
|
|
for (int i = 0; i < indentation * 4; i++)
|
|
System.out.print(" ");
|
|
|
|
System.out.println(root.getData());
|
|
if (getLeftSubtree() != null)
|
|
getLeftSubtree().inOrderPrt(indentation + 1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Debug function, print the tree for debugging purpose
|
|
*/
|
|
public String toString() {
|
|
if (root != null) return root.toString();
|
|
else return null;
|
|
}
|
|
|
|
///////////////////////////////////////
|
|
///////////////////////////////////////
|
|
// Please add the functions required for your lab homework here.
|
|
|
|
|
|
/**
|
|
* inserts an int into the tree
|
|
*
|
|
* @param e the integer we want to insert
|
|
* @return a bool depending if we inserted it or not.
|
|
*/
|
|
public boolean insert(int e) {
|
|
if (this.root == null) {
|
|
root = new BSTNode(e);
|
|
return true;
|
|
}
|
|
if (root.getData() == e) {
|
|
return false;
|
|
}
|
|
if (this.root != null) {
|
|
if (this.root.getData() >= e) {
|
|
return this.getLeftSubtree().insert(e);
|
|
|
|
}
|
|
if (this.root.getData() < e) {
|
|
return this.getRightSubtree().insert(e);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
/**
|
|
* remove
|
|
*
|
|
* @param e the integer to search and remove.
|
|
* @return a bool depending if we removed something.
|
|
*/
|
|
public boolean remove(int e) {
|
|
if (this.root == null) {
|
|
return false;
|
|
}
|
|
if (root.getData() == e) {
|
|
if (getLeftSubtree().root == null && getRightSubtree().root == null) {
|
|
root = null;
|
|
} else if (getLeftSubtree().root == null && getRightSubtree().root != null) {
|
|
root = getRightSubtree().root;
|
|
} else if (getLeftSubtree().root != null && getRightSubtree().root == null) {
|
|
root = getLeftSubtree().root;
|
|
} else if (getLeftSubtree().root != null && getRightSubtree().root != null) {
|
|
int maxDataLeft =
|
|
this.getLeftSubtree().removeMax();
|
|
this.root.setData(maxDataLeft);
|
|
}
|
|
return true;
|
|
}
|
|
if (root != null) {
|
|
if (e < root.getData()) {
|
|
return getLeftSubtree().remove(e);
|
|
} else if (e > root.getData()) {
|
|
return getRightSubtree().remove(e);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* remove max
|
|
* Removes the max integer in the subtree
|
|
*
|
|
* @return the max integer we removed
|
|
*/
|
|
private int removeMax() {
|
|
BST RT = this.getRightSubtree();
|
|
int maxData;
|
|
if (RT.root == null) {
|
|
maxData = this.root.getData();
|
|
this.root = this.getLeftSubtree().root;
|
|
return maxData;
|
|
} else {
|
|
return RT.removeMax();
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Search Recursion
|
|
*
|
|
* @param e The int we are looking for
|
|
* @return The Node of the item we are looking for.
|
|
*/
|
|
private BSTNode searchRecursion(int e) {
|
|
if (this.root == null) {
|
|
return null;
|
|
}
|
|
if (root.getData() == e) {
|
|
return root;
|
|
}
|
|
if (root != null) {
|
|
if (e < root.getData()) {
|
|
return getLeftSubtree().searchRecursion(e);
|
|
} else if (e > root.getData()) {
|
|
return getRightSubtree().searchRecursion(e);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Search Non Recursion
|
|
*
|
|
* @param e integer to search for
|
|
* @return The Node of the item we are looking for.
|
|
*/
|
|
private BSTNode searchNonRecursion(int e) {
|
|
BST cursor = this;
|
|
do {
|
|
if (cursor.root.getData() == e) {
|
|
return cursor.root;
|
|
}
|
|
if (e < cursor.root.getData()) {
|
|
cursor = cursor.getLeftSubtree();
|
|
} else if (e > cursor.root.getData()) {
|
|
cursor = cursor.getRightSubtree();
|
|
}
|
|
} while (cursor.root != null);
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Returns the sum of all items in the tree
|
|
*
|
|
* @return sum of all the ints in the tree.
|
|
*/
|
|
public int sum() {
|
|
if (root == null) {
|
|
return 0;
|
|
}
|
|
return root.getData()
|
|
+ getRightSubtree().sum()
|
|
+ getLeftSubtree().sum();
|
|
}
|
|
|
|
/**
|
|
* Test cases provided by the instructor
|
|
*/
|
|
private static void test1() {
|
|
BST tree = new BST();
|
|
System.out.println("Initial tree:");
|
|
tree.inOrderPrt(); //test inOrder traversal
|
|
|
|
boolean rc;
|
|
|
|
// int i = 2;
|
|
// double b = 1;
|
|
// while(i < 999){
|
|
//
|
|
// tree.insert(i);
|
|
// i++;
|
|
// }
|
|
//test 1: insert method, and test 2 in-order traversal
|
|
rc = tree.insert(12);
|
|
System.out.println("\nInsert 12, inserted=" + rc + ", after adding 12:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(6);
|
|
System.out.println("\nInsert 6, inserted=" + rc + ", after adding 6:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(19);
|
|
System.out.println("\nInsert 19, inserted=" + rc + ", after adding 19:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(4);
|
|
System.out.println("\nInsert 4, inserted=" + rc + ", after adding 4:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(8);
|
|
System.out.println("\nInsert 8, inserted=" + rc + ", after adding 8:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(16);
|
|
System.out.println("\nInsert 16, inserted=" + rc + ", after adding 16:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(8);
|
|
System.out.println("\nInsert 8 (second), inserted=" + rc + ", after adding 8:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(5);
|
|
System.out.println("\nInsert 5, inserted=" + rc + ", after adding 5:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(9);
|
|
System.out.println("\nInsert 9, inserted=" + rc + ", after adding 9:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(20);
|
|
System.out.println("\nInsert 20, inserted=" + rc + ", after adding 20:");
|
|
tree.inOrderPrt();
|
|
|
|
System.out.println("Inorder traversal results:");
|
|
tree.inOrderPrt();
|
|
System.out.print("\n\n");
|
|
|
|
//test 3: search method
|
|
BSTNode node = tree.searchRecursion(6);
|
|
System.out.println("searchRecursion 6, node=" + node);
|
|
node = tree.searchRecursion(22);
|
|
System.out.println("searchRecursion 22, node=" + node);
|
|
node = tree.searchRecursion(8);
|
|
System.out.println("searchRecursion 8, node=" + node + "\n");
|
|
|
|
node = tree.searchNonRecursion(6);
|
|
System.out.println("searchNonRecursion 6, node=" + node);
|
|
node = tree.searchNonRecursion(22);
|
|
System.out.println("searchNonRecursion 22, node=" + node);
|
|
node = tree.searchNonRecursion(8);
|
|
System.out.println("searchNonRecursion 8, node=" + node);
|
|
|
|
// //test 4: remove method
|
|
rc = tree.remove(30); //test case 0: e does not exist
|
|
System.out.println("\nRemove 30, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(20); //test case 1: leaf
|
|
System.out.println("\nRemove 20, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(4); //test case 2: left is empty
|
|
System.out.println("\nRemove 4, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
|
|
rc = tree.remove(19); //test case 3: right is empty
|
|
System.out.println("\nRemove 19, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(6); //test case 4: no subtree is empty
|
|
System.out.println("\nRemove 6, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(12); //more tests: remove root
|
|
System.out.println("\nRemove 12, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(8); //more tests
|
|
System.out.println("\nRemove 8, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(5); //more tests
|
|
System.out.println("\nRemove 5, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(8); //more tests
|
|
System.out.println("\nRemove 8, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
rc = tree.remove(16); //more tests
|
|
System.out.println("\nRemove 16, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
System.out.print("sum=" + tree.sum() + "\n");
|
|
|
|
System.out.println("\nAdding a series of numbers:");
|
|
tree.insert(30);
|
|
tree.insert(20);
|
|
tree.insert(10);
|
|
tree.insert(25);
|
|
tree.insert(28);
|
|
tree.insert(24);
|
|
tree.insert(11);
|
|
tree.insert(5);
|
|
tree.insert(11);
|
|
tree.insert(12);
|
|
tree.insert(50);
|
|
tree.insert(40);
|
|
tree.insert(35);
|
|
tree.inOrderPrt();
|
|
System.out.print("sum=" + tree.sum() + "\n");
|
|
|
|
System.out.print("sum=" + tree.sum() + "\n");
|
|
System.out.print("\n\n");
|
|
|
|
System.out.println("\nRemove 20 (removeNode case 4):");
|
|
tree.remove(20);
|
|
tree.inOrderPrt();
|
|
System.out.print("sum=" + tree.sum() + "\n");
|
|
|
|
System.out.println("Inorder traversal results: ");
|
|
tree.inOrderPrt();
|
|
System.out.print("\n");
|
|
System.out.print("sum=" + tree.sum() + "\n");
|
|
}
|
|
|
|
private static void test2() {
|
|
BST tree = new BST();
|
|
System.out.println("Ziad's Test Cases");
|
|
System.out.println("Initial tree:");
|
|
tree.inOrderPrt(); //test inOrder traversal
|
|
|
|
boolean rc;
|
|
//Just added some extra tests for null cases.
|
|
tree.remove(1);
|
|
tree.removeMax();
|
|
tree.sum();
|
|
tree.searchRecursion(1);
|
|
tree.searchNonRecursion(1);
|
|
|
|
//test 1: insert method, and test 2 in-order traversal
|
|
rc = tree.insert(12);
|
|
System.out.println("\nInsert 12, inserted=" + rc + ", after adding 12:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(6);
|
|
System.out.println("\nInsert 6, inserted=" + rc + ", after adding 6:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(19);
|
|
System.out.println("\nInsert 19, inserted=" + rc + ", after adding 19:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(4);
|
|
System.out.println("\nInsert 4, inserted=" + rc + ", after adding 4:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(8);
|
|
System.out.println("\nInsert 8, inserted=" + rc + ", after adding 8:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(16);
|
|
System.out.println("\nInsert 16, inserted=" + rc + ", after adding 16:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(8);
|
|
System.out.println("\nInsert 8 (second), inserted=" + rc + ", after adding 8:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(5);
|
|
System.out.println("\nInsert 5, inserted=" + rc + ", after adding 5:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(9);
|
|
System.out.println("\nInsert 9, inserted=" + rc + ", after adding 9:");
|
|
tree.inOrderPrt();
|
|
rc = tree.insert(20);
|
|
System.out.println("\nInsert 20, inserted=" + rc + ", after adding 20:");
|
|
tree.inOrderPrt();
|
|
|
|
System.out.println("Inorder traversal results:");
|
|
tree.inOrderPrt();
|
|
System.out.print("\n\n");
|
|
|
|
//test 3: search method
|
|
BSTNode node = tree.searchRecursion(6);
|
|
System.out.println("searchRecursion 6, node=" + node);
|
|
node = tree.searchRecursion(22);
|
|
System.out.println("searchRecursion 22, node=" + node);
|
|
node = tree.searchRecursion(8);
|
|
System.out.println("searchRecursion 8, node=" + node + "\n");
|
|
|
|
node = tree.searchNonRecursion(6);
|
|
System.out.println("searchNonRecursion 6, node=" + node);
|
|
node = tree.searchNonRecursion(22);
|
|
System.out.println("searchNonRecursion 22, node=" + node);
|
|
node = tree.searchNonRecursion(8);
|
|
System.out.println("searchNonRecursion 8, node=" + node);
|
|
|
|
// //test 4: remove method
|
|
rc = tree.remove(30); //test case 0: e does not exist
|
|
System.out.println("\nRemove 30, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(20); //test case 1: leaf
|
|
System.out.println("\nRemove 20, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(4); //test case 2: left is empty
|
|
System.out.println("\nRemove 4, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
|
|
rc = tree.remove(19); //test case 3: right is empty
|
|
System.out.println("\nRemove 19, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(6); //test case 4: no subtree is empty
|
|
System.out.println("\nRemove 6, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(12); //more tests: remove root
|
|
System.out.println("\nRemove 12, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(8); //more tests
|
|
System.out.println("\nRemove 8, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(5); //more tests
|
|
System.out.println("\nRemove 5, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
|
|
rc = tree.remove(8); //more tests
|
|
System.out.println("\nRemove 8, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
rc = tree.remove(16); //more tests
|
|
System.out.println("\nRemove 16, rc=" + rc);
|
|
tree.inOrderPrt();
|
|
System.out.print("sum=" + tree.sum() + "\n");
|
|
|
|
System.out.println("\nAdding a series of numbers:");
|
|
tree.insert(30);
|
|
tree.insert(20);
|
|
tree.insert(10);
|
|
tree.insert(25);
|
|
tree.insert(28);
|
|
tree.insert(24);
|
|
tree.insert(11);
|
|
tree.insert(5);
|
|
tree.insert(11);
|
|
tree.insert(12);
|
|
tree.insert(50);
|
|
tree.insert(40);
|
|
tree.insert(35);
|
|
tree.inOrderPrt();
|
|
System.out.print("sum=" + tree.sum() + "\n");
|
|
|
|
System.out.print("sum=" + tree.sum() + "\n");
|
|
System.out.print("\n\n");
|
|
|
|
System.out.println("\nRemove 20 (removeNode case 4):");
|
|
tree.remove(20);
|
|
tree.inOrderPrt();
|
|
System.out.print("sum=" + tree.sum() + "\n");
|
|
|
|
System.out.println("Inorder traversal results: ");
|
|
tree.inOrderPrt();
|
|
System.out.print("\n");
|
|
System.out.print("sum=" + tree.sum() + "\n");
|
|
}
|
|
|
|
/**
|
|
* @param args arguments from system
|
|
*/
|
|
public static void main(String[] args) {
|
|
//You need to let your program pass the test cases in this function
|
|
test1();
|
|
test2();
|
|
//You can add your own test cases here.
|
|
}
|
|
}
|