0% found this document useful (0 votes)
3 views

dsa_u6_FONT10

Uploaded by

Preet Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

dsa_u6_FONT10

Uploaded by

Preet Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Q. What is hashing  Definition: Hashing is the of and Q. What is file ➔ A file is a collection of related records.

For
retrieving element (data) in data structure to provide faster way example, a student file might include all of the records of
of finding the element using the hash key.In all search techniques students enrolled in a school. A police department might keep a
like linear search, binary search and search trees, the time file of criminal records, which includes details about all known
required to search an element is depend on the total number of criminals. Files are stored on secondary storage devices such as
elements in that data structure.In all these search techniques, as hard disks, CD-ROMs etc. Within a file, all records usually have
the number of elements gets increased, the time required to the same structure. That is, every record in the file contains the
search an element also linearly.Hashing is another approach in same fields. Only the data stored it the fields of different record
which time required to search an element doesn't depend on the will be different. Function of file Handling: Function Description
number of elements.Using hashing data structure, an element is Fopen() Opens new or existing file. Fprintf() Write data into file.
searched with constant time complexity.Hashing is an effective Fscanf() Reads data from file. Fputc() Writes a character into
way to reduce the number of comparisons to search an element in file.Fgetc() Reads a character from file. Fclose() Closes the
a data structure.Q. What is hash table  Definition: Hash table file.Fseek() Sets the file pointer to given position. Fputw() Writes
is an array which maps a key (data) into the data structure with an integer to file. Fgetw() Reads an integer from file. Ftell()
the help of hash function such that insertion, deletion and search Return current position. Rewind() Sets the file pointer to the
operations can be performed with constant time complexity (i.e. beginning of the file.
O(1)). All the data values are inserted into the hash table based
on the hash value. Hash key value is used to map the data with Q.What primary operation can be performed on file?explain
index in the hash table And the hash key is generated for every w.r.t file handling.  In file handling, there are several primary
data using a That means every entry in the hash table is based on operations that can be performed on a file. These operations
the hash function. key value generated using a hash function. allow you to manipulate and interact with files stored on a
Hash tables are used to perform the operations like insertion, computer system. Here are the main file operations: 1. Creating a
deletion and search very quickly in a data structure. Using hash File: This operation involves creating a new file with a specified
table concept, insertion, deletion and search operations are name and extension. The file can be empty or contain initial data.
accomplished in constant time. Generally, every hash table 2. Opening a File: Opening a file allows you to access its
makes use of a function. which we'll call to map the data into the contents for reading or writing. It establishes a connection
hash table. Q. What is hash function A hash function is any between the file and the program, enabling you to perform
function that can be used to map a data set of an arbitrary size to various operations on the file. 3. Reading from a File: This
a data set of a fixed size, which falls into the hash table. The operation involves retrieving data from a file. It allows you to
values returned by a hash function are called hash values, hash access the existing content of a file and extract information from
codes, hash sums, or simply hashes.To achieve a good hashing it. You can read the entire file or read it line by line. 4. Writing to
mechanism, it is important to have a good hash with the a File: Writing to a file involves adding or modifying data within
following basic requirements/properties. the file. It allows you to store information permanently. You can
write data to a file either in a single operation or incrementally by
Q. What is characteristic of good hashing function. 1.Easy appending new data to the existing content. 5. Appending to a
to compute: It should be easy to compute and must not become File: Appending to a file involves adding new data to the end of
an algorithm in itself. 2. Uniform distribution: It should provide a an existing file without overwriting its content. This operation is
uniform across hash table and should not result in clustering. 3. useful when you want to add more information to an existing file
Less Collisions occur when pairs of elements are mapped to the without losing the previously stored data. 6. Closing a File:
same hash value. These should be avoided. 4.Be easy and quick Closing a file terminates the connection between the file and the
to compute. 5.Use all the information provided in the key. program. It is an important step to release system resources and
ensure that any pending operations are completed and the file is
Q. What is collision. Q. List out different techniques to properly saved. 7. Renaming a File: Renaming a file allows you
resolve collision in hash table. Definition: When two to change its name or move it to a different directory while
different keys produce the same address, there is a collision. The retaining its content. This operation is useful when you want to
keys involved are called synonyms. Coming up with a hashing organize files or update their names. 8. Deleting a File: Deleting
function that avoids collision is extremely difficult. It is best to a file removes it from the file system permanently. Once deleted,
simply find ways. to deal with them. The possible solution, can the file cannot be recovered, so caution must be exercised when
be: 1.out the records 2.Use extra memory 3.Put more than one using this operation.
record at a single address. An example of Collision Hash table
11 Hash function: key mod hash size So, the new positions in the
hash table are:

KEY 23 18 29 28 39 13 16 42 17
POSITION 1 7 7 6 6 2 5 9 6
1. Chaining -Store colliding keys in a linked list at the same
hash table index. 2. Open Addressing -Store colliding keys
elsewhere in the table Collision Resolution Techniques-1.
Separate Chaining 2. Open Addressing.
Q.Explain different types of file organization? ➔File Q. Explain open addressing. Explain Linear probing with
organization is a mechanism which helps in organization the data suitable example.  Open addressing/probing is carried out for
or records in a file. File organization does not refer to the way in insertion into fixed size hash tables (hash tables with 1 or more
which files are organized in directories, but how the file contains buckets). If the index given by the hash function is occupied,
are inserted and accessed. There are different ways of file then there is need to find another bucket for the element to be
organization; the most frequently used are sequential, indexed stored. Then increment the table position by some number.
and relative, These ways differ in how easily the file records can Probing is just a way of resolving a collision when hashing
be accessed and the complexity in which records can be values into bucket There are three schemes commonly used for
organized. 1.Sequential: - In a sequential method of file probing- 1. Linear Probing 2. Quadratic Probing 3. Double
organization, records are organized or arranged in the sequence Hashing. 1) Linear Probing- The next slot for the collided key is
by which the were added in the file. It is not possible to insert a found in this method by using a technique called "Probing". It
new record anywhere in the file. The record can be inserted only generates a probe sequence of slots in the hash table and we need
at the end of the file. This is considered as a simple file to choose the proper slot for the key 'x'. Suppose that a key
organization mechanism which lets us to process batches of hashes into a position that has been already occupied. The
records in the file without the process of addition or deletion of simplest strategy is to look for the next available position to
records. 2.Indexed: - In the method of indexed file organization, place the item. Suppose we have a set of hash codes consisting of
reference numbers are included such as employee numbers, (89, 18, 49, 58, 9) and we need to place them into a table of size
which identify a specific record in relation to other records. 10. following table demonstrates this process: hash (89, 10) = 9//
These references are known as primary keys which are unique hash (18, 10) = 8 hash (49, 10) = 9 // hash (58, 10) = 8 hash (9,
for that particular record. 3.Relative/Random: - One more type 10) = 9.
of organizing files is relative to the location where the file
begins. A relative key is assigned for the purpose of determining Q. Explain various operations on Sequential file in detail .
the order of files. Access to relative files is fast, because the (A) Creating a file -Sequential files need to be opened for
physical location of a record in the file is directly calculated from output when they are created and must be closed before the
its key. program ends.A record containing sentinel values should be
written as the last record in the file before it is closed, so that
List different file opening modes? Explain various file other programs using that same file do not have to know how
opening mode with respect to text and binary files. ➔A file many records there are in that file. Algorithm to create
must be opened before you can read from it or write to it. Either Sequential File BEGIN CreateAFile //Open FriendsData for
ofstream or fstream abject may be used to open a file for writing. output /FOR i= 1 to 10// Display "Please enter the details for the
And ifstream object is used to open a file for reading purpose next person:" /Get fname, sname, emailaddr, mobile Write
only. Following is the standard syntax for open() function, which Friends Data from fname, sname, emailaddr, mobile //NEXT
is a member of fstream, ifstream, and ofstream objects. Here, the i//Let fname= "xxx"//Let sname= "xxx"//Write FriendsData from
first argument specifies the name and location of the file to be fname, sname, emailaddr, mobile Close Friends Data//END
opened and the second argument of the open() member function CreateAFile B) Printing the contents of a file -Sequential files
defines the mode in which the file should be opened. You can that already exist need to be opened for input before they can be
combine two or more of those values. For example if you want to read, and closed before the program ends. It is best to use
open a file in which mode and want to truncate it if it already priming read as in the following algorithm, in case the located
exists, following will be the syntax : In similar wat, you can open file contains no records in it. It also ensures that the sentinel
a file for reading and writing purpose as follows: Closing a file: - values do not get processed or printed. Algorithm to print
When a C++ program terminates, it automatically flushes all the contents of Sequential File- BEGIN DisplayFileContents Open
stream, release all the allocated memory and close all the opened Friends Data for input//Read fname, sname, emailaddr, mobile
files. But it is always a good practice that a programmer should from Friends Data//"This is a priming read, performed just before
close all the opened files before program termination. Following entering the loop to provide the first record (if there is one) for
is the standard syntax for close() function, which is a member of printing WHILE fname <> "xxx" AND sname <> "xxx"//Display
fstream, ifstream, and ofstream objects. fname, sname, emailaddr, mobile//Read fname, sname,
emailaddr, mobile from Friends Data 'this reads subsequent
Q. Explain the read (data and record from a file) 'C++' file records which can then be tested for the sentinel value before
function with syntax and example. //#include <iostream>// they are processed//END WHILE//Close Friends Data: END
#include <fstream>//using namespace std;//int main(){//string DisplayFileContents.//C) Appending records to an existing
srg;//ifstream filestream("Phoenix.txt");//if sequential file To enable new records to be added at the end of an
(filestream.is_open())//{//while (getline (filestream,srg))//{//cout existing sequential file, it must be opened so that records can be
<< srg <<endl;//}//filestream.close();//}//else {//cout<<"Can not appended and closed before the program ends To open it for this
open file.."<<endl;//}//return 0;//}//Output - Welcome to Phoenix purpose, use the format Open filename for append. Pseudo code
InfoTech Learn ADS and C++. to insert record - procedure insertRecord(record)//open the
sequential file in append mode// write the record to the end of the
file//close the file//end procedure.
Q. Write a c++ pseudo code for modify and delete operation Q. Explain types of indexes- 1)Primary Index- Primary index
in sequencial file. #include <iostream>//#include means if the index is created on the primary key of the table then
<fstream>//struct Record {// int id;// std::string name;// that index is called as primary index. As we know primary keys
};//void modifyRecord(int recordId, const Record& are unique to each record so it is very easy to retrieve the records
modifiedRecord) {// std::fstream file("filename.txt", std::ios::in | from the table. Also primary keys are in the sorted form to
std::ios::out);//Record currentRecord;// if (file) {// while (file upgrade the performance of the transactions. Ordered Indexing is
>> currentRecord.id >> currentRecord.name) {// if of two types ; i)Dense Index (ii) Sparse Index 2) Multilevel
(currentRecord.id == recordId) {// file.seekp(file.tellg() - Index. To speed up the search process it is necessary to keep
sizeof(Record));// file.write(reinterpret_cast<const index records in the main memory. In such case multilevel index
char*>(&modifiedRecord), sizeof(Record));//std::cout << is stored on disk with actual data files. As the size of database
"Record modified successfully." << std::endl;// return;// }//}// grows, multilevel index supports to access records quick and
}//std::cout << "Record not found." << std::endl;//}//void faster because large size index cannot be kept in memo may leads
deleteRecord(int recordId) {// std::ifstream to multiple disk access. 3) Secondary Index - If the table size
inputFile("filename.txt");//std::ofstream grows the mapping file size also grows. When address is got
outputFile("tempfile.txt");//Record currentRecord;//while from mapping then data will be searched from secondary
(inputFile >> currentRecord.id >> currentRecord.name) {//if memory. If the size of mapping grows, fetching of address
(currentRecord.id != recordId) {//outputFile << currentRecord.id becomes slower. Hence to overcome this problem secondary
<< " " << currentRecord.name << "\n";// }// }// indexing is introduced. Here another level of indexing is
inputFile.close();// outputFile.close();// introduced to reduce the mapping size. Then each range is further
std::remove("filename.txt");// std::rename("tempfile.txt", divided into smaller ranges. Then first level of mapping is stored
"filename.txt");//std::cout << "Record deleted successfully." << in the primary memory so that address fetch is faster and also
std::endl;//}//int main() {// int recordIdToModify = 123; // secondary level of mapping and the actual data are stored in the
Specify the ID of the record to be modified/// Record secondary memory-hard disk. 4) Clustering Index- Basically
modifiedRecord;// modifyRecord(recordIdToModify, records with the similar characteristics are grouped together and
modifiedRecord);// int recordIdToDelete = 456; indexes are created for these groups. In some cases indexes are
//deleteRecord(recordIdToDelete);//return 0;//} created on non-primary key columns which may not be unique
for each record in the data. So to identify and retrieve the records
Q. Explain with example (1) seekg() (ii) tellg() (iii) rewind() faster we will group two or more columns to get unique values
iv) seekp v) tellp.  I) seekg()-This function is used to move and create index on columns. This method is called as clustering
the get pointer to a desired location with respect to a reference index.
point. Syntax - file_pointer.seekg (number of bytes Reference
point); Example- fin.seekg(10,ios::beg); ii) tellg()-This is used to Q. Explain primitive operation on index sequential files. 
know where the get pointer is in a file. Syntax Insertion: Inserting a new record into the index-sequential file
file_pointer.tellg(); Example int posn fin.tellg0: iii) rewind()- involves the following steps: Insert the record at the appropriate
This sets the file position indicator to the beginning of the given position within the file based on the ordering of the records.
file stream. syntax - id rewind(FILE stream); iv) seekp()- This is Update the associated index to include the new record,
used to move the put pointer to a desired location with respect to maintaining the order of the index keys. If necessary, adjust the
a reference point. Syntax file_pointer.seekp(number of bytes pointers in the index to reflect the new record's position in the
Reference point): Example fout.seekp(10,jos::beg): v) tellp()- file. Deletion: Deleting a record from the index-sequential file
This is used to know where the put pointer is in a file. Syntax file involves the following steps: Locate the record to be deleted by
pointer.tellp(): Example- int posn-fout.tellp(); using the index to search for the appropriate key value. Remove
the record from the file. Update the associated index to exclude
Q. Write a c++ program to write a content of file into another the deleted record, adjusting the pointers in the index as
file.  #include <iostream>//#include <fstream>//int main() {/// necessary. Modification: Modifying a record within the index-
std::ifstream inputFile("input.txt");// std::ofstream sequential file involves the following steps: Locate the record to
outputFile("output.txt");// if (inputFile.is_open() && be modified by using the index to search for the appropriate key
outputFile.is_open()) {// char ch;//while (inputFile.get(ch)) {// value. Update the record with the new values. If the modification
outputFile.put(ch);// }//inputFile.close();// outputFile.close();// affects the key value of the record, the index needs to be adjusted
std::cout << "File content copied successfully." << std::endl;// } accordingly to maintain the order of the keys. Search: Searching
else {// std::cout << "Unable to open the file." << std::endl;// for a specific record in the index-sequential file involves the
}//return 0;//} following steps:Use the index to search for the appropriate key
value. Retrieve the associated record using the pointer stored in
Q. Write a c++ program for reading a character from
the index entry.
keyboard and writing it in text file.  #include
<iostream>//#include <fstream>//int main() {// std::ofstream
outputFile("output.txt");// if (outputFile.is_open()) {// char ch;//
std::cout << "Enter a character: ";// std::cin >>
ch;//outputFile.put(ch);// outputFile.close();// std::cout <<
"Character written to the file successfully." << std::endl;//} else
{// std::cout << "Unable to open the file." << std::endl;// }//
return 0;//}
Sequential file Index Sequential Direct Access
Called as QSAM Called as VSAM VSAM files
files. files.
Data entered in Data entered in key Data entered in
entry sequential sequential order. RRN number
order.
Duplicate data is Duplicate data is Not allowed
allowed. not allowed.
data is not stored in data is stored in Data in sorted order
order. order based on key. based on RRN
Delete not Delete Applicable Delete Applicable
applicable.
Access is slow faster fatstest
Key Not avaialble available available
Data is stored in Data is stored in Data is stored in
disc disc disc only

You might also like