(Solved) : Using Java Modify 3 Methods Put Get Delete Rsequentialsearchstjava List Maintained Increas Q43942714 . . .
Using java:
Modify the 3 methods, put, get, and delete inRSequentialSearchST.java so that the list is maintained inincreasing order of the keys. When implementing get and delete,make sure to take advantage of the fact that the list is in orderto avoid looking at all the items in the list. All of thesefunctions must use recursion.This means each one will needa private helper function that takes a node (the front of the list)as an additional argument. Your code cannot contain any loopsexcept in the keys() method and the checkInvariant() method whichyou are not changing. You also may not use the keys() method inyour code. Note: You are modifying the implementation of themethods, but not their interface. To the external world, themethods should behave exactly as before. Make sure you code takesadvantage of the sorted order.
* The {@code SequentialSearchST} class represents an(unordered)
* symbol table of generic key-value pairs.
* It supports the usual <em>put</em>,<em>get</em>, <em>contains</em>,
* <em>delete</em>, <em>size</em>, and<em>is-empty</em> methods.
* It also provides a <em>keys</em> method for iteratingover all of the keys.
* A symbol table implements the <em>associativearray</em> abstraction:
* when associating a value with a key that is already in the symboltable,
* the convention is to replace the old value with the newvalue.
* The class also uses the convention that values cannot be {@codenull}. Setting the
* value associated with a key to {@code null} is equivalent todeleting the key
* from the symbol table.
* <p>
* This implementation uses a singly-linked list and sequentialsearch.
* It relies on the {@code equals()} method to test whether twokeys
* are equal. It does not call either the {@code compareTo()}or
* {@code hashCode()} method.
* The <em>put</em> and <em>delete</em>operations take linear time; the
* <em>get</em> and <em>contains</em>operations takes linear time in the worst case.
* The <em>size</em>, and <em>is-empty</em>operations take constant time.
* Construction takes constant time.
* <p>
* For additional documentation, see <ahref=”http://algs4.cs.princeton.edu/31elementary”>Section3.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewickand Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
import java.util.LinkedList;
public class RSequentialSearchST<Key extendsComparable<Key>, Value> {
private int n; // number of key-value pairs
private Node first; // the linked list of key-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 in this symbol table.
*
* @return the number of key-value pairs in this symbol table
*/
public int size() {
return n;
}
/**
* Returns true if this symbol table is empty.
*
* @return {@code true} if this symbol table is empty;
* {@code false} otherwise
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* Returns true if this symbol table contains the specifiedkey.
*
* @param key the key
* @return {@code true} if this symbol table contains {@codekey};
* {@code false} otherwise
* @throws IllegalArgumentException if {@code key} is {@codenull}
*/
public boolean contains(Key key) {
if (key == null) throw new IllegalArgumentException(“argument tocontains() is null”);
return get(key) != null;
}
/**
* Returns all keys in the symbol table as an {@codeIterable}.
* To iterate over all of the keys in the symbol table named {@codest},
* use the foreach notation: {@code for (Key key :st.keys())}.
*
* NOTE: Java’s LinkedList was use in place of the book’s Queueclass.
*
* @return all keys in the symbol table
*/
public Iterable<Key> keys() {
LinkedList<Key> queue = new LinkedList<Key>();
for (Node x = first; x != null; x = x.next)
queue.add(x.key);
return queue;
}
/**
* Returns the value associated with the given key in this symboltable.
* Takes advantage of the fact that the keys appear in increasingorder to terminate
* early when possible.
*
* @param key the key
* @return the value associated with the given key if the key is inthe symbol table
* and {@code null} if the key is not in the symbol table
* @throws IllegalArgumentException if {@code key} is {@codenull}
*/
public Value get(Key key) {
// TODO
// Change this code to make use of the fact the list is sortedto 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 alist (Node) as an argument
// and returns the correct value.
if (key == null)throw newIllegalArgumentException(“argument to get() is null”);
for (Node x = first; x !=null; x = x.next) {
if (key.equals(x.key))
return x.val;
}
return null;
}
/**
* Inserts the specified key-value pair into thesymbol table while maintaining the
* ordering of keys. Overwrites the old value withthe new value if the symbol table
* already contains the specified key. Deletes thespecified key (and its associated
* value) from this symbol table if the specifiedvalue is {@code null}.
*
* @param key the key
* @param val the value
* @throws IllegalArgumentExceptionif {@code key} is {@code null}
*/
public void put(Key key, Valueval) {
// 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 functionthat takes the front of a
// list (Node) as an argument and returns the front of themodified list (Node).
if (key == null)throw newIllegalArgumentException(“first argument to put() is null”);
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++;
}
/**
* Removes the specified key and its associatedvalue from this symbol table
* (if the key is in this symbol table). Takesadvantage of the fact that the
* keys appear in increasing order to terminate`early when possible.
*
* @param key the key
* @throws IllegalArgumentExceptionif {@code key} is {@code null}
*/
public void delete(Key key){
// TODO
// Change this code to make use of the fact that the list issorted to
// terminate early when possible.
// As this code already uses recursion, the private helperfunction is
// already present below, but it will need to be changed toterminate
// early when appropriate.
if (key == null)throw newIllegalArgumentException(“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;
}
public booleancheckInvariant() {
if (first == null)
return true;
Node current = first;
Node next = first.next;
while (next != null) {
if (current.key.compareTo(next.key) > 0)
return false;
current = next;
next = current.next;
}
return true;
}
}
Expert Answer
Answer to Using java: Modify the 3 methods, put, get, and delete in RSequentialSearchST.java so that the list is maintained in inc…
OR