/** * Returns the value associated with the given key 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 * @return the value associated with the given key if the key is in the symbol table * and {@code null} if the key is not in the symbol table * @throws IllegalArgumentException if {@code key} is {@code null} */ public Value get(Key key) { // TODO // Change this code to make use of the fact the list is sorted to terminate early // when possible. Also, solve this using recursion. To do this, you will need to // add a recursive helper function that takes the front of a list (Node) as an argument // and returns the correct value. if (key == null) throw new IllegalArgumentException("argument to get() is null"); for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) return x.val; } return null; } You may not use the keys() method in your code. You are modifying the implementation of the methods, but not their interface or contract. Please use the nodes instead of "left" and "right".
/**
* Returns the value associated with the given key 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
* @return the value associated with the given key if the key is in the symbol table
* and {@code null} if the key is not in the symbol table
* @throws IllegalArgumentException if {@code key} is {@code null}
*/
public Value get(Key key) {
// TODO
// Change this code to make use of the fact the list is sorted to terminate early
// when possible. Also, solve this using recursion. To do this, you will need to
// add a recursive helper function that takes the front of a list (Node) as an argument
// and returns the correct value.
if (key == null) throw new IllegalArgumentException("argument to get() is null");
for (Node x = first; x != null; x = x.next) {
if (key.equals(x.key))
return x.val;
}
return null;
}
You may not use the keys() method in your code. You are modifying the implementation of the
methods, but not their interface or contract. Please use the nodes instead of "left" and "right".
Step by step
Solved in 4 steps with 5 images