Hashed Array Tree

Hashed Array Tree is a type of data structure that is used for storing and managing data in the form of an array. It is a combination of an array and a hash table. The hashed array tree provides the benefits of both arrays and hash tables, such as fast access to elements and efficient memory usage.

It has a dynamic size, so it can grow and shrink as needed. The hashed array tree is implemented using an array that stores the elements and a hash table that maps the keys to the indices of the elements in the array.

How Hashed Array Tree Works?

Let’s understand how a hashed array tree works with the help of an example:

Suppose we have a set of elements {A, B, C, D, E, F, G, H, I, J} and we want to store these elements in a hashed array tree.

Here’s how a hashed array tree works:

  • Initially, we create an array of size ‘n’ and a hash table that maps the keys to the indices of the elements in the array.
  • For each element in the set, we calculate the hash value of the key and store the element at the corresponding index in the array.
  • We also store the key and the index in the hash table.
  • When we want to access an element by its key, we calculate the hash value of the key and retrieve the index from the hash table.
  • We then access the element at the retrieved index in the array.

Operations on Hashed Array Tree

A hashed array tree supports the following operations:

  • Insert(key, value): Insert an element with the given key and value into the hashed array tree.
  • Get(key): Retrieve the element with the given key from the hashed array tree.
  • Delete(key): Delete the element with the given key from the hashed array tree.

Implementation of Hashed Array Tree

Now, let’s understand how we can implement a hashed array tree. We need to keep few things in mind while implementing hashed array trees we need to handle collisions, resizing the array, and rehashing the keys.

Algorithm for Insert Operation

Following is the algorithm for Insert Operation −

1.Calculate the hash value of the key.
2.Check if the hash value is within the array bounds.
3.If the index is empty, insert the element at the index.
4.If the index is not empty, handle the collision.

Code for Insert Operation

Following is an example of how we can implement the insert operation in a hashed array tree:

CC++JavaPython

// C Program to perform Insert Operation on Hashed Array Tree#include <stdio.h>#include <stdlib.h>#define SIZE 10int hashTable[SIZE]={0};int arr[SIZE]={0};inthashFunction(int key){return key % SIZE;}voidinsert(int key,int value){int index =hashFunction(key);while(arr[index]!=0){
      index =(index +1)% SIZE;}
   arr[index]= value;
   hashTable[index]= key;}voiddisplay(){for(int i =0; i < SIZE; i++){if(arr[i]!=0){printf("Key: %d, Value: %d\n", hashTable[i], arr[i]);}}}intmain(){insert(3,10);insert(13,20);insert(23,30);insert(33,40);insert(43,50);insert(53,60);insert(63,70);display();return0;}

Output

Following is the output of the above C program:

Key: 3, Value: 10
Key: 13, Value: 20
Key: 23, Value: 30
Key: 33, Value: 40
Key: 43, Value: 50
Key: 53, Value: 60
Key: 63, Value: 70

Algorithm for Get Operation on Hashed Array Tree

Following is the algorithm for the Get Operation on Hashed Array Tree −

1.Calculate the hash value of the key.
2.Retrieve the index from the hash table.
3.Access the element at the retrieved index in the array.

Code for Get Operation

Following is an example of how we can implement the get operation in a hashed array tree:

CC++JavaPython

// C Program to perform Get Operation on Hashed Array Tree#include <stdio.h>#include <stdlib.h>#define SIZE 10int hashTable[SIZE]={0};int arr[SIZE]={0};inthashFunction(int key){return key % SIZE;}voidinsert(int key,int value){int index =hashFunction(key);while(arr[index]!=0){
      index =(index +1)% SIZE;}
   arr[index]= value;
   hashTable[index]= key;}intget(int key){int index =hashFunction(key);while(hashTable[index]!= key){
      index =(index +1)% SIZE;}return arr[index];}intmain(){insert(3,10);insert(13,20);insert(23,30);insert(33,40);insert(43,50);insert(53,60);insert(63,70);insert(73,80);insert(83,90);insert(93,100);printf("Value: %d\n",get(33));return0;}

Output

Following is the output of the above C program:

Value: 40

Algorithm for Delete Operation on Hashed Array Tree

Following is the algorithm for the Delete Operation on Hashed Array Tree −

1.Calculate the hash value of the key.
2.Retrieve the index from the hash table.
3.Delete the element at the retrieved index in the array.

Code for Delete Operation

Following is an example of how we can implement the delete operation in a hashed array tree:

CC++JavaPython

// C Program to perform Delete Operation on Hashed Array Tree#include <stdio.h>#include <stdlib.h>#define SIZE 10int hashTable[SIZE]={0};int arr[SIZE]={0};inthashFunction(int key){return key % SIZE;}voidinsert(int key,int value){int index =hashFunction(key);while(arr[index]!=0){
      index =(index +1)% SIZE;}
   arr[index]= value;
   hashTable[index]= key;}voiddeleteEl(int key){int index =hashFunction(key);while(hashTable[index]!= key){
      index =(index +1)% SIZE;}
   arr[index]=0;
   hashTable[index]=0;}voiddisplay(){for(int i =0; i < SIZE; i++){if(arr[i]!=0){printf("Key: %d, Value: %d\n", hashTable[i], arr[i]);}}}intmain(){insert(3,10);insert(13,20);insert(23,30);insert(33,40);insert(43,50);insert(53,60);printf("Before Deletion:\n");display();deleteEl(33);printf("After Deletion:\n");display();return0;}

Output

Following is the output of the above C program:

Before Deletion
Key: 3, Value: 10
Key: 13, Value: 20
Key: 23, Value: 30
Key: 33, Value: 40
Key: 43, Value: 50
Key: 53, Value: 60
After Deletion
Key: 3, Value: 10
Key: 13, Value: 20
Key: 23, Value: 30
Key: 43, Value: 50
Key: 53, Value: 60

Time Complexity of Hashed Array Tree

The time complexity of operations on a hashed array tree is as follows:

  • Insert Operation: The time complexity of the insert operation is O(1) on average, as the hash function provides constant-time access to the array index.
  • Get Operation: The time complexity of the get operation is O(1) on average, as the hash function provides constant-time access to the array index.
  • Delete Operation: The time complexity of the delete operation is O(1) on average, as the hash function provides constant-time access to the array index.

Applications of Hashed Array Tree

Hashed array tree is used in the following applications:

  • Database Management: Hashed array tree is used to store and manage data in databases.
  • Cache Management: It is used to store and manage cache data efficiently.
  • File Systems: Hashed array tree is used to store and manage file system data.
  • Indexing: It is used to create indexes for fast data retrieval.
  • Memory Management: Hashed array tree is used to manage memory efficiently.

Conclusion

In this chapter, we learned about hashed array tree in data structures. We discussed how hashed array tree works, its operations, implementation, and time complexity. We also saw examples of insertget, and delete operations on a hashed array tree in C, C++, Java, and Python. Hashed array tree is a powerful data structure that provides fast access to elements and efficient memory usage.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *