Menu

(Solved) : Get Help Constructing Working Recursive Functions Public Value Get Key Key Todo Change Cod Q43947072 . . .

Can I get some help constructing working recursive functions forthese?

public Value get(Key key) {
   // TODO
   // Change this code to make use of the fact the listis sorted to terminate early
   // when possible. Also, solve this using recursion. Todo this, you will need to
   // add a recursive helper function that takes thefront of a list (Node) as an argument
   // and returns the correct value.
if (key == null) throw new IllegalArgumentException(“argument toget() 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)
            returnnull;
        else
           get(x.next,key);
    //return x.val;
            //returnx.next.val;
           //get(x.next,key);
   
   
}

/**
* Inserts the specified key-value pair into the symbol table whilemaintaining the
* ordering of keys. Overwrites the old value with the new value ifthe symbol table
* already contains the specified key. Deletes the specified key(and its associated
* value) from this symbol table if the specified value is {@codenull}.
*
* @param key the key
* @param val the value
* @throws IllegalArgumentException if {@code key} is {@codenull}
*/
public void put(Key key, Value val) {
   // TODO
   // Change this code to make sure the list remainssorted! Also, solve this using recursion.
   // To do this, you will need to add a recursive helperfunction that takes the front of a
   // list (Node) as an argument and returns the front ofthe modified list (Node).
   if (key == null) throw newIllegalArgumentException(“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 = newNode(key,val,x);
  
}

/**
* Removes the specified key and its associated value from thissymbol table   
* (if the key is in this symbol table). Takes advantage of the factthat the
* keys appear in increasing order to terminate` early whenpossible.
*
* @param key the key
* @throws IllegalArgumentException if {@code key} is {@codenull}
*/
public void delete(Key key) {
   // TODO
   // Change this code to make use of the fact that thelist is sorted to
   // terminate early when possible.
   // As this code already uses recursion, the privatehelper function is
   // already present below, but it will need to bechanged to terminate
   // early when appropriate.
if (key == null) throw new IllegalArgumentException(“argument todelete() 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){
           returnnull;
}
delete(x.next, key);
//return x;
}

Expert Answer


Answer to Can I get some help constructing working recursive functions for these? public Value get(Key key) { // TODO // Change th…

OR