** * Removes the specified key and its associated value from this symbol table * (if the key is in this symbol table). Takes advantage of the fact that the * keys appear in increasing order to terminate` early when possible. * * @param key the key * @throws IllegalArgumentException if {@code key} is {@code null} */ public void delete(Key key) { // TODO // Change this code to make use of the fact that the list is sorted to // terminate early when possible. // As this code already uses recursion, the private helper function is // already present below, but it will need to be changed to terminate // early when appropriate. if (key == null) throw new IllegalArgumentException("argument to delete() is null"); first = delete(first, key); } // delete key in linked list beginning at Node x // warning: function call stack too large if table is large private Node delete(Node x, Key key) { // TODO // Modify this to terminate early when possible. if (x == null) return null; if (key.equals(x.key)) { n--; return x.next; } x.next = delete(x.next, key); return x; } } You may not use the keys() method in your code. You are modifying the implementation of the methods, but not their interface or contract.
/**
* Removes the specified key and its associated value from this symbol table
* (if the key is in this symbol table). Takes advantage of the fact that the
* keys appear in increasing order to terminate` early when possible.
*
* @param key the key
* @throws IllegalArgumentException if {@code key} is {@code null}
*/
public void delete(Key key) {
// TODO
// Change this code to make use of the fact that the list is sorted to
// terminate early when possible.
// As this code already uses recursion, the private helper function is
// already present below, but it will need to be changed to terminate
// early when appropriate.
if (key == null) throw new IllegalArgumentException("argument to delete() is null");
first = delete(first, key);
}
// delete key in linked list beginning at Node x
// warning: function call stack too large if table is large
private Node delete(Node x, Key key) {
// TODO
// Modify this to terminate early when possible.
if (x == null)
return null;
if (key.equals(x.key)) {
n--;
return x.next;
}
x.next = delete(x.next, key);
return x;
}
}
You may not use the keys() method in your code. You are modifying the implementation of the
methods, but not their interface or contract.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps