forked from satojkovic/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table.cpp
More file actions
209 lines (187 loc) · 5.74 KB
/
Copy pathhash_table.cpp
File metadata and controls
209 lines (187 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
class HashEntry {
public:
std::string key;
int value;
HashEntry* next;
HashEntry() {
key = "";
value = -1;
next = nullptr;
}
HashEntry(std::string key, int value) {
this->key = key;
this->value = value;
this->next = nullptr;
}
};
class HashTable {
public:
HashEntry** bucket;
int slots;
int size;
HashTable(int s) {
bucket = new HashEntry*[s];
for (int i = 0; i < s; i++) {
bucket[i] = nullptr;
}
slots = s;
size = 0;
}
int getSize() {
return size;
}
bool isEmpty() {
return getSize() == 0;
}
int getIndex(std::string key) {
int Key = 0;
for (int i = 0; i < key.length(); i++) {
Key = 37 * Key + key[i];
}
if (Key < 0) {
Key *= -1;
}
return Key % slots;
}
void insert(std::string key, int value) {
int hash_index = getIndex(key);
if (bucket[hash_index] == nullptr) {
bucket[hash_index] = new HashEntry(key, value);
size++;
} else {
// chaining strategy
HashEntry* temp = bucket[hash_index];
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = new HashEntry(key, value);
size++;
}
}
void resize() {
slots *= 2;
HashEntry** to_bucket = new HashEntry*[slots];
for (int i = 0; i < slots; i++) {
to_bucket[i] = nullptr;
}
for (int i = 0; i < slots/2; i++) {
if (bucket[i] != nullptr) {
HashEntry* from_elem = bucket[i];
while (from_elem != nullptr) {
int hash_index = getIndex(from_elem->key);
if (to_bucket[hash_index] == nullptr) {
to_bucket[hash_index] = new HashEntry(from_elem->key, from_elem->value);
} else {
// Find next free space
HashEntry* to_elem = to_bucket[hash_index];
while (to_elem->next != nullptr) {
to_elem = to_elem->next;
}
to_elem->next = new HashEntry(from_elem->key, from_elem->value);
}
// Next element in the bucket[i]
from_elem = from_elem->next;
}
}
}
bucket = to_bucket;
}
int search(std::string key) {
int hash_index = getIndex(key);
if (bucket[hash_index] == nullptr) {
std::cout << "Not found: " << key << std::endl;
return -1;
}
if (bucket[hash_index]->key == key) {
return bucket[hash_index]->value;
} else {
// Find the value in the linked list
HashEntry* elem = bucket[hash_index];
while (elem != nullptr) {
if (elem->key == key) {
return elem->value;
}
elem = elem->next;
}
}
std::cout << "Not found: " << key << std::endl;
return -1;
}
void deleteKey(std::string key) {
int hash_index = getIndex(key);
if (bucket[hash_index] == nullptr) {
std::cout << "Not found: " << key << std::endl;
return;
}
// Delete the key in the linked list
if (bucket[hash_index]->key == key) {
if (bucket[hash_index]->next != nullptr) {
bucket[hash_index] = bucket[hash_index]->next;
} else {
bucket[hash_index] = nullptr;
}
size--;
return;
} else {
HashEntry* prev = bucket[hash_index];
HashEntry* curr = bucket[hash_index];
while (curr != nullptr) {
if (curr->key == key) {
if (curr->next != nullptr) {
prev->next = curr->next;
curr = curr->next;
} else {
prev->next = nullptr;
}
size--;
return;
}
prev = curr;
curr = curr->next;
}
std::cout << "Not found: " << key << std::endl;
}
}
void display() {
HashEntry* temp;
std::cout << "HT slots: " << slots << " , size: " << size << std::endl;
for (int i = 0; i < slots; i++) {
if (bucket[i] != nullptr) {
std::cout << "HashTable index : " << i << " ";
temp = bucket[i];
while (temp != nullptr) {
std::cout << "(key = " << temp->key << ", value = " << temp->value << " )";
temp = temp->next;
}
}
std::cout << std::endl;
}
}
};
int main() {
HashTable ht(4);
ht.insert("London",2);
ht.insert("London",10);
ht.insert("New York",15);
ht.insert("Tokyo",7);
ht.insert("Bangkok",2);
ht.display();
std::cout << "Resize HT" << std::endl;
ht.resize(); // increase slots to 8
ht.insert("Beijing",6);
ht.insert("Islamabad",9);
ht.insert("New Delhi",17);
ht.insert("Moscow",12);
ht.insert("Amsterdam",5);
ht.insert("Paris",13);
ht.display();
std::cout << "London => " << ht.search("London") << std::endl;
std::cout << "Moscow => " << ht.search("Moscow") << std::endl;
std::cout << "Islamabad => " << ht.search("Islamabad") << std::endl;
ht.deleteKey("London");
ht.deleteKey("Moscow");
ht.deleteKey("Islamabad");
ht.display();
return 0;
}