diff --git a/cpp/data_structures/Hash_implementation/Makefile b/cpp/data_structures/Hash_implementation/Makefile new file mode 100644 index 000000000..5f12d6bda --- /dev/null +++ b/cpp/data_structures/Hash_implementation/Makefile @@ -0,0 +1,9 @@ +CC=g++ +Output:main.o hash.o + $(CC) main.o hash.o -o nameht +main.o:main.cpp + $(CC) -c main.cpp +hash.o:hash.cpp + $(CC) -c hash.cpp +clean: + rm *.o nameht \ No newline at end of file diff --git a/cpp/data_structures/Hash_implementation/README.md b/cpp/data_structures/Hash_implementation/README.md new file mode 100644 index 000000000..419167196 --- /dev/null +++ b/cpp/data_structures/Hash_implementation/README.md @@ -0,0 +1,32 @@ + +Implement Hash ADT with closed Hashing. + +Consider the dataset given for previous laboratory session on BSTs. Further consider the field, +Name N as a key. Assume that all keys at least 5 characters long and may discard keys/records +which are shorter than 5 characters. Implement Hash table ADT with the necessary member +functions, +• to initialize the hash table (HT) with a specific size and specific collision resolving technique +• to insert a record with the given key +• to delete a record with the given key +• to find a record with the given key +• to rehash the hash table when load factor λ > 0.75 +• to print the statistics of number of probes for unsuccessful U and successful S find operations +carried out so far and current load factor λ +format of execution: +./nameht option [SIZE] [KEY] [CR] +option: +1. to initialize hash table for specific SIZE and CR +2. to find a given KEY +3. to insert a given KEY +4. to delete a given KEY +5. to compute load factor +6. to rehash hash table to a given size SIZE +7. to display statistics +Arguments mentioned in [ ] brackets are optional. +Example: +./nameht 1 500 LP - to create a hash table with size 500 slots and linear probing resolving +technique +./nameht 2 “vijay kumar” - find the name “vijay kumar” in the hash table. If exists, print its +frequency +./nameht 4 arun - delete the record with Key “arun” + diff --git a/cpp/data_structures/Hash_implementation/dataset_random_2000k.txt.tar.gz b/cpp/data_structures/Hash_implementation/dataset_random_2000k.txt.tar.gz new file mode 100644 index 000000000..f6b8deadb Binary files /dev/null and b/cpp/data_structures/Hash_implementation/dataset_random_2000k.txt.tar.gz differ diff --git a/cpp/data_structures/Hash_implementation/hash.cpp b/cpp/data_structures/Hash_implementation/hash.cpp new file mode 100644 index 000000000..2f5c17b3e --- /dev/null +++ b/cpp/data_structures/Hash_implementation/hash.cpp @@ -0,0 +1,483 @@ +#include +#include +#include +#include "hash.h" +using namespace std; + +void hashtable :: s_and_u() +{ + cout<name<name =""; +// Arr2[i]->tot_count=0; +// Arr2[i]->d_flag=0; +// } +// for(int i=0;iname)%hashsize1; +// if(Arr2[pos]->name=="") +// { +// Arr2[pos]->name = Arr[i]->name; +// Arr2[pos]->tot_count = Arr[i]->tot_count; +// Arr2[pos]->d_flag = Arr[i]->d_flag; +// } +// else +// { +// collision_no1++; +// int pos = hash_func2(Arr[i]->name,collision_no1)%hashsize1; +// while(Arr2[pos]->name!="") +// { +// collision_no1++; +// pos = hash_func2(Arr[i]->name,collision_no1)%hashsize1; +// } +// Arr2[pos]->name = Arr[i]->name; +// Arr2[pos]->tot_count = Arr[i]->tot_count; +// Arr2[pos]->d_flag = Arr[i]->d_flag; +// } +// } +// else if(method == 2) +// { +// int pos = hash_func1(Arr[i]->name)%hashsize1; +// if(Arr2[pos]->name=="") +// { +// Arr2[pos]->name = Arr[i]->name; +// Arr2[pos]->tot_count = Arr[i]->tot_count; +// Arr2[pos]->d_flag = Arr[i]->d_flag; +// } +// else +// { +// collision_no1++; +// int pos = hash_func3(Arr[i]->name,collision_no1)%hashsize1; +// while(Arr2[pos]->name!="") +// { +// collision_no1++; +// pos = hash_func3(Arr[i]->name,collision_no1)%hashsize1; +// } +// Arr2[pos]->name = Arr[i]->name; +// Arr2[pos]->tot_count = Arr[i]->tot_count; +// Arr2[pos]->d_flag = Arr[i]->d_flag; +// } +// } +// } +// delete[] Arr; +// hashsize = hashsize1; +// return Arr2; +// } + +void hashtable :: rehash(int size,int method) //rehash with given size +{ + int hashsize1 = size; + hashtable h2(hashsize1); + for(int i=0;iname!="") + { + if(method==1) + { + h2.insert(Arr[i]->name , Arr[i]->tot_count , 1); + } + else + { + h2.insert(Arr[i]->name , Arr[i]->tot_count , 2); + } + + } + } + for(int i=0;iname!="") + { + if(method==1) + { + h2.insert(Arr[i]->name , Arr[i]->tot_count , 1); + } + else + { + h2.insert(Arr[i]->name , Arr[i]->tot_count , 2); + } + + } + } + for(int i=0;i0.75) return 0; + else return 1; +} + +int hashtable :: check_lambda1() +{ + float i = (entries*(1.0))/(hashsize*(1.0)); + if(i>0.75) return 0; + else return 1; +} + +void hashtable :: insert_collision(string name,int tot_count,int method,int collision_no) //insert with collision +{ + if(method==1) + { + int pos = hash_func2(name,collision_no)%hashsize; + if(Arr[pos]->name=="") + { + Arr[pos]->name = name; + Arr[pos]->tot_count = tot_count; + Arr[pos]->d_flag=0; + entries++; + //cout<<"inserted"<name==name) + { + (Arr[pos]->tot_count)+=tot_count; + //cout<<"inserted"< hashsize) + { + Arr = rehash(method); + //cout<<"re-hash2 "<name!="" || Arr[pos]->name == name) + // { + // collision_no++; + // if(collision_no > hashsize) + // { + // Arr = rehash(method); + // cout<<"re-hash2 "<name = name; + // Arr[pos]->tot_count += tot_count; + // entries++; + // if(collision_no > hashsize) + // { + // Arr = rehash(method); + // cout<<"re-hash2 "<name=="") + { + Arr[pos]->name = name; + Arr[pos]->tot_count = tot_count; + Arr[pos]->d_flag=0; + entries++; + //cout<<"inserted"<name<name; + } + } + else if(Arr[pos]->name==name) + { + (Arr[pos]->tot_count)+=tot_count; + //cout<<"inserted"< hashsize) + { + Arr = rehash(method); + //cout<<"re-hash2"<name=="") + { + Arr[pos]->name = name; + Arr[pos]->tot_count = tot_count; + entries++; + //cout<<"inserted"<name<name; + } + } + else if(method == 2) + { + if(check_lambda1()==0) + { + Arr = rehash(method); + //cout<<"re-hash1"<name<name; + } + } + } + else if(Arr[pos]->name==name) + { + (Arr[pos]->tot_count)+=tot_count; + //cout<<"inserted"< hashsize) + { + Arr = rehash(method); + } + else if(method == 1 && collision_no > hashsize) + { + Arr = rehash(method); + //cout<<"re-hash2"<name==name && Arr[pos]->d_flag==0) + { + cout<tot_count<name=="") + { + cout<<"not found\n"; + } + else + { + collision_no++; + unsuccesful++; + find_collision(name,collision_no,method); + } + } +} + +void hashtable :: find_collision(string name,int collision_no,int method) +{ + if(method == 1) + { + int pos = hash_func2(name,collision_no)%hashsize; + if(Arr[pos]->name == "" && Arr[pos]->d_flag==0) + { + cout<<"not found"<name == name) + { + cout<tot_count<name == "" && Arr[pos]->d_flag==0) + { + cout<<"not found"<name == name) + { + cout<tot_count<name == "" && Arr[pos]->d_flag==0) + { + cout<<"not found"<name == name) + { + Arr[pos]->name=""; + Arr[pos]->d_flag=1; + Arr[pos]->tot_count = 0; + } + else + { + collision_no++; + delete_collision(name,collision_no,method); + } + } + if(method == 2) + { + int pos = hash_func3(name,collision_no)%hashsize; + if(Arr[pos]->name == "" && Arr[pos]->d_flag==0) + { + cout<<"not found"<name == name) + { + Arr[pos]->name=""; + Arr[pos]->d_flag=1; + Arr[pos]->tot_count = 0; + } + else + { + collision_no++; + delete_collision(name,collision_no,method); + } + } +} + +void hashtable :: delete_(string name,int method) +{ + if(method==1 || method ==2) + { + int pos = hash_func1(name)%hashsize; + int collision_no=0; + if(Arr[pos]->name==name && Arr[pos]->d_flag==0) + { + Arr[pos]->name=""; + Arr[pos]->d_flag=1; + Arr[pos]->tot_count = 0; + } + else if(Arr[pos]->name=="") + { + cout<<"not found1\n"; + } + else + { + collision_no++; + delete_collision(name,collision_no,method); + } + } +} + diff --git a/cpp/data_structures/Hash_implementation/hash.h b/cpp/data_structures/Hash_implementation/hash.h new file mode 100644 index 000000000..2bceb65f3 --- /dev/null +++ b/cpp/data_structures/Hash_implementation/hash.h @@ -0,0 +1,56 @@ +#include +#include +#include +using namespace std; + +struct node +{ + string name; + int tot_count; + int d_flag; +}; +typedef node* nodePtr; +class hashtable +{ + node **Arr; + int entries; + int hashsize; + int succesful; + int unsuccesful; + public: + hashtable(int size) + { + entries = 0; + hashsize = size; + succesful = unsuccesful =0; + Arr = new node*[hashsize]; + for(int i=0;iname = ""; + Arr[i]->tot_count = 0; + Arr[i]->d_flag = 0; + } + } + + int check_lambda(); + void insert(string name,int tot_count,int method); + void find(string n,int method); + void find_collision(string n,int collision_no,int method); + void insert_collision(string name,int tot_count,int method,int collision_no); + node** rehash(int method); + void rehash(int size,int method); + void size(); + void print_table(); + int check_lambda1(); + void print_lf(); + void delete_(string name,int method); + void delete_collision(string name,int method,int collision_no); + void clear_nodes(); + void s_and_u(); +}; + +int hash_func1(string name); +int hash_func2(string name,int collision_no); +int hash_func3(string name,int collision_no); + diff --git a/cpp/data_structures/Hash_implementation/main.cpp b/cpp/data_structures/Hash_implementation/main.cpp new file mode 100644 index 000000000..d23bea342 --- /dev/null +++ b/cpp/data_structures/Hash_implementation/main.cpp @@ -0,0 +1,86 @@ +#include +#include +#include "hash.h" +using namespace std; +int main(int argc,char* argv[]) +{ + if(argc==3) + { + int size = atoi(argv[1]); //enter prime size for QP for more effective hashing. + string m = argv[2]; + int method; + if(m == "LP") + { + method = 1; + } + else if(m == "QP") + { + method = 2; + } + ifstream f("dataset_random_10k.txt"); //file name. + int i; + int tot_count; + string n; + int count = 0; + hashtable h(100); + while(f>>i) + { + count++; + f>>tot_count; + f>>i; + f>>i; + f>>n; + h.insert(n,tot_count,method); + //cout<>select; + while(select!=-1) //according to option specified ot does the operations. + { + if(select == 2) + { + string find_name; + cin>>find_name; + h.find(find_name,method); + } + + else if(select == 3) + { + string ins_name; + int tot; + + cin>>ins_name; + cin>>tot; + h.insert(ins_name,tot,method); + } + else if(select == 4) + { + string del_name; + cin>>del_name; + h.delete_(del_name,method); + } + else if(select == 5) + { + h.print_lf(); + } + else if(select == 6) + { + int size; + cin>>size; + h.rehash(size,method); + } + else if(select == 7) + { + h.s_and_u(); + } + cin>>select; + } + } + + else + { + cout<<"Enter ./nameht [size_of_ht] [probing_method]"; + } +}