binart search tree toString doesnt print all the elements

as above, heres my code:
public class BSNode
{
  //instance variables
  private int data;
  private BSTree left;
  private BSTree right;
 
  /**
   * BSNode contructor
   * @param d a character to set data to
   * @postcondition a new node has been created with
   *           data set to d, right and left set to
   *           new null BSTree
   */
  public BSNode(int d)
  {
    data = d;
    left = new BSTree();
    right = new BSTree();
  }
   public int getRootData() {return data;}
   public BSTree getLeftChild() {return left;}
   public BSTree getRightChild() { return right; }
   public void setRootData(int d) {data = d;}
   public void setLeftChild(BSTree l) { left = l;}
   public void setRightChild(BSTree r) {left = r;}
}

 public class BSTree
{
  private BSNode root;

   public BSTree() {root = null;}
   public BSTree(BSNode n) { root = n;}
 
   public BSTree getLeft() {return root.getLeftChild();}
   public BSTree getRight() {return root.getRightChild();}
   public boolean emptyTree() { return(root == null);}
   public int getData() { return root.getRootData();}

   public String toString()
  {
    if(emptyTree())
      return " ";
    else
      return  
      getLeft().toString() +  
      getData() +
      getRight().toString();
  }

   public BSTree findData(int data)
  {
    BSTree temp;
    if(emptyTree() || data == getData())
      return this;
    if(data < getData())
    {
      temp = getLeft();
      return temp.findData(data);
    }
    else
    {
      temp = getRight();
      return temp.findData(data);
    }
  }

   public void setLeft(BSTree t) {root.setLeftChild(t);}
   public void setRight(BSTree t) {root.setRightChild(t);}

   public void insertData(int d)
  {
    BSNode n = new BSNode(d);
    if(!findData(d).emptyTree())
    {
      System.out.println("data already present");
      return;
    }
    if(emptyTree())
      root = n;
    else if (d < getData())
    {
      if(getLeft().emptyTree())
        root.setLeftChild(new BSTree(n));
      else
        getLeft().insertData(d);
    }
    else
    {
      if(getRight().emptyTree())
        root.setRightChild(new BSTree(n));
      else
        getRight().insertData(d);
    }
  }
}
 
DAJonesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
try this and tell me

   public String toString()
  {
    if(emptyTree())
      return " ";
    else
      return   getLeft().toString() +  String.valueOf(getData()) +  getRight().toString();
  }
0
petmagdyCommented:
if not enouph please provide ur main method so I can trace ur problem in toString()
0
DAJonesAuthor Commented:
nah, it gave me the same thing:
public class BSTreeTest
{
  public static void main(String[] args)
  {
    System.out.println("testing the methods of BSTree...");
    System.out.println();
    System.out.println("creating a BSTree object...");
    BSTree a = new BSTree();
    System.out.println();
    System.out.println("inserting integers 1-10");
    a.insertData(5);
    a.insertData(3);
    a.insertData(10);
    a.insertData(2);
    System.out.println(a);
  }
}
output:
testing the methods of BSTree...
creating a BSTree object...
inserting integers 1-10
 2 10 5
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

DAJonesAuthor Commented:
I think maybe my prob is in the findData or the insert if I change the order of insertions I get different stuff
eg
public class BSTreeTest
{
  public static void main(String[] args)
  {
    System.out.println("testing the methods of BSTree...");
    System.out.println("inserting integers);
    a.insertData(4);
    a.insertData(3);
    a.insertData(2);
    a.insertData(1);
    System.out.println(a);
  }
}
output:
inserting integers
 1 2 3 4

if I reverse the order, I get:
1 4

0
CEHJCommented:
You need to traverse the tree. At the moment you're just using the root node
0
petmagdyCommented:
ok i got it change insertNode to this:

  public void insertData(int d)
  {
    BSNode n = new BSNode(d);
    if(!findData(d).emptyTree())
    {
      System.out.println("data already present");
      return;
    }
    if(emptyTree())
      root = n;
    else if (d < getData())
    {
        getLeft().insertData(d);
    }
    else
    {
       getRight().insertData(d);
    }
  }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
I tested this, it solves the problem :-)
0
CEHJCommented:
>>binart search tree toString doesnt print all the elements

Can someone tell me how the accepted answer relates to the question?
0
petmagdyCommented:
the problem was not in toString() the problem was that inserting (insertData() method) into tree didn't insert correctly in all cases
Have a nice day all :-)
0
DAJonesAuthor Commented:
sorry, the problem wasn't in the toString at all, but in the insert the lines taken out were'n allowing all the elements to be inserted
thanks so much for the respones!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.