Question

In JAVA... Implement size(), delete(), and keys() for SequentialSearchST.

In JAVA...

Implement size(), delete(), and keys() for SequentialSearchST.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class SequentialSearchST<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 SequentialSearchST() {
}

/**
* Returns the number of key-value pairs in this symbol table.
* (atrate)return the number of key-value pairs in this symbol table
*/
public int size() {
return n;
}

/**
* Is this symbol table empty?
* (atrate)return {(atrate)code true} if this symbol table is empty and {(atrate)code false} otherwise
*/
public boolean isEmpty() {
return size() == 0;
}

/**
* Does this symbol table contain the given key?
* (atrate)param key the key
* (atrate)return {(atrate)code true} if this symbol table contains {(atrate)code key} and
* {(atrate)code false} otherwise
*/
public boolean contains(Key key) {
return get(key) != null;
}

/**
* Returns the value associated with the given key.
* (atrate)param key the key
* (atrate)return the value associated with the given key if the key is in the symbol table
* and {(atrate)code null} if the key is not in the symbol table
*/
public Value get(Key key) {
for (Node x = first; x != null; x = x.next) {
if (key.equals(x.key))
return x.val;
}
return null;
}

/**
* Inserts the key-value pair into the symbol table, overwriting the old value
* with the new value if the key is already in the symbol table.
* If the value is {(atrate)code null}, this effectively deletes the key from the symbol table.
* (atrate)param key the key
* (atrate)param val the value
*/
public void put(Key key, Value val) {
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 key and associated value from the symbol table
* (if the key is in the symbol table).
* (atrate)param key the key
*/
public void delete(Key key) {
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) {
if (x == null) return null;
if (key.equals(x.key)) {
n--;
return x.next;
}
x.next = delete(x.next, key);
return x;
}


/**
* Returns all keys in the symbol table as an {(atrate)code Iterable}.
* To iterate over all of the keys in the symbol table named {(atrate)code st},
* use the foreach notation: {(atrate)code for (Key key : st.keys())}.
* (atrate)return all keys in the symbol table as an {(atrate)code Iterable}
*/
public Iterable<Key> keys() {
Queue<Key> queue = new Queue<Key>();
for (Node x = first; x != null; x = x.next)
queue.enqueue(x.key);
return queue;
}


/**
* Unit tests the {(atrate)code SequentialSearchST} data type.
*
* (atrate)param args the command-line arguments
*/
public static void main(String[] args) {
SequentialSearchST<String, Integer> st = new SequentialSearchST<String, Integer>();
for (int i = 0; !StdIn.isEmpty(); i++) {
String key = StdIn.readString();
st.put(key, i);
}
for (String s : st.keys())
StdOut.println(s + " " + st.get(s));
}
}

Add a comment
Know the answer?
Add Answer to:
In JAVA... Implement size(), delete(), and keys() for SequentialSearchST.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT