So, managed to get my Binary Search Tree to work properly:
package SystemandDesign.BinaryTree; import java.io.FileInputStream; import java.io.IOException; import java.util.List; import java.io.ObjectInputStream; import java.util.Scanner; public class BinaryTree{ private int data; private List<Integer> list; private BinaryTree root; private BinaryTree leftSubtree; private BinaryTree rightSubtree; private ObjectInputStream input; public BinaryTree(int value){ this.data = value; this.leftSubtree = null; this.rightSubtree = null; this.root = null; } public int getData(){ return this.data; } public void insert(int value){ if(root == null){//starts a new Binary tree root = new BinaryTree(value); } if(value < data){//if the data being inserted is less than the original data, it will be inserted to the left side. if(leftSubtree == null){ leftSubtree = new BinaryTree(value); } else{ leftSubtree.insert(value); } } else if(value > data){//right side of binary tree if(rightSubtree == null){ rightSubtree = new BinaryTree(value); } else{ rightSubtree.insert(value); } } } public void inOrder(BinaryTree leaf){ if(leaf != null){ inOrder(leaf.leftSubtree); System.out.print(leaf.data+""); inOrder(leaf.rightSubtree); } } public static void main(String[] args){ BinaryTree tree = new BinaryTree(9); tree.openFile(); tree.readFile(); tree.closeFile(); } public void openFile(){ try{ input = new ObjectInputStream(new FileInputStream("data.ser")); }catch(IOException ioException){ System.err.println("Error opening file."); } } public void readFile(){ try{ Scanner in = new Scanner(new FileInputStream("data.ser")); }catch(IOException e){ System.err.println("Error class not found."); } } public void closeFile(){ try{ if(input != null) input.close(); }catch(IOException ioException){ System.err.println("Error closing file."); System.exit(1); } } }
Now I need to create a test class for it
package SystemandDesign.BinaryTree; import junit.framework.TestCase; public class BinaryTreeTest extends TestCase{ BinaryTree d1; BinaryTree d2; BinaryTree d3; BinaryTree d4; BinaryTree d5; BinaryTree d6; BinaryTree d7; BinaryTree d8; private BinaryTree leftSubtree; private BinaryTree rightSubtree; public void setUp(){ d1 = new BinaryTree(2); d2 = new BinaryTree(5); d3 = new BinaryTree(9); d4 = new BinaryTree(10); d5 = new BinaryTree(12); d6 = new BinaryTree(19); d7 = new BinaryTree(3); d8 = new BinaryTree(4); } public void testInsert(){ assertEquals(d1 == d2); } public void testinOrder(){ } }
No matter how many times I keep rewritting the testInsert method I get a chock full of errors.