• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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);
    }
  }
}
 
0
DAJones
Asked:
DAJones
  • 5
  • 3
  • 2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now