Im suppose to make a preOrder transversal method so that it
Im suppose to make a preOrder transversal method so that it performs a preOrder transversal of the tree.
For each node it traverses, it should call sb.append() to add a \"[\" the results of traversing the subtree rooted at that node, and then a \"]\". You should add a space before the results of the left and right child traversals, but only if the node exists.
This is what i have so far
public class BinarySearchTree<E extends Comparable<E>> extends AbstractSet<E> {
 protected Entry<E> root;
protected int size;
/**
 * Initializes this BinarySearchTree object to be empty, to contain only
 * elements of type E, to be ordered by the Comparable interface, and to
 * contain no duplicate elements.
 */
 public BinarySearchTree() {
 root = null;
 size = 0;
 }
/**
 * Initializes this BinarySearchTree object to contain a shallow copy of a
 * specified BinarySearchTree object. The worstTime(n) is O(n), where n is the
 * number of elements in the specified BinarySearchTree object.
 *
 * @param otherTree - the specified BinarySearchTree object that this
 * BinarySearchTree object will be assigned a shallow copy of.
 */
 public BinarySearchTree(BinarySearchTree<? extends E> otherTree) {
 root = copy(otherTree.root, null);
 size = otherTree.size;
 }
/* Method to complete is here! */
 public void preOrder(Entry<E> ent, StringBuilder sb) {
 // TODO: Complete me!
    if(ent == null){
        return;
    }
    if(ent.left != null){
        sb.append(\"[\");
        preOrder(ent.left, sb);
    }
    if(ent.right != null){
        sb.append(\"[\");
        preOrder(ent.right, sb);
    }
      
    sb.append(\"]\");
   
       
 }
But my preOrder method gives me these errors
The following hint(s) may help you locate some ways in which your solution may be improved:
--Pre-order traversal should process the root, then its left child (and exclude the non-existent right child)! Expected [t [o]], but was []]
--Should just include the root\'s element when the tree only has 1 element! Expected [z], but was ]
--Pre-order traversal should process the root, ignore the non-existent left child, then process the right child! Expected [w [z]]. but was []]
--Pre-order traversal should process the root, the left child, and then the right child! Expected [9 [0] [d]], but was [][]]
--Pre-order traversal should process the root, and then the left child. In processing the left child, it should process that node first, and then process its children! Expected [d [9 [d]]], but was [[]]]
--Pre-order traversal should process the root, and then the right child. In processing the right child, it should process that node first, and then process its children! Expected [v [w [z]]], but was [[]]]
--Pre-order traversal should process a node and then recursively process each of its children! Expected [v [d [9 [0] [d]] [j [g] [t [o]]]] [w [z]]], but was [[[][]][[][[]]]][[]]]
--Pre-order traversal should process a node and then recursively process each of its children! Expected [v [d [9 [0] [d]] [j [g] [t]]]], but was [[[][]][[][]]]]
Solution
import java.util.Stack;
 
 /* Class containing left and right child of current
 node and key value*/
 class Node {
 
 int data;
 Node left, right;
 
 public Node(int item) {
 data = item;
 left = right = null;
 }
 }
 
 /* Class to print the inorder traversal */
 class BinaryTree {
 
 Node root;
 
 void preorder() {
 if (root == null) {
 return;
 }
   
 //keep the nodes in the path that are waiting to be visited
 Stack<Node> stack = new Stack<Node>();
 Node node = root;
 
 //first node to be visited will be the left one
 while (node != null) {
 stack.push(node);
 node = node.left;
 }
 
 // traverse the tree
 while (stack.size() > 0) {
 
 // visit the top node
 node = stack.pop();
 System.out.print(node.data + \" \");
 if (node.right != null) {
 node = node.right;
 
 // the next node to be visited is the leftmost
 while (node != null) {
 stack.push(node);
 node = node.left;
 }
 }
 }
 }
 
 public static void main(String args[]) {
 
 /* creating a binary tree and entering
 the nodes */
 BinaryTree tree = new BinaryTree();
 tree.root = new Node(1);
 tree.root.left = new Node(2);
 tree.root.right = new Node(3);
 tree.root.left.left = new Node(4);
 tree.root.left.right = new Node(5);
 tree.preorder();
 }
 }



