-
Notifications
You must be signed in to change notification settings - Fork 36
/
Hash_Table.cpp
176 lines (167 loc) · 4.19 KB
/
Hash_Table.cpp
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
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
/*
* Node Class Declaration
*/
template <class T>
class BSTNode
{
private:
T value;
BSTNode *left, *right;
public:
BSTNode(T value) //Constructor
{
this->value = value;
left = NULL;
right = NULL;
}
/*
* Adding element to a node
*/
void add(T& value)
{
if (value < this->value)
{
if (left)
left->add(value);
else
left = new BSTNode(value);
}
else if (this->value < value)
{
if (right)
right->add(value);
else
right = new BSTNode(value);
}
}
/*
* Check if node contains element
*/
bool contains(T& value)
{
if (value < this->value)
{
if (left)
return left->contains(value);
else
return false;
}
else if (this->value < value)
{
if (right)
return right->contains(value);
else
return false;
}
else
{
return true;
}
}
friend class BSTHashtable;
};
/*
* Table Class Declaration
*/
class BSTHashtable
{
private:
int size;
vector<BSTNode<string>*>* bucket;
int hash(string& s)
{
unsigned int hashVal = 0;
hashVal = s[s.length()-1];
return hashVal % size;
}
public:
BSTHashtable(int vectorSize)
{
size = vectorSize;
bucket = new vector<BSTNode<string>*>(size);
}
void add(string& s)
{
int index = hash(s);
if (bucket->at(index) == NULL) //Call friend to add string
bucket->at(index) = new BSTNode<string>(s);
else
bucket->at(index)->add(s);
}
bool contains(string& s)
{
int index = hash(s);
if (bucket->at(index) == NULL)
return false;
if( bucket->at(index)->contains(s)) //Call friend to search the String
{
cout<<"String \""<<s<<"\" found at index: "<<index<<endl;
return true;
}
else return false;
}
void display()
{
for (unsigned int i = 0; i < bucket->size(); i++)
{
cout <<"[" << i << "] -> ";
if (bucket->at(i) != NULL)
{
BSTNode<string> *node = bucket->at(i);
display_bst(node);
}
cout << "Null" << endl;
}
}
void display_bst(BSTNode<string> *node) // Calling Friend to show List Items using BST LDR Method
{
if (node!=NULL)
{
display_bst(node->left);
cout << node->value << " -> ";
display_bst(node->right);
}
}
};
int main()
{
BSTHashtable table(10);
string str;
int choice;
while (1)
{
cout<<"\t1.Insert element into the table"<<endl;
cout<<"\t2.Find element in the table"<<endl;
cout<<"\t3.Display Table Chained with Binary Tree"<<endl;
cout<<"\t0.Exit"<<endl;
cout<<"\tEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter String to be inserted: ";
cin>>str;
table.add(str);
break;
case 2:
cout<<"Enter String to search: ";
cin>>str;
if (table.contains(str) == 0)
cout<<"String \""<<str<<"\" not found in the table"<<endl;
break;
case 3:
cout<<"Displaying Table Chained with Binary Tree: "<<endl;
table.display();
break;
default:
cout<<"\nEnter correct option\n";
exit(0);
}
}
return 0;
}