Link to home
Start Free TrialLog in
Avatar of DAJones
DAJones

asked on

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);
    }
  }
}
 
Avatar of petmagdy
petmagdy
Flag of Canada image

try this and tell me

   public String toString()
  {
    if(emptyTree())
      return " ";
    else
      return   getLeft().toString() +  String.valueOf(getData()) +  getRight().toString();
  }
if not enouph please provide ur main method so I can trace ur problem in toString()
Avatar of DAJones
DAJones

ASKER

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
Avatar of DAJones

ASKER

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

Avatar of CEHJ
You need to traverse the tree. At the moment you're just using the root node
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tested this, it solves the problem :-)
>>binart search tree toString doesnt print all the elements

Can someone tell me how the accepted answer relates to the question?
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 :-)
Avatar of DAJones

ASKER

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!