Using java and no libraries Q3.1 Develop a symbol-table implementation ArrayST t
ID: 3607370 • Letter: U
Question
Using java and no libraries
Q3.1 Develop a symbol-table implementation ArrayST that uses an (unordered) array as the underlying data structure to implement our basic symbol-table API.
Q3.2 Self-organizing search. A self-organizing search algorithm is one that rearranges items to make those that are accessed frequently likely to be found early in the search. Modify your search implementation for Q3.1 to perform the following action on every search hit: move the key-value pair found to the beginning of the list, moving all pairs between the beginning of the list and the vacated position to the right one position. This procedure is called the move-to-front heuristic.
Explanation / Answer
Solution:
Q1 is solved, please repost the other one.
Symbol table are key-value pairs, An implementation of them is given below:
class UnorderedArrayST {
private:
int *Key;
item **val;
int n;
public:
UnorderedST(int capacity) {
keys = new int[capacity];
vals = new *item[capacity];
n = capacity;
}
item *get(int key) {
for (int i = 0; i < N; i++)
if (keys[i].equals(key))
return vals[i];
return null;
}
void put(int key, item *val) {
if (val == null) { // remove key from table if value is null
delete(key);
return;
}
// if key exists, overrides the old value (avoid duplicates)
for (int i = 0; i < N; i++) {
if (key.equals(keys[i])) {
vals[i] = val;
return;
}
}
// add new key and value at the end of array
vals[n] = val;
keys[n] = key;
n++;
}
// remove given key and associated value
void delete(int key) {
for (int i = 0; i < n; i++) {
// if key exists, swap the key-value pair with the last element
if (key.equals(keys[i])) {
keys[i] = keys[n-1];
vals[i] = vals[n-1];
n--;
return;
}
}
// Is the given key exists?
bool contains(int key) { return get(key) != null; }
// Is the table empty?
bool isEmpty() { return n == 0; }
}
}
please rate well....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.