Question

hi, I am suppose to implement that method in java for bst but the result doesn't...

hi, I am suppose to implement that method in java for bst but the result doesn't work as excpected

* _Part 8: Implement this method._
 *
 * toString() is a method defined by Java's Object class
 * that can be used to provide a String representation for your
 * class.  It is called anytime you concatenate an Object to
 * a String.
 *
 * Build a string representation of the BST that describes both
 * the values stored inside and the shape of the structure; the
 * approach is described below.
 *
 * 1) Build a String representation of the tree
 *   performing a pre-order traversal of the tree.
 * 2) As each node is "visited" call the .toString() method on the
 *    data payload for that node, and add this value to the string.
 * 3) The printed contents of each node should follow the following format
 *       "(  )"
 * 4) A null Node reference should also be delimited with ()
 *
 * For example an empty tree (no nodes) should return the following:
 * "()"
 * A tree with one root node, whose data is "A" should return:
 * "(A ()())"
 * A tree with a root whose data is "B" and whose left child contains
 * "A" and whose right child contains "C" should return:
 * "(B (A ()())(C ()()))"
 *
 * @return - A String representation of the tree
 *
 */
public String toString() {

   String openpa = "(";

   if (root!=null) {


      openpa = openpa + root.getData() + " ";

      //left child
      Node current = getRoot().getLeft();
      while (current != null) {

         //if left child is null, print the current node, then move to right child
         if (current.getLeft() == null) {
            openpa = openpa + "(" + current.getData()+ "()())";
            current = current.getRight();
         } else {

            //find predecessor
            Node inopre = current.getLeft();
            while (inopre.getRight() != null && inopre.getRight() != current) {
               inopre = inopre.getRight();
            }

            //if the right child of predecessor = current
            if (inopre.getRight() == current) {
               inopre.right = null;

               //set predecessor right child to null
               // move to the right

               current = current.getRight();

            } else {

               //if not, then print this node
               openpa = openpa + "(" + current.getData() + ")";

               //reset inorder predecessor right child to this node
               inopre.right = current;

               //move left
               current = current.getLeft();
            }
         }
      }
   }

   return openpa + ")";
}
/**
    *
    * The Node class for our BST.  The BST
    * as defined above is constructed from zero or more
    * Node objects. Each object has between 0 and 2 children
    * along with a data member that must implement the
    * Comparable interface.
    *
    * @param <T>
    */

   public static class Node<T extends Comparable<T>>

   {
      private Node<T> parent;
      private Node<T> left;
      private Node<T> right;
      private T data;
      
      private Node(T d) {
         data = d;
         parent = null;
         left = null;
         right = null;
      }
      public Node<T> getParent() { return parent; }
      public Node<T> getLeft() { return left; }
      public Node<T> getRight() { return right; }
      public T getData() { return data; }
   }
public static void main (String[] args){

      BST tree=new BST();
      tree.add(10);
      tree.add(8);
      tree.add(50);
      tree.add(9);
      tree.add(30);
      tree.add(70);
      tree.add(35);

      System.out.println(tree.rank(10));
      System.out.println(tree.toString());
}
}

OUTPUT

(10 (8()())(9()())(50)(30()())(35()())(70()()))

Process finished with exit code 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since the remaining code of the BST class is not provided, I’m attaching only the toString() method which I have fixed. Also an overloaded version of toString() method is given, both of them are needed, the second one actually performs recursion and return the String in pre order traversal format. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//returns the string representation of pre order traversal of elements

public String toString() {

      // calling the recursive method to do the traversal and return the

      // string encapsulated inside ()

      return "(" + toString(root) + ")";

}

// helper method to traverse bst in pre order and return a String

// representation of pre order traversal

private String toString(Node<T> node) {

      if (node == null) {

            // end condition

            return "";

      }

      // concatenating this node's data, with the result of preorder traversal

      // of left subtree and that of right subtree in proper format

      return node.data + " (" + toString(node.left) + ")("

                  + toString(node.right) + ")";

}

Add a comment
Know the answer?
Add Answer to:
hi, I am suppose to implement that method in java for bst but the result doesn't...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT