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:

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

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 
 // C++ Program to perform Insert Operation on Hashed Array Tree #include <iostream> using namespace std; #define SIZE 10 int hashTable[SIZE] = {0}; int arr[SIZE] = {0}; int hashFunction(int key){ return key % SIZE; } void insert(int key, int value){ int index = hashFunction(key); while(arr[index] != 0){ index = (index + 1) % SIZE; } arr[index] = value; hashTable[index] = key; } void display(){ for(int i = 0; i < SIZE; i++){ if(arr[i] != 0){ cout << "Key: " << hashTable[i] << ", Value: " << arr[i] <<endl; } } } int main(){ insert(3, 10); insert(13, 20); insert(23, 30); insert(33, 40); insert(43, 50); insert(53, 60); insert(63, 70); display(); return 0; } 

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 
 // Java Program to perform Insert Operation on Hashed Array Tree public class HashedArrayTree { static final int SIZE = 10; static int[] hashTable = new int[SIZE]; static int[] arr = new int[SIZE]; static int hashFunction(int key){ return key % SIZE; } static void insert(int key, int value){ int index = hashFunction(key); while(arr[index] != 0){ index = (index + 1) % SIZE; } arr[index] = value; hashTable[index] = key; } static void display(){ for(int i = 0; i < SIZE; i++){ if(arr[i] != 0){ System.out.println("Key: " + hashTable[i] + ", Value: " + arr[i]); } } } public static void main(String[] args){ insert(3, 10); insert(13, 20); insert(23, 30); insert(33, 40); insert(43, 50); insert(53, 60); insert(63, 70); display(); } } 

Output

Following is the output of the above Java 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 
 # Python Program to perform Insert Operation on Hashed Array Tree SIZE = 10 hashTable = [0] * SIZE arr = [0] * SIZE def hashFunction(key): return key % SIZE def insert(key, value): index = hashFunction(key) while arr[index] != 0: index = (index + 1) % SIZE arr[index] = value hashTable[index] = key def display(): for i in range(SIZE): if arr[i] != 0: print("Key:", hashTable[i], ", Value:", arr[i]) insert(3, 10) insert(13, 20) insert(23, 30) insert(33, 40) insert(43, 50) insert(53, 60) insert(63, 70) display() 

Output

Following is the output of the above Python 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:

 // C Program to perform Get Operation on Hashed Array Tree #include <stdio.h> #include <stdlib.h> #define SIZE 10 int hashTable[SIZE] = {0}; int arr[SIZE] = {0}; int hashFunction(int key){ return key % SIZE; } void insert(int key, int value){ int index = hashFunction(key); while(arr[index] != 0){ index = (index + 1) % SIZE; } arr[index] = value; hashTable[index] = key; } int get(int key){ int index = hashFunction(key); while(hashTable[index] != key){ index = (index + 1) % SIZE; } return arr[index]; } int main(){ 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)); return 0; } 

Output

Following is the output of the above C program:

 Value: 40 
 // C++ Program to perform Get Operation on Hashed Array Tree #include <iostream> using namespace std; #define SIZE 10 int hashTable[SIZE] = {0}; int arr[SIZE] = {0}; int hashFunction(int key){ return key % SIZE; } void insert(int key, int value){ int index = hashFunction(key); while(arr[index] != 0){ index = (index + 1) % SIZE; } arr[index] = value; hashTable[index] = key; } int get(int key){ int index = hashFunction(key); while(hashTable[index] != key){ index = (index + 1) % SIZE; } return arr[index]; } int main(){ 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); cout << "Value: " << get(33) <<endl; return 0; } 

Output

Following is the output of the above C++ program:

 Value: 40 
 // Java Program to perform Get Operation on Hashed Array Tree public class HashedArrayTree { static final int SIZE = 10; static int[] hashTable = new int[SIZE]; static int[] arr = new int[SIZE]; static int hashFunction(int key){ return key % SIZE; } static void insert(int key, int value){ int index = hashFunction(key); while(arr[index] != 0){ index = (index + 1) % SIZE; } arr[index] = value; hashTable[index] = key; } static int get(int key){ int index = hashFunction(key); while(hashTable[index] != key){ index = (index + 1) % SIZE; } return arr[index]; } public static void main(String[] args){ 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); System.out.println("Value: " + get(33)); } } 

Output

Following is the output of the above Java program:

 Value: 40 
 # Python Program to perform Get Operation on Hashed Array Tree SIZE = 10 hashTable = [0] * SIZE arr = [0] * SIZE def hashFunction(key): return key % SIZE def insert(key, value): index = hashFunction(key) while arr[index] != 0: index = (index + 1) % SIZE arr[index] = value hashTable[index] = key def get(key): index = hashFunction(key) while hashTable[index] != key: index = (index + 1) % SIZE return arr[index] 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) print("Value:", get(33)) 

Output

Following is the output of the above Python 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:

 // C Program to perform Delete Operation on Hashed Array Tree #include <stdio.h> #include <stdlib.h> #define SIZE 10 int hashTable[SIZE] = {0}; int arr[SIZE] = {0}; int hashFunction(int key){ return key % SIZE; } void insert(int key, int value){ int index = hashFunction(key); while(arr[index] != 0){ index = (index + 1) % SIZE; } arr[index] = value; hashTable[index] = key; } void deleteEl(int key){ int index = hashFunction(key); while(hashTable[index] != key){ index = (index + 1) % SIZE; } arr[index] = 0; hashTable[index] = 0; } void display(){ for(int i = 0; i < SIZE; i++){ if(arr[i] != 0){ printf("Key: %d, Value: %d\n", hashTable[i], arr[i]); } } } int main(){ 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(); return 0; } 

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 
 // C++ Program to perform Delete Operation on Hashed Array Tree #include <iostream> using namespace std; #define SIZE 10 int hashTable[SIZE] = {0}; int arr[SIZE] = {0}; int hashFunction(int key){ return key % SIZE; } void insert(int key, int value){ int index = hashFunction(key); while(arr[index] != 0){ index = (index + 1) % SIZE; } arr[index] = value; hashTable[index] = key; } void deleteEl(int key){ int index = hashFunction(key); while(hashTable[index] != key){ index = (index + 1) % SIZE; } arr[index] = 0; hashTable[index] = 0; } void display(){ for(int i = 0; i < SIZE; i++){ if(arr[i] != 0){ cout << "Key: " << hashTable[i] << ", Value: " << arr[i] <<endl; } } } int main(){ insert(3, 10); insert(13, 20); insert(23, 30); insert(33, 40); insert(43, 50); insert(53, 60); cout << "Before Deletion:" <<endl; display(); deleteEl(33); cout << "After Deletion:" <<endl; display(); return 0; } 

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 
 // Java Program to perform Delete Operation on Hashed Array Tree public class HashedArrayTree { static final int SIZE = 10; static int[] hashTable = new int[SIZE]; static int[] arr = new int[SIZE]; static int hashFunction(int key){ return key % SIZE; } static void insert(int key, int value){ int index = hashFunction(key); while(arr[index] != 0){ index = (index + 1) % SIZE; } arr[index] = value; hashTable[index] = key; } static void deleteEl(int key){ int index = hashFunction(key); while(hashTable[index] != key){ index = (index + 1) % SIZE; } arr[index] = 0; hashTable[index] = 0; } static void display(){ for(int i = 0; i < SIZE; i++){ if(arr[i] != 0){ System.out.println("Key: " + hashTable[i] + ", Value: " + arr[i]); } } } public static void main(String[] args){ insert(3, 10); insert(13, 20); insert(23, 30); insert(33, 40); insert(43, 50); insert(53, 60); System.out.println("Before Deletion:"); display(); deleteEl(33); System.out.println("After Deletion:"); display(); } } 

Output

Following is the output of the above Java 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 
 # Python Program to perform Delete Operation on Hashed Array Tree SIZE = 10 hashTable = [0] * SIZE arr = [0] * SIZE def hashFunction(key): return key % SIZE def insert(key, value): index = hashFunction(key) while arr[index] != 0: index = (index + 1) % SIZE arr[index] = value hashTable[index] = key def deleteEl(key): index = hashFunction(key) while hashTable[index] != key: index = (index + 1) % SIZE arr[index] = 0 hashTable[index] = 0 def display(): for i in range(SIZE): if arr[i] != 0: print("Key:", hashTable[i], ", Value:", arr[i]) insert(3, 10) insert(13, 20) insert(23, 30) insert(33, 40) insert(43, 50) insert(53, 60) print("Before Deletion:") display() deleteEl(33) print("After Deletion:") display() 

Output

Following is the output of the above Python 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 insert, get, 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.

Advertisements
close