Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The BinaryNode Class ==================== import java.util.ArrayList; import jav

ID: 3565593 • Letter: T

Question

 The BinaryNode Class                     ====================      import java.util.ArrayList; import java.util.Stack; import java.lang.reflect.Array; import java.util.LinkedList; // to simulate a queue  public class BinaryNode<T>  {          // create an empty node     public BinaryNode()      {         this(null, null, null);     }          // create a node with given value and children     public BinaryNode(T theElement, BinaryNode<T> lt, BinaryNode<T> rt)     {         element = theElement;         left = lt;         right = rt;     }          // return the element     public T getElement()     {         return element;     }          // return the left child     public BinaryNode<T> getLeft()     {         return left;     }          // return the right child     public BinaryNode<T> getRight()     {         return right;     }          // set the element to a given value     // @param x is the new value     public void setElement( T x)     {         element = x;     }          // set the left child     // @param t is the left child     public void setLeft(BinaryNode<T> t)     {         left = t;     }          // set the right child     // @param t is the right child     public void setRight(BinaryNode<T> t)     {         right = t;     }          // @return the size of the subtree at node t      public static <T> int size( BinaryNode<T> t)     {         if ( t == null)             return 0;         else             return 1 + size(t.left) + size(t.right);     }          // @return the height of the subtree of node t     public static <T> int height( BinaryNode<T> t)     {         if (t == null)             return -1;         else              return 1 + Math.max(height(t.left), height(t.right));     }      // find if an item occurs in the tree     // @param inq is the inquiry     public BinaryNode<T> find(T inq)     {         if (element.equals(inq))             return this;         BinaryNode<T> out = null;         if ( left != null)         {             out = left.find(inq);         }             if ( out != null)             return out;         else if ( right != null)             out = right.find(inq);         return out;     }              // create a duplicate tree     // @return the root of the duplicate tree     public BinaryNode<T> duplicate()     {         BinaryNode<T> root = new BinaryNode<T>(element,null,null);         if ( left != null)             // create a duplicate tree for the left subtree             root.left = left.duplicate();         if (right != null)             root.right = right.duplicate();         return root;     }               // print the tree elements in preorder     public void printPreOrder()     {         System.out.print(element + ", ");         if (left != null)             left.printPreOrder();         if (right != null)             right.printPreOrder();     }              // print the tree elements in postorder // iterativeInorder     public void iterativePreOrder()     {                Stack<BinaryNode<T>> s = new Stack();         BinaryNode<T> current = this;         while (current != null || !s.empty())         {             if (current != null)             {                 System.out.println(current.element);                 // process it, put it in the stack, and go left                 s.push(current);                 current = current.left;             }             else // pop the stack and go right             {                 current = s.pop();                 current = current.right;             }         }         }                      // print in post order     public void printPostOrder()     {         if (left != null)             left.printPostOrder();         if (right != null)             right.printPostOrder();         System.out.println(element);     }              // print the tree elements in inorder     public void printInOrder()     {         if (left != null)             left.printInOrder();         System.out.println(element);         if (right != null)             right.printInOrder();     }         // the fields     private T element;     private BinaryNode<T> left;     private BinaryNode<T> right; }  ******Need to answer this question:!!  Method 7. Add the method public BinaryNode parent(BinaryNode n) that finds the parent of the node n in this tree. If n = null throw an IllegalArgumentException. If n = this or n does not occur in the tree return null 

Explanation / Answer

//Print by breadth search
    public void printByLevels(BinaryNode<T> root) {
       if(root != null) {
           ArrayList<BinaryNode<T>> nodes = new ArrayList<BinaryNode<T>>();
            int level = 1;
            nodes.add(root);
            while(nodes != null && nodes.size() > 0) {
               System.out.println( "Level - "+level+ " : Nodes : ");
               int size = nodes.size();
               ArrayList<BinaryNode<T>> temp = new ArrayList<BinaryNode<T>>();
               for(int i = 0; i < size; i++) {
                   BinaryNode<T> cur = nodes.get(i);
                   System.out.print(cur.getElement()+" , ");
                  
                   if(cur.left != null)
                       temp.add(cur.left);
                   if(cur.right != null)
                       temp.add(cur.right);
               }
              
               nodes = temp;
               level++;
              
            }
       }
    }