Question
Can I get some help constructing working recursive functions for these? public Value get(Key key) { // TODO // Change this code to make use
Can I get some help constructing working recursive functions for these?
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 get(first,key); //return null; } private Value get(Node x,Key key) { if (x == null) return null; if(key.equals(x.key)) return x.val; else if(x.key.compareTo(key) > 0) return null; else get(x.next,key); //return x.val; //return x.next.val; //get(x.next,key); }
/** * Inserts the specified key-value pair into the symbol table while maintaining the * ordering of keys. Overwrites the old value with the new value if the symbol table * already contains the specified key. Deletes the specified key (and its associated * value) from this symbol table if the specified value is {@code null}. * * @param key the key * @param val the value * @throws IllegalArgumentException if {@code key} is {@code null} */ public void put(Key key, Value val) { // TODO // Change this code to make sure the list remains sorted! 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 front of the modified list (Node). if (key == null) throw new IllegalArgumentException("argument to delete() is null"); put(first,key,val); } private Node put(Node x,Key key, Value val) { if (x == null) first = new Node(key, val, first); return null; if(x.next.key.compareTo(key) < 0) { put(x.next,key,val); return x.next; } else Node node = new Node(key,val,x); }
/** * 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"); delete(first, key); }
// delete key in linked list beginning at Node x // warning: function call stack too large if table is large private Void delete(Node x, Key key) { // TODO // Modify this to terminate early when possible. if (x == null) return null; if (key.equals(x.key)) { n--; //delete(x); //return x.next; } if(x.key.compareTo(key) > 0){ return null; } delete(x.next, key); //return x; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started