Question
Java Question: THIS METHOD HAS TO BE DONE USING RECURSION. /** * Inserts the specified key-value pairinto the symbol table while maintaining the * ordering
Java Question:
THIS METHOD HAS TO BE DONE USING RECURSION.
/**
* Inserts the specified key-value pairinto the symbol table while maintaining the
* ordering of keys. Overwrites theold value with the new value if the symbol table
* already contains the specifiedkey. Deletes the specified key (and its associated
* value) from this symbol table if thespecified 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 makesure the list remains sorted! Also, solve this usingRECURSION.
// To do this, you will needto add a recursive helper function that takes the front of a
// list (Node) as an argumentand returns the front of the modified list (Node).
if (key == null) thrownew IllegalArgumentException("first argument to put() isnull");
if (val == null) {
delete(key);
return;
}
for (Node x = first;x != null; x = x.next) {
if (key.equals(x.key)) {
x.val = val;
return;
}
}
first = new Node(key,val, first);
n++;
}
-------------------------ADDITIONAL INFORMATION THAT YOU MIGHT WANTTO SEE-----------------------
public class RSequentialSearchST
private intn; //number of key-value pairs
private Nodefirst; // the linked list ofkey-value pairs
// a helper linked list data type
private class Node {
private Key key;
private Value val;
private Node next;
public Node(Key key,Value val, Node next) {
this.key = key;
this.val = val;
this.next = next;
}
}
/**
* Initializes an empty symbol table.
*/
public RSequentialSearchST() {
}
/**
* Returns the number of key-value pairs inthis symbol table.
*
* @return the number of key-value pairs inthis symbol table
*/
public int size() {
return n;
}
/**
* Returns true if this symbol table isempty.
*
* @return {@code true} if this symboltable is empty;
* {@code false}otherwise
*/
public boolean isEmpty() {
return size() ==0;
}
/**
* Returns true if this symbol tablecontains the specified key.
*
* @param key the key
* @return {@code true} if this symboltable contains {@code key};
* {@code false}otherwise
* @throws IllegalArgumentException if{@code key} is {@code null}
*/
public boolean contains(Key key) {
if (key == null) thrownew IllegalArgumentException("argument to contains() isnull");
return get(key) !=null;
}
/**
* Returns all keys in the symbol table asan {@code Iterable}.
* To iterate over all of the keys in thesymbol table named {@code st},
* use the foreach notation: {@code for(Key key : st.keys())}.
*
* NOTE: Java's LinkedList was use inplace of the book's Queue class.
*
* @return all keys in the symboltable
*/
public Iterable
LinkedList
for (Node x = first; x!= null; x = x.next)
queue.add(x.key);
return queue;
}
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