Skip to content

Commit 2bce8d6

Browse files
committed
1 parent 8e37824 commit 2bce8d6

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed

21- Hashing/easyaccess.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<iostream>
2+
#include<cstring>
3+
#include "hashtable.h"
4+
5+
using namespace std;
6+
int main(){
7+
hashtable<int>price_menu;
8+
price_menu.insert("Burger",100);
9+
price_menu.insert("Pizza",120);
10+
price_menu.insert("Potato",160);
11+
price_menu.insert("IceCream",130);
12+
price_menu.insert("Coldrink",190);
13+
price_menu.insert("Samosa",10)
14+
price_menu.print();
15+
16+
int *price= price_menu.search("IceCream");
17+
if(price==NULL){
18+
cout<<"Not Found"<<endl;
19+
}
20+
else{
21+
cout<<"Price is "<<*price<<endl;
22+
}
23+
price_menu["Dosa"]=60;
24+
price_menu["Dosa"]+=10;
25+
cout<<price_menu["Dosa"]<<endl;
26+
return 0;
27+
}

21- Hashing/easyaccess.h

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*When we insert the Key-value Pair the Table Size increases as well as load Factor increases.
2+
When the load factor> 0.75 means when the 75% of the table is filled then rehashing process done. */
3+
4+
5+
#include<iostream>
6+
#include<cstring>
7+
using namespace std;
8+
9+
template<typename T>
10+
class Node{
11+
public:
12+
string key;
13+
T value;
14+
Node<T>*next;
15+
Node(string key, T val){
16+
this->key= key;
17+
value= val;
18+
next= NULL;
19+
}
20+
~Node(){
21+
if(next!=NULL){
22+
delete next;
23+
}
24+
}
25+
};
26+
template<typename T>
27+
class Hashtable{
28+
Node<T>**table; //pointer to an array of an pointers
29+
30+
int current_size;
31+
int table_size;
32+
int hashfun(string key){
33+
int idx=0;
34+
int p=1;
35+
for(int j=0;j<key.length();j++){
36+
idx= idx+(key[j]*p)%table_size;
37+
idx= idx%table_size;
38+
p= (p*27)%table_size;
39+
}
40+
return idx;
41+
}
42+
void rehashing(){
43+
Node<T>**oldtable=table;
44+
int oldsize= table_size;
45+
table_size= 2*table_size; //
46+
table= new Node<T>*[table_size];
47+
for(int i=0;i<table_size;i++){
48+
table[i]=NULL;
49+
}
50+
current_size=0;
51+
//Shift the table from old table to new table;
52+
53+
for(int i=0;i<oldsize;i++){
54+
Node<T>*temp= oldtable[i];
55+
while(temp!=NULL){
56+
insert(temp->key,temp->value);
57+
temp= temp->next;
58+
}
59+
// Delete entire row in old table
60+
if(oldtable[i]!=NULL){
61+
delete oldtable[i];
62+
// Recursively destructive class.
63+
}
64+
}
65+
delete [] oldtable;
66+
}
67+
68+
public:
69+
Hashtable(int ts=7){
70+
table_size=ts;
71+
table= new Node<T>*[table_size];
72+
current_size=0;
73+
for(int i=0;i<table_size;i++){
74+
table[i]=NULL;
75+
}
76+
}
77+
void insert(string key, T value){
78+
int idx= hashfun(key);
79+
Node<T>*n= new Node<T>(key,value);
80+
n->next= table[idx];
81+
table[idx]=n;
82+
current_size++;
83+
84+
float load_factor= current_size/(1.0*table_size);
85+
if(load_factor>0.7){
86+
rehashing();
87+
}
88+
}
89+
void print(){
90+
for(int i=0;i<table_size;i++){
91+
cout<<"Bucket is "<<i<<"-->";
92+
Node<T>*temp= table[i];
93+
while(temp!=NULL){
94+
cout<<temp->key<<"-->";
95+
temp= temp->next;
96+
}
97+
cout<<endl;
98+
}
99+
}
100+
101+
T* search(string key){
102+
int index= hashfun(key);
103+
Node<T>*temp= table[index];
104+
while(temp!=NULL){
105+
if(temp->key==key){
106+
return &temp->value;
107+
}
108+
temp= temp->next;
109+
}
110+
return NULL;
111+
}
112+
T& operator[](string key){
113+
T*f= search(key);
114+
if(f==NULL){
115+
T garbage;
116+
insert(key,garbage);
117+
f= search(key);
118+
}
119+
return *f;
120+
}
121+
};

21- Hashing/searchKey.h

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*When we insert the Key-value Pair the Table Size increases as well as load Factor increases.
2+
When the load factor> 0.75 means when the 75% of the table is filled then rehashing process done. */
3+
4+
5+
#include<iostream>
6+
#include<cstring>
7+
using namespace std;
8+
9+
template<typename T>
10+
class Node{
11+
public:
12+
string key;
13+
T value;
14+
Node<T>*next;
15+
Node(string key, T val){
16+
this->key= key;
17+
value= val;
18+
next= NULL;
19+
}
20+
~Node(){
21+
if(next!=NULL){
22+
delete next;
23+
}
24+
}
25+
};
26+
template<typename T>
27+
class Hashtable{
28+
Node<T>**table; //pointer to an array of an pointers
29+
30+
int current_size;
31+
int table_size;
32+
int hashfun(string key){
33+
int idx=0;
34+
int p=1;
35+
for(int j=0;j<key.length();j++){
36+
idx= idx+(key[j]*p)%table_size;
37+
idx= idx%table_size;
38+
p= (p*27)%table_size;
39+
}
40+
return idx;
41+
}
42+
void rehashing(){
43+
Node<T>**oldtable=table;
44+
int oldsize= table_size;
45+
table_size= 2*table_size; //
46+
table= new Node<T>*[table_size];
47+
for(int i=0;i<table_size;i++){
48+
table[i]=NULL;
49+
}
50+
current_size=0;
51+
//Shift the table from old table to new table;
52+
53+
for(int i=0;i<oldsize;i++){
54+
Node<T>*temp= oldtable[i];
55+
while(temp!=NULL){
56+
insert(temp->key,temp->value);
57+
temp= temp->next;
58+
}
59+
// Delete entire row in old table
60+
if(oldtable[i]!=NULL){
61+
delete oldtable[i];
62+
// Recursively destructive class.
63+
}
64+
}
65+
delete [] oldtable;
66+
}
67+
68+
public:
69+
Hashtable(int ts=7){
70+
table_size=ts;
71+
table= new Node<T>*[table_size];
72+
current_size=0;
73+
for(int i=0;i<table_size;i++){
74+
table[i]=NULL;
75+
}
76+
}
77+
void insert(string key, T value){
78+
int idx= hashfun(key);
79+
Node<T>*n= new Node<T>(key,value);
80+
n->next= table[idx];
81+
table[idx]=n;
82+
current_size++;
83+
84+
float load_factor= current_size/(1.0*table_size);
85+
if(load_factor>0.7){
86+
rehashing();
87+
}
88+
}
89+
void print(){
90+
for(int i=0;i<table_size;i++){
91+
cout<<"Bucket is "<<i<<"-->";
92+
Node<T>*temp= table[i];
93+
while(temp!=NULL){
94+
cout<<temp->key<<"-->";
95+
temp= temp->next;
96+
}
97+
cout<<endl;
98+
}
99+
}
100+
101+
T* search(string key){
102+
int index= hashfun(key);
103+
Node<T>*temp= table[index];
104+
while(temp!=NULL){
105+
if(temp->key==key){
106+
return &temp->value;
107+
}
108+
temp= temp->next;
109+
}
110+
return NULL;
111+
}
112+
};

0 commit comments

Comments
 (0)