So here is the code that I have. The problem is my insert method. It compiles, but it doesn't do so well in the test class I have set up for it.
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 BinaryTree getRoot() { return this.root; } public void insert(int value){ if(root == null){//starts a new Binary tree root = new BinaryTree(value); } else 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, List<Integer> list){ System.out.println(leaf); if(leaf != null){ inOrder(leaf.leftSubtree, list); //System.out.print(leaf.data+""); list.add(leaf.getData()); inOrder(leaf.rightSubtree, list);//Have it return an arrayList. } else { } } 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("C:\\Users\\Carlos Sotres\\Documents\\SystemandDesign\\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"); System.err.println(e); } } public void closeFile(){ try{ if(input != null) input.close(); }catch(IOException ioException){ System.err.println("Error closing file."); System.exit(1); } } }
Here is a look at my test class:
package SystemandDesign.BinaryTree; import junit.framework.TestCase; import java.util.List; import java.util.ArrayList; public class BinaryTreeTest extends TestCase{ BinaryTree d1; BinaryTree d2; BinaryTree d3; BinaryTree d4; BinaryTree d5; BinaryTree d6; BinaryTree d7; BinaryTree d8; public void setUp(){ d1 = new BinaryTree(2); d1.openFile(); 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,d3); // // // } public void testinOrder(){ List<Integer> leaves = new ArrayList<Integer>(); d1.insert(1); d1.insert(2); d1.insert(3); System.out.println(d1.getRoot()); d1.inOrder(d1.getRoot(), leaves); System.out.println(leaves); } }
Here is a copy of the console:
Class edu.rice.cs.plt.lambda.SimpleBox@156c089added to classNames. File C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary Tree.java added to files.
Class edu.rice.cs.plt.lambda.SimpleBox@1ce18cadded to classNames. File C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary TreeTest.java added to files.
Error opening file.
SystemandDesign.BinaryTree.BinaryTree@b93ab6
SystemandDesign.BinaryTree.BinaryTree@b93ab6
null
null
[1]
Its supposed to walk the BinaryTree inOrder Traverse, and print out the array in that order, here is my modification to the insertMethod, it looks like it reads the root and and rightSubtree with the current test parameters
public void insert(int value){ if(root == null){//starts a new Binary tree root = new BinaryTree(value); System.out.println("yes Root"); } else 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){ System.out.println("yes Left Subtree"); leftSubtree = new BinaryTree(value); } else{ leftSubtree.insert(value); } } else if(value > data){//right side of binary tree if(rightSubtree == null){ System.out.println("yes Right Subtree"); rightSubtree = new BinaryTree(value); } else{ rightSubtree.insert(value); } } }