In Java I did an insert method for my program, but I want to make sure its correct, please check it and fix if theres any errors the code public void insert(E key) throws DuplicateItemException { BSTNode child = new BSTNode(key); if ( root == null ) { root = child; } else { BSTNode parent = insertionPoint(key); } try { BSTNode parent = insertionPoint(key); if (key.compareTo(parent.getKey()) < 0){ parent.setLeftChild(child); } else if (key.compareTo(parent.getKey()) > 0) { parent.setRightChild(child); } } catch (DuplicateItemException ex) { throw ex; } }
In Java
I did an insert method for my program, but I want to make sure its correct, please check it and fix if theres any errors
the code
public void insert(E key) throws DuplicateItemException {
BSTNode<E> child = new BSTNode(key);
if ( root == null ) {
root = child;
}
else {
BSTNode<E> parent = insertionPoint(key);
}
try {
BSTNode<E> parent = insertionPoint(key);
if (key.compareTo(parent.getKey()) < 0){
parent.setLeftChild(child);
}
else if (key.compareTo(parent.getKey()) > 0) {
parent.setRightChild(child);
}
}
catch (DuplicateItemException ex) {
throw ex;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images