The BinaryNode Class ==================== import java.util.ArrayList; import jav
ID: 3565685 • 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 { // create an empty node public BinaryNode() { this(null, null, null); } // create a node with given value and children public BinaryNode(T theElement, BinaryNode lt, BinaryNode rt) { element = theElement; left = lt; right = rt; } // return the element public T getElement() { return element; } // return the left child public BinaryNode getLeft() { return left; } // return the right child public BinaryNode 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) { left = t; } // set the right child // @param t is the right child public void setRight(BinaryNode t) { right = t; } // @return the size of the subtree at node t public static int size( BinaryNode 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 int height( BinaryNode 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 find(T inq) { if (element.equals(inq)) return this; BinaryNode 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 duplicate() { BinaryNode root = new BinaryNode(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> s = new Stack(); BinaryNode 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 left; private BinaryNode 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++;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.