-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB+ Tree.cpp
263 lines (223 loc) · 6.19 KB
/
B+ Tree.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <string>
#include<bits/stdc++.h>
#include <dirent.h>
//#include <filesystem>
#include "B+ Tree.h"
#define _CRT_SECURE_NO_DEPRECATE //for VS 2019
bool hasEnding (std::string const &fullString, std::string const &ending)
{
if (fullString.length() >= ending.length())
{
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else
{
return false;
}
}
void printvec(vector<string> vec)
{
for (auto a : vec)
cout<<a<<" ";
cout << endl;
}
void printvec(vector<int> vec)
{
for (auto a : vec)
cout<<a<<" ";
cout << endl;
}
inline bool file_exists(string name) {
name="./DBFiles/"+name+".txt";
//cout<<"Checking if "<<name<<" exists"<<endl;
if (FILE *file = fopen(name.c_str(), "r"))
{
fclose(file);
return true;
} else
{
return false;
}
}
inline bool isNumber(const string str)
{
for (char const &c : str) {
if (isdigit(c) == 0)
return false;
}
return true;
}
void importMethod(BPTree** bPTree)
{
int ch;
while(1)
{
cout<<"\n\n\t\t\tWARNING!!"<<endl;
cout<<"This option is to import multiple record files into the tree at once.\nIt will scan the DBFiles folder and place all the files of the format \"RecordNo.txt\" in the B+ Tree.\nIt will insert only the Files/Records which are not already present in the tree\n\n";
cout<<"STEPS TO IMPORT :\n";
cout<<"\t1.Place the record text files in the DBFiles Folder\n\t2.Rename each file to the Record Number(The key that has to be used by B+ Tree)\n\nTo Continue \t: \tEnter 1\nTo Abort \t: \tEnter 2\nEnter your choice : ";
cin>>ch;
if(ch==2)
return;
else if(ch==1)
break;
else cout<<"Enter a valid choice.\n";
};
DIR *dir;
struct dirent *diread;
vector<string> files;
if ((dir = opendir("./DBFiles")) != nullptr) {
while ((diread = readdir(dir)) != nullptr) {
files.push_back(diread->d_name);
}
closedir (dir);
} else {
perror ("opendir");
exit(1);
}
vector<string> textfiles;
for (auto file : files)
{
if (hasEnding(file,".txt"))
textfiles.push_back(file.substr(0,file.size()-4));
};
vector<int> numbers;
for (auto file : textfiles)
{
if (isNumber(file))
{
if(file_exists(file))
numbers.push_back(atoi(file.c_str()));
}
};
//printvec(numbers);
if(numbers.size()==0)
{
cout<<"No eligible files found for importing!\n";
return;
}
for(int i=0;i<numbers.size();i++)
{
if((*bPTree)->search(numbers[i]).second==-1)
{
string fileName = "./DBFiles/";
fileName += to_string(numbers[i]) + ".txt";
FILE* filePtr = fopen(fileName.c_str(), "r");
(*bPTree)->insert(numbers[i], filePtr);
cout << "Insertion of Record No. : " << numbers[i] << " Successful"<<endl;
};
};
}
void insertionMethod(BPTree** bPTree) {
int rNo,dose;
string name,vaccine,mNo;
cout << "Please provide the Record No. : ";
cin >> rNo;
cout<<"Record "<<rNo<<" details :\n";
cout << "Name : ";
cin >> name ;
cout<<"Mobile No. : ";
cin>>mNo;
cout<<"Vaccine(Covishield/Covaxin) : ";
cin>> vaccine ;
cout<<"Dose number(1/2) : ";
cin>> dose;
string fileName = "DBFiles/";
fileName += to_string(rNo) + ".txt";
FILE* filePtr = fopen(fileName.c_str(), "w");
string userTuple = name + " " + mNo + " " + vaccine +" "+ to_string(dose) + "\n";
fprintf(filePtr, userTuple.c_str());
fclose(filePtr);
filePtr = fopen(fileName.c_str(), "r");
(*bPTree)->insert(rNo, filePtr);
//fclose(filePtr);
cout << "Insertion of Record No. : " << rNo << " Successful"<<endl;
}
void searchMethod(BPTree* bPTree) {
int rNo;
cout << "What's the Record No. to Search? ";
cin >> rNo;
pair<Node*,int> found=bPTree->search(rNo);
if(found.second==-1)
cout<<"Key Not found\n";
else
{
Node* cursor=found.first;
int idx=found.second;
cout << "Hurray!! Key FOUND" << endl;
cout << "Corresponding Tuple Data is: ";
char ch = fgetc(cursor->ptr2TreeOrData.dataPtr[idx]);
while (ch != EOF) {
printf("%c", ch);
ch = fgetc(cursor->ptr2TreeOrData.dataPtr[idx]);
};
fseek(cursor->ptr2TreeOrData.dataPtr[idx], 0, SEEK_SET);
cout<<endl;
};
}
void printMethod(BPTree* bPTree) {
int opt;
cout << "Press \n\t1.Hierarical-Display \n\t2.Sequential-Display\n";
cin >> opt;
if (opt == 1)
bPTree->display(bPTree->getRoot());
else
bPTree->seqDisplay(bPTree->getRoot());
}
void deleteMethod(BPTree* bPTree) {
cout << "Showing you the Tree, Choose a key from here: " << endl;
bPTree->display(bPTree->getRoot());
int tmp;
cout << "Enter a key to delete: " << endl;
cin >> tmp;
bPTree->removeKey(tmp);
//Displaying
bPTree->display(bPTree->getRoot());
}
int main() {
/*
Please have a look at the default schema to get to know about the table
Reference - img/database.jpg
*/
cout << "\n***Welcome to DATABASE SERVER***\n"
<< endl;
bool flag = true;
int option;
int maxChildInt = 4, maxNodeLeaf = 3;
cout << "Please provide the value to limit maximum child Internal Nodes can have: ";
cin >> maxChildInt;
cout << "\nAnd Now Limit the value to limit maximum Nodes Leaf Nodes can have: ";
cin >> maxNodeLeaf;
BPTree* bPTree = new BPTree(maxChildInt, maxNodeLeaf);
do {
cout << "\nPlease provide the queries with respective keys : " << endl;
cout << "\tPress 1: Import \n\tPress 2: Insertion \n\tPress 3: Search \n\tPress 4: Print Tree\n\tPress 5: Delete Key In Tree\n\tPress 6: ABORT!" << endl;
cin >> option;
switch (option) {
case 1:
importMethod(&bPTree);
break;
case 2:
insertionMethod(&bPTree);
break;
case 3:
searchMethod(bPTree);
break;
case 4:
printMethod(bPTree);
break;
case 5:
deleteMethod(bPTree);
break;
case 6:
flag=false;
break;
default:
cout<<"Enter valid option !"<<endl;
}
}while (flag);
return 0;
}