Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Binary Tree insert(int value) method

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binary Tree insert(int value) method

    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);
          }
     
        }
     
     
      }
    Last edited by geeksutopia; March 25th, 2014 at 12:35 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Binary Tree insert(int value) method

    it doesn't do so well
    Please explain.

    What should the program's output look like?

    The output that was posted is hard to read because it appears that the lines are run together. Can you separate the lines to make it easier to read?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Tree insert(int value) method

    Quote Originally Posted by Norm View Post
    Please explain.

    What should the program's output look like?

    The output that was posted is hard to read because it appears that the lines are run together. Can you separate the lines to make it easier to read?
    Its supposed to take the different arguments and print out the right and left subtree, problem is, it reads the root but returns null for both left and right subtrees

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Binary Tree insert(int value) method

    Can you post a sample of what the output should look like for the data that current program has?

    returns null for both left and right subtrees
    Try debugging the code by adding some println() statements that print out the values of variables as they are changed so you can see what the computer sees as the code is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Tree insert(int value) method

    Quote Originally Posted by Norm View Post
    Can you post a sample of what the output should look like for the data that current program has?


    Try debugging the code by adding some println() statements that print out the values of variables as they are changed so you can see what the computer sees as the code is executed.
    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);
          }
     
        }
     
     
      }
    Last edited by GregBrannon; March 25th, 2014 at 02:14 PM. Reason: Removed spaces from opening code tag.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Binary Tree insert(int value) method

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Binary Tree insert(int value) method

    One hole in the code is the missing else statement at the end of the if/else if chain. There should be code there to catch missed conditions.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Tree insert(int value) method

    Wait, where at?

  9. #9
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Tree insert(int value) method

    Quote Originally Posted by Norm View Post
    One hole in the code is the missing else statement at the end of the if/else if chain. There should be code there to catch missed conditions.
    Where is that?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Binary Tree insert(int value) method

    See the code in post#5
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Binary Tree insert(int value) method

    In the code posted in the first post, you also misspelled your last name in the part where you open the file, Sotres instead of Satres. That's where the error opening file comes from most likely.

Similar Threads

  1. (Binary Tree) Family tree - help with my addChild method
    By Pip_Squeak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2014, 07:52 AM
  2. Binary Search Tree Lookup Method
    By Sbonett in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 15th, 2012, 07:43 AM
  3. a recorsive method to get level of a certin leaf on a binary tree???
    By oshikoren in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 11:27 AM
  4. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  5. Trouble with Binary Search on Insert Method
    By EricSt in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 17th, 2010, 10:34 PM

Tags for this Thread