Test 2A - Algorithms and Data Structures | CP SC 212, Exams of Algorithms and Programming

Material Type: Exam; Class: ALGS/DATA STRUCTURES; Subject: COMPUTER SCIENCE; University: Clemson University; Term: Fall 2005;

Typology: Exams

Pre 2010

Uploaded on 07/28/2009

koofers-user-1cd
koofers-user-1cd 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPSC 212-301 Name: _____________________________
Test #2A October 26, 2005
Honor Pledge:
I pledge that I have neither given nor received unauthorized assistance on this test.
Signed: _______________________ Date: _____________
20 points. 50 minutes. Open Java API. Open cucs API. You may not visit any other web page. You are to
develop a class called MyHashTable with one constructor:
public MyHashTable (int n) {
// ... your code ...
} // MyHashTable
which receives an int variable x which specifies the length (number of elements) of the hash table, and
three public methods:
public void insert (int x) { public void delete (int x) {
// … inserts x into hash table // … deletes x from hash table
} // insert // NOTE: for 5-pt bonus credit
} // delete
public String toString () {
// ... your code ...
} // toString
The method insert(int x) inserts the value x into the hash table. Use f(x)=x%n where n is the
length of the hash table, as the primary hash function. Use quadratic probing to handle collisions. If you
cannot insert the value in the hash table, print an error message. Note that a value of -1 indicates that an
array element is empty.
For 5-point bonus credit, implement the method delete(int x) which deletes the value x from the hash
table. Recall the need for a bit array to remember locations of values that have been deleted. Do not
attempt to write delete() until you have completely and correctly written insert().
The following test driver:
public static void main(String args[]) {
MyHashTable h = new MyHashTable(19);
System.out.println("Initial hash table: " + h);
int v = 15; h.insert(v); System.out.print("Inserted " + v);
v += 19; h.insert(v); System.out.print(", " + v);
v += 19; h.insert(v); System.out.print(", " + v);
v += 19; h.insert(v); System.out.print(", " + v);
v += 19; h.insert(v); System.out.print(", " + v);
System.out.println("\nAfter inserts : " + h);
} // end main
produces the following output:
Initial hash table: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
Inserted 15, 34, 53, 72, 91
After inserts : [53, -1, -1, -1, -1, 72, -1, -1, -1, -1, -1, -1, 91, -1, -1, 15, 34, -1, -1]
This test driver is provided only as an example. You should test your code with a more rigorous test driver.
pf2

Partial preview of the text

Download Test 2A - Algorithms and Data Structures | CP SC 212 and more Exams Algorithms and Programming in PDF only on Docsity!

CPSC 212-301 Name: _____________________________

Test #2A October 26, 2005

Honor Pledge:

I pledge that I have neither given nor received unauthorized assistance on this test.

Signed: _______________________ Date: _____________

20 points. 50 minutes. Open Java API. Open cucs API. You may not visit any other web page. You are to

develop a class called MyHashTable with one constructor:

public MyHashTable (int n) { // ... your code ... } // MyHashTable

which receives an int variable x which specifies the length (number of elements) of the hash table, and

three public methods:

public void insert (int x) { public void delete (int x) { // … inserts x into hash table // … deletes x from hash table } // insert // NOTE: for 5-pt bonus credit } // delete public String toString () { // ... your code ... } // toString

The method insert(int x) inserts the value x into the hash table. Use f(x)=x%n where n is the

length of the hash table, as the primary hash function. Use quadratic probing to handle collisions. If you

cannot insert the value in the hash table, print an error message. Note that a value of -1 indicates that an

array element is empty.

For 5-point bonus credit, implement the method delete(int x) which deletes the value x from the hash

table. Recall the need for a bit array to remember locations of values that have been deleted. Do not

attempt to write delete() until you have completely and correctly written insert().

The following test driver:

public static void main(String args[]) { MyHashTable h = new MyHashTable(19); System.out.println("Initial hash table: " + h); int v = 15; h.insert(v); System.out.print("Inserted " + v); v += 19; h.insert(v); System.out.print(", " + v); v += 19; h.insert(v); System.out.print(", " + v); v += 19; h.insert(v); System.out.print(", " + v); v += 19; h.insert(v); System.out.print(", " + v); System.out.println("\nAfter inserts : " + h); } // end main

produces the following output:

Initial hash table: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1] Inserted 15, 34, 53, 72, 91 After inserts : [53, -1, -1, -1, -1, 72, -1, -1, -1, -1, -1, -1, 91, -1, -1, 15, 34, -1, -1]

This test driver is provided only as an example. You should test your code with a more rigorous test driver.

Submission:

Submit your work using: handin.212.301 2 MyHashTable.java

Also submit any helper methods and/or classes you developed during this test and required by your class.

Important:

If you cannot complete one or more of the methods (for example the delete() method), be sure to include

at least a skeleton of the method which prints the String:

“Method ___________ not completed”

so that my test driver will be able to compile your program.

Evaluation:

Your program will be evaluated on: (a) correctness of algorithm, and (b) efficiency of algorithm.