diff --git a/src/BST.java b/src/BST.java index 829d94b..68eab01 100644 --- a/src/BST.java +++ b/src/BST.java @@ -2,38 +2,41 @@ /** - * Node for Binary Tree - * + * Node for Binary Tree + * * @author Huiping Cao */ -class BSTNode{ +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 - + private BST left; //the left child of this node + private BST right; //the right child of this node + /** * No-argument constructor */ - public BSTNode(){} - + public BSTNode() { + } + /** * Constructor with the initial element + * * @param initData: the initial element */ - public BSTNode(int initData){ - data = initData; + 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 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){ + public BSTNode(int initData, BST initLeft, BST initRight) { data = initData; left = initLeft; right = initRight; @@ -41,15 +44,14 @@ class BSTNode{ /** * 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!=null && left.root==null)) - &&((right==null)||(right!=null && right.root==null))); + public boolean isLeaf() { + return (left == null || left.root == null) && (right == null || right.root == null); } - - + + /** * @return the data */ @@ -91,23 +93,22 @@ class BSTNode{ public void setRight(BST right) { this.right = right; } - - + + /** * Convert this BTNode to a string */ - public String toString() - { - String str=""; - - if((left==null)||(left!=null && left.root==null)) str +="(null)"; - else str +="("+left.root.getData()+")"; - + public String toString() { + String str = ""; + + if (left == null || left.root == null) str += "(null)"; + else str += "(" + left.root.getData() + ")"; + str += data; - - if((right==null)||(right!=null&&right.root==null)) str +="(null)"; - else str +="("+right.root.getData()+")"; - + + if (right == null || right.root == null) str += "(null)"; + else str += "(" + right.root.getData() + ")"; + return str; } } @@ -115,251 +116,498 @@ class BSTNode{ /** * The class for Binary Search Tree - * @author Huiping Cao * + * @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(){ + private BST getLeftSubtree() { return root.getLeft(); } - + /** * Get the right subtree of this tree + * * @return the right subtree of this tree */ - private BST getRightSubtree(){ + private BST getRightSubtree() { return root.getRight(); } - + /** * Print the tree using in-order traversal strategy */ - public void inOrderPrt(){ + 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 - * + * I slightly changed the method to print right subtree first + * It is to make the display more easier to read + *

* node - * @param indentation - * branch + * + * @param indentation branch */ - private void inOrderPrt(int indentation){ - if(root!=null){ - if(getRightSubtree()!=null) getRightSubtree().inOrderPrt(indentation+1); - - for(int i=0;i e){ - this.getRightSubtree().insert(e); - return true; - } - return true; - } - return true; - } - public boolean remove (int e){ - if (this.root == null) { - root = new BSTNode(e); - return true; - } - if (root.getData() == e){ - if (getLeftSubtree().root == null && getRightSubtree().root == null){ - root = null; - } - if (getLeftSubtree().root == null && getRightSubtree().root != null){ - root = getRightSubtree().root; - } - if (getLeftSubtree().root != null && getRightSubtree().root == null){ - root = getLeftSubtree().root; - } - if (getLeftSubtree().root != null && getRightSubtree().root != null){ - int maxDataLeft = - } - - return true; - } - if (this.root != null) { - if (this.root.getData() <= e){ - this.getLeftSubtree().insert(e); - return true; - } - if (this.root.getData() > e){ - this.getRightSubtree().insert(e); - return true; - } - return true; - } - return true; - } - - public int removeMax(){ - BSTNode RT = this.getRightSubtree().root; - int maxData; - if (RT == null){ - maxData = ; - } - } - /** * Debug function, print the tree for debugging purpose */ - public String toString() - { - if(root!=null) return root.toString(); + 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 - * @throws Exception */ - private static void test1() throws Exception{ + private static void test1() { BST tree = new BST(); System.out.println("Initial tree:"); tree.inOrderPrt(); //test inOrder traversal - - boolean rc = true; - + + 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(); - rc = tree.insert(20); System.out.println("\nInsert 20 " + - "AGAIN?, inserted="+rc+", after adding 20:"); tree.inOrderPrt(); - + 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(); + 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); -// + 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 - * @throws Exception - */ - public static void main(String[] args) throws Exception { - //You need to let your program pass the test cases in this function - test1(); - - //You can add your own test cases here. - + 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. + } }