-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBPT.h
529 lines (406 loc) · 13.3 KB
/
BPT.h
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/** \file BPT.h
\brief Function prototypes and implementation for the BPT class
This contains the header file for BPT class.
It also contains constructors, mutators, accessors, and helper functions.
*/
#ifndef BplusTournamentSort_BPT_h
#define BplusTournamentSort_BPT_h
#include <iostream>
#include <vector>
#include <limits>
#include <assert.h>
#include <math.h>
#include "SequenceSet.h"
using namespace std;
template <class Type>
class BPT
{
private:
SequenceSet<Type>* root;
SequenceSet<Type>* head; // head of sequence set doubly linked list
SequenceSet<Type>* tail; // tail of sequence set doubly linked list
int ORDER;
string file_prefix;
string file_postfix;
int file_count;
// helper
/** \fn void rec_put(Type, SequenceSet<Type>*)
* \brief inserts the item into appropriate node
* \param item is an int or string
* \param p is a pointer to a node of type int or string
* \pre the parameter item should be an int or string
* \pre p should not be full
* \post the item is put into the correct node
*/
void rec_put(Type, SequenceSet<Type>*);
/** \fn bool rec_remove(Type, SequenceSet<Type>*)
* \brief removes the item from appropriate node
* \param item is an int or string
* \param p is a pointer to a node of type int or string
* \pre the parameter item should be an int or string
* \pre p should not be empty
* \post the item is removed from its location
* \post a boolean value is returned to indicate success or failure
*/
bool rec_remove(Type, SequenceSet<Type>*);
/** \fn Type getSmallest(SequenceSet<Type>*)
* \brief gets the appropriate leaf with the smallest item
* \param p is a pointer to a node of type int or string
* \pre the parameter p should not be empty
* \post The smallest item is returned
*/
Type getSmallest(SequenceSet<Type>*);
/** \fn push_key_up_from(SequenceSet<Type>*, Type)
* \brief connects newly created node to its parent
* \param p is a pointer to a node of type int or string
* \param item is an int or string
* \pre assumes that there is a newly created node
* \post the node is connected to its parent
*/
void push_key_up_from(SequenceSet<Type>*, Type);
/** \fn bool rebuild(SequenceSet<Type>*)
* \brief rebuilds the tree
* \param p is a pointer to a node of type int or string
* \pre the node should not be empty
* \pre the node should not be a leaf
* \post the tree is rebuilt
*/
bool rebuild(SequenceSet<Type>*);
public:
/**Default Constructor */
BPT();
/**Copy Constructor*/
BPT(int);
// Setter
/** \fn void put(Type)
* \brief This method just calls the private method void rec_put(Type, SequenceSet<Type>*)
* \param item is an int or string
* \pre
* \post the item is placed at the correct location
*/
void put(Type);
/** \fn void remove(Type)
* \brief This method just calls the private method void rec_remove(Type, SequenceSet<Type>*)
* \param item is an int or string
* \pre
* \post the item is removed from the location
*/
bool remove(Type);
// Getter
/** \fn void print()
* \brief This method prints all the items in the B+tree
* \pre The tree has items in it
* \post prints all the items
*/
void print();
};
// Definition of BPT Class
template <class Type>
BPT<Type>::BPT()
{
cout << "NOT USE BPT()\n";
// default order is 3
BPT(3);
/** endcode */
}
template <class Type>
BPT<Type>::BPT(int order)
{
ORDER = order;
file_count = 0;
file_prefix = "nodefile_";
file_postfix = ".txt";
// horizontal link
head = new SequenceSet<Type>(ORDER);
tail = new SequenceSet<Type>(ORDER);
// initial node(=leaf)
SequenceSet<Type>* node;
node = new SequenceSet<Type>(ORDER);
node->setLeaf(true);
node->setParent(NULL);
stringstream ss;
ss << ++file_count;
node->setFilename(file_prefix + ss.str() + file_postfix);
root = node;
ofstream outfile;
outfile.open(node->getFilename().c_str(), ios_base::trunc);
head->setBack(NULL);
head->setNext(node);
node->setBack(head);
node->setNext(tail);
tail->setBack(node);
tail->setNext(NULL);
}
// find appropriate leaf and insert the item.
// p can be leaf and middle node
template <class Type>
void BPT<Type>::rec_put(Type item, SequenceSet<Type>* p)
{
if (p->isLeaf())
{
if (p->isDataFull())
{
p->putData(item);
// Create new leaf
SequenceSet<Type>* newp = new SequenceSet<Type>(ORDER);
newp->setLeaf(true);
newp->setParent(p->getParent()); // same parent as p
stringstream ss;
ss << ++file_count;
newp->setFilename(file_prefix + ss.str() + file_postfix);
// horizontal link
SequenceSet<Type>* temp;
temp = p->getNext();
p->setNext(newp); // p => new node
newp->setBack(temp->getBack()); // p <= new node
newp->setNext(temp); // new node => p.next
// copy contents ( data)
int middle = (ORDER + 1) / 2; // when order size=3, middle is 2; when order=4, middle is 2
copy(p->getDataBegin()+middle, p->getDataBegin() + p->getSize(), newp->getDataBegin());
newp->setSize(p->getSize() - middle);
p->setSize(middle);
// push up new key
push_key_up_from(newp, getSmallest(newp));
ofstream outfile1;
outfile1.open(p->getFilename().c_str(), ios_base::trunc);
for (int i=0; i < p->getSize(); i++)
{
outfile1 << p->getDataAt(i) << '|';
}
ofstream outfile2;
outfile2.open(newp->getFilename().c_str(), ios_base::trunc);
for (int i=0; i < newp->getSize(); i++){
outfile2 << newp->getDataAt(i) << '|';
}
}
else
{
p->putData(item);
ofstream outfile;
outfile.open(p->getFilename().c_str(), ios_base::app);
outfile << item << '|';
}
}
else
{
int pos=0;
for (int i=0; i < p->getSize(); i++)
{
if (i == 0)
{
if (item < p->getDataAt(0))
{
pos = 0;
}
else
{
pos = 1;
}
}
else if (item >= p->getDataAt(i))
{
pos++;
}
}
rec_put(item, p->getChildAt(pos));
}
}
template <class Type>
bool BPT<Type>::rebuild(SequenceSet<Type>* p)
{
if (p == NULL || p->isLeaf() || p->getSize() == 0)
{
return false;
}else
{
SequenceSet<Type> tempNode = SequenceSet<Type>(ORDER);
int tempSize =0;
int tempCsize = 0;
for (int i=0; i<p->getCsize(); i++)
{
if (p->getChildAt(i)->getSize() == 0)
{
if (p->getChildAt(i)->isLeaf())
{
// relink horizontal connection
p->getChildAt(i)->getBack()->setNext(p->getChildAt(i)->getNext());
p->getChildAt(i)->getNext()->setBack(p->getChildAt(i)->getBack());
}
// child is empty node
p->getChildAt(i)->setParent(NULL);
delete p->getChildAt(i);
continue;
}
if (tempCsize == 0)
{
tempNode.writeChildAt(tempCsize++, p->getChildAt(i));
}
else
{
tempNode.writeDataAt(tempSize++, getSmallest(p->getChildAt(i)));
tempNode.writeChildAt(tempCsize++, p->getChildAt(i));
}
}
copy(tempNode.getDataBegin(), tempNode.getDataBegin() + tempSize, p->getDataBegin());
copy(tempNode.getChildrenBegin(), tempNode.getChildrenBegin() + tempCsize, p->getChildrenBegin());
p->setSize(tempSize);
p->setCsize(tempCsize);
rebuild(p->getParent());
return true;
}
}
template <class Type>
bool BPT<Type>::rec_remove(Type item, SequenceSet<Type>* p)
{
if (p->isLeaf())
{
bool found = false;
int indexWhenFound = 0;
for (int i=0; i < p->getSize(); i++)
{
if (p->getDataAt(i) == item)
{
found = true;
indexWhenFound = i;
}
}
if (found)
{
if (indexWhenFound != 0)
{
p->removeDataAt(indexWhenFound);
}
else
{
p->removeDataAt(0);
rebuild(p->getParent());
}
ofstream outfile;
outfile.open(p->getFilename().c_str(), ios_base::trunc);
for (int i=0; i < p->getSize(); i++)
{
outfile << p->getDataAt(i) << '|';
}
return true;
}
else
{
return false;
}
}
else
{
int pos=0;
for (int i=0; i < p->getSize(); i++)
{
if (i == 0)
{
if (item < p->getDataAt(0))
{
pos = 0;
}
else
{
pos = 1;
}
}else
if (item >= p->getDataAt(i))
{
pos++;
}
}
return rec_remove(item, p->getChildAt(pos));
}
}
template <class Type>
void BPT<Type>::put(Type item)
{
rec_put(item, root);
}
template <class Type>
bool BPT<Type>::remove(Type item)
{
return rec_remove(item, root);
}
template <class Type>
void BPT<Type>::print()
{
SequenceSet<Type>* traveler;
traveler = head->getNext();
while (traveler != NULL)
{
for (int i=0; i < traveler->getSize(); i++)
{
cout << traveler->getDataAt(i) << "|";
}
traveler = traveler->getNext();
cout << " |";
}
}
template <class Type>
Type BPT<Type>::getSmallest(SequenceSet<Type>* p)
{
if (p->isLeaf())
{
return p->getDataAt(0);
}
else
{
return getSmallest(p->getChildAt(0));
}
}
// this method is called for newly created node to connect (=set key & pointer) it to parent
// called by rec_put
template <class Type>
void BPT<Type>::push_key_up_from(SequenceSet<Type>* p, Type key)
{
SequenceSet<Type>* pp;
pp = p->getParent();
if (pp == NULL) // reached at root
{
// create new root node
// then same as separation case
// Creating new root
SequenceSet<Type>* newpp = new SequenceSet<Type>(ORDER);
newpp->setParent(NULL);
// set value and connections
newpp->putData(key);
newpp->putChild(root);
root->setParent(newpp);
newpp->putChild(p);
p->setParent(newpp);
root = newpp;
}
else
{ // has parent
if (pp->isDataFull()) // full => need separate
{
// store it first
pp->putData(key);
pp->putChild(p);
// then separate by creating new parent
SequenceSet<Type>* newpp = new SequenceSet<Type>(ORDER);
newpp->setParent(pp->getParent());
// copy contents (data & children) except middle key
int middle = (ORDER + 1) / 2;
Type middleValue = pp->getDataAt(middle);
copy(pp->getDataBegin() + middle + 1, pp->getDataBegin() + pp->getSize(), newpp->getDataBegin());
copy(pp->getChildrenBegin() + middle + 1, pp->getChildrenBegin() + pp->getCsize(), newpp->getChildrenBegin());
newpp->setSize(pp->getSize() - middle - 1);
newpp->setCsize(pp->getCsize() - middle - 1);
for (int i=0; i<newpp->getCsize(); i++)
{
newpp->getChildAt(i)->setParent(newpp);
}
pp->setSize(middle);
pp->setCsize(middle + 1);
push_key_up_from(newpp, middleValue);
}
else
{
pp->putData(key);
pp->putChild(p);
}
}
}
#endif