Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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, Value> {
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 keys() {
LinkedListqueue = new 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions

Question

As clearly as possible, define plane stress and plane strain.

Answered: 1 week ago