Question: I need to rewrite this remove method in a Binary Search Tree class iteratively: public void remove(AnyType v) { root = remove(v, root); } private
I need to rewrite this remove method in a Binary Search Tree class iteratively:
public void remove(AnyType v) { root = remove(v, root); }
private Node
int compareResult = v.compareTo(t.getData());
if (compareResult < 0) t.setLeft(remove(v, t.getLeft())); else if (compareResult > 0) t.setRight(remove(v, t.getRight())); else if (t.getLeft() != null && t.getRight() != null) { Node
return t; }
Here is the entire BinarySearchTree class for reference:
import java.util.Iterator; import java.util.LinkedList;
public class BinarySearchTree
public Node(AnyType d, Node
public AnyType getData() { return data; }
public Node
public Node
public void setData(AnyType d) { data = d; }
public void setLeft(Node
public void setRight(Node
private Node
public BinarySearchTree() { makeEmpty(); }
public void makeEmpty() { root = null; }
public boolean isEmpty() { return (root == null); }
public boolean contains(AnyType v) { return contains(v, root); }
private boolean contains(AnyType v, Node
int compareResult = v.compareTo(t.getData());
if (compareResult < 0) return contains(v, t.getLeft()); else if (compareResult > 0) return contains(v, t.getRight()); else return true; }
public AnyType findMin() throws IllegalStateException { if (isEmpty()) throw new IllegalStateException("Search Tree is empty.");
Node
private Node
return findMin(t.getLeft()); }
public AnyType findMax() throws IllegalStateException { if (isEmpty()) throw new IllegalStateException("Search Tree is empty.");
Node
private Node
return findMax(t.getRight()); }
public void insert(AnyType v) {
root = insert(v, root); }
private Node
// if the root is empty, then set the node to be inserted as the root node if (t == null) return (node);
// declare a parent node initially set to null Node
// and a current node set to the root Node
// compare the value of the root to the value you want to insert int compareRoot = v.compareTo(t.getData());
while (curr != null) { // if current isn't null, set the parent equal to current parent = curr; // if value being inserted is less than the root, then move current to // the left if (compareRoot < 0) curr = curr.getLeft(); // else move current to the right else curr = curr.getRight(); }
int compareParent = (parent.getData()).compareTo(v); // if the parent node is less than the current node, insert the current // node to the right if (compareParent < 0) parent.setRight(node); // else insert the node to the left else parent.setLeft(node);
return t; }
/*public void remove(AnyType v) { // Write the iterative version of the remove method here.
}*/ public void remove(AnyType v) { root = remove(v, root); }
private Node
int compareResult = v.compareTo(t.getData());
if (compareResult < 0) t.setLeft(remove(v, t.getLeft())); else if (compareResult > 0) t.setRight(remove(v, t.getRight())); else if (t.getLeft() != null && t.getRight() != null) { Node
return t; }
public void printTree() { // Write the code to print out the values in the tree // in a tree like form as described in the assignment // description.
}
public Iterator
inOrderTraversal(root, snapshot); return snapshot.iterator(); }
private void inOrderTraversal(Node
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
