Вы находитесь на странице: 1из 6

CS 2022- Data Structures and Algorithms:

Practical 1- Data Structures


Objective
After successfully completing this lab you should be able to, Identify required components to build hash tables. Design and implement hash tables considering the given problem and hash function. Use basic programming language constructs to build necessary data structures.

Prerequisite
Students are expected to be capable of developing programs in C or Java.

Remarks
Check your program with the given input and out conditions. Download the dictionary text file and input text file from moodle. Save them in your project folder. Upload the code in the given link in moodle.

Problem A bookshop owner comes to your XYZ software company with a text file of a dictionary that they publish. He asks you to develop an online dictionary. The dictionary should function as mentioned below. When a user enters a word, dictionary should display the meaning if it is available in the dictionary .Else it should display a message saying the word cannot be found. Dictionary search should be very efficient so that it can perform a search nearly in O(1) time. Dictionary should be very efficient in space complexity.

Project plan After a meeting with your development team the following key design decisions were taken. A hash table should be used as the data structure to store the dictionary Linked Lists should be used to handle collisions.

Task 1 Following algorithm was proposed to assign a numeric value to a given word. Take each character and get the ASCII value of that character. Then multiply each ASCII value with the position of that character in the word Then add all values to get the numeric value of the word

Eg: If word= APPLE ASCII values A=65 P=80 L=76 E=69 Numeric value= (65x1)+(80x2)+(80x3)+(76x4)+(69x5) =1114 Write the algorithm to calculate the numeric value of a given word following the above method.

Task 2 The following hash function was proposed to do the hashing Take the numeric value, multiply it by 3999 Take the result and get , result mod 379 ( result%379) Again take the result and get, result mod 57

Eg: For word APPLE hash code is calculated as below. Numeric value=1114 1114*3999= 4454886 1379655 mod 379=120 95 mod 57= 6 h(345)=6

Write algorithm to calculate hash function of a given word using the algorithm developed in task 1. Task 3 Construct a hash table for the given problem using an array. Your system should read the given dictionary text file and insert records in the corresponding positions in the hash table. Create the dictionary Read the dictionary words from the given input file dictionary.txt Calculate numeric value of the word using the implementation in task 1 Calculate hash code from the implementation in task 2 Insert the word in the corresponding position of the hash table.

Retrieve the meaning A user will input a list of words to get the meanings in a file input.txt. Your program should Read this file and get the words one by one Search each word in the hash table and write the hash table position of the word in an out file, each output separated by a space.

Hint: Identify the number of entries in the hash table from the given hash code. Assume that there will be no collisions between the generated hash codes

Read files Java


try { BufferedReader br = new BufferedReader(new FileReader("./dictionary.txt")); String word; while ((word = br.readLine()) != null) {

//write your code to insert the word to the hash table


} br.close(); } catch (IOException e) { e.printStackTrace(); }

Read input and write output to a file in Java


try { BufferedReader br = new BufferedReader(newFileReader("./input.txt")); FileWriter fr = new FileWriter(newFileReader(new File("./output.txt")));

String inputWord; int hashTablePosition; while ((inputWord = br.readLine()) != null) {

/*hashTablePosition=Write your code to search the given word in the hash table **/
fr.append(hashTablePosition); fr.append( ); } br.close(); fr.close(); } catch (IOException e) { e.printStackTrace();

Вам также может понравиться