Data structures are primarily used to store data and manage a large amount of data, and efficient data structures are helpful for developing better algorithms. The main operations performed on a data structure are Insert, Search, Update, and Delete. With increasing amounts of data, all these operations tend to take a large amount of time.
There may be scenarios where we want to have all these operations to be performed in O(1) time i.e. constant time. To implement this, we have a data structure called a Direct Addressing Table.
Direct Addressing Table is a random data structure that maps records with their corresponding unique keys using Arrays. In Direct Address Tables, the keys of records are used directly as indexes. This helps in fast and efficient Insert, Search, and Delete operations.
The following operations can be done in O(1) time:
- Insertion: Inserting an element at an index in an array takes O(1) time. Here also, for the insertion of an element, the key is used as an index.
- Searching: Using the key values as the indexes give a faster lookup for searching any record.
- Deletion: To delete an element from the Direct Address Table, set the index/location to point to null.
Let’s consider we are given the task of creating and maintaining a catalog for data of college students of a particular division with a maximum capability of 70 students per division. Each student has a unique roll number assigned to him/her. The task is to store the complete information of every student efficiently. Now, we can use the roll number of a student as a key to put his/her data at that index. Firstly, we create an array arr of size 70 which will act as our direct address table.
Let’s suppose we insert the student data with roll number 13, we use 13 as an index and insert that student’s data at arr. Insert another student’s data with roll number 18, we use 18 as an index and insert that student’s data at arr. The image below is a representation of the insertion operation being performed.
Now, consider we want to retrieve the information about the student with roll number 18, we can directly use 18 as an index and get that student’s data at arr. This address table lookup method gives access to data in O(1) time. Suppose we want to delete the information about the student with roll number 13 (maybe because he left the college), use 13 as an index, and remove that student’s data at arr by setting it to null.
But there are some limitations of using this data structure:
- The keys must be integers / can be converted to integers.
- All the keys must be unique.
- The range of integer keys should be relatively small (else we will need a very large amount of memory)
- The integer keys should be relatively close enough (else there will be more empty slots and memory will be wasted)
Let’s look at the implementation of the above example:
Student with roll number 18 - Name: Joe, Gender: Male No student found with roll number 13
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.