-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTree.h
821 lines (721 loc) · 18.6 KB
/
BTree.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
#pragma once
#include "LinkedList.h"
template <class data>
class BTree;
template <class data>
class BTNode {
public:
data* Keys;
BTNode<data>** Children;
LinkedList<data>** Links;
short int KeyIndex;
short int ChildIndex;
const short int Order;
ostream& operator<<(ostream& output)
{
output << this->ID;
this->DisplayKeys();
}
void InitLinks();
void InitChildren();
void InsertSorted(BTNode<data>**& Arr, const int Index);
void ClearNulls(BTNode<data>**&, short int&);
public:
BTNode(const int);
BTNode(const int, const data);
void InsertKey(const data);
void InsertKey(const data, LinkedList<data>*&);
void InsertLink(const data val);
void InsertChild(BTNode<data>*);
void DeleteChild(const int);
int DeleteKey(const data);
const data MidKey();
const data MaxKey();
const data MinKey();
bool SearchKeys(const data val);
bool NoChildSpace();
bool NoKeySpace();
bool KeysFull();
bool ChildrenFull();
bool IsLeafNode();
void MergeNodes(BTNode<data>*);
void DeleteLinks(const int);
void DisplaySpecific(const data);
void DisplayKeys();
void Display();
~BTNode();
friend const Field<int> DefKey(BTNode<data>* Child, Field<int>);
friend const Field<char> DefKey(BTNode<data>* Child, Field<char>);
friend const Field<double> DefKey(BTNode<data>* Child, Field<double>);
friend const Field<string> DefKey(BTNode<data>* Child, Field<string>);
friend class BTree<data>;
};
template<class data>
inline void BTNode<data>::InitLinks()
{
for (int i = 0; i < Order; i++)
Links[i] = nullptr;
}
template<class data>
inline void BTNode<data>::InitChildren()
{
for (int i = 0; i <= Order; i++)
Children[i] = nullptr;
}
inline const Field<int> DefKey(BTNode<Field<int>>* Child, Field<int>)
{
if (Child == nullptr) return 1000000;
if (Child->Keys == nullptr) return 10000000;
else return Child->Keys[0];
}
inline const Field<double> DefKey(BTNode<Field<double>>* Child, Field<double>)
{
if (Child == nullptr) return 1000000;
if (Child->Keys == nullptr) return 10000000;
else return Child->Keys[0];
}
inline const Field<string> DefKey(BTNode<Field<string>>* Child, Field<string>)
{
if (Child == nullptr) return string("AAAAAAAAAAAAAAA");
if (Child->Keys == nullptr) return string("AAAAAAAAAAAAAAAAA");
else return Child->Keys[0];
}
inline const Field<char> DefKey(BTNode<Field<char>>* Child, Field<char>)
{
if (Child == nullptr) return 256;
if (Child->Keys == nullptr) return 256;
else return Child->Keys[0];
}
template<class data>
inline void SortedInsertion(data*& Array, LinkedList<data>**& links,int index) //SortsKeys
{
data temp;
LinkedList<data>* tlink;
while (index > 0 && (Array[index] < Array[index - 1] || Array[index-1] < data()))
{
temp = Array[index];
Array[index] = Array[index - 1];
Array[index - 1] = temp;
tlink = links[index];
links[index] = links[index - 1];
links[index - 1] = tlink;
index--;
}
}
inline void SortedInsertion(Field<string>*& Array, LinkedList<Field<string>>**& links, int index) //SortsKeys
{
Field<string> temp;
LinkedList<Field<string>>* tlink;
while (index > 0 && (Array[index] < Array[index - 1]))
{
temp = Array[index];
Array[index] = Array[index - 1];
Array[index - 1] = temp;
tlink = links[index];
links[index] = links[index - 1];
links[index - 1] = tlink;
index--;
}
}
template <class data>
void ShiftRight(BTNode<data>**& Array, const int index, const int size)
{
BTNode<data>* temp;
for (int i = size; i >= index; i--)
Array[i] = Array[i - 1];
}
template <class data>
void ShiftLeft(BTNode<data>**& Array, const int index, const int size)
{
BTNode<data>* temp;
for (int i = index; i < size - 1; i++)
Array[i] = Array[i + 1];
Array[size - 1] = nullptr;
}
template <class data>
void ShiftLeft(data*& Array, LinkedList<data>**& links,const int index, int size)
{
for (int i = index; i < size - 1; i++)
{
Array[i] = Array[i + 1];
links[i] = links[i + 1];
}
//links[size - 1].~LinkedList(); //
links[size - 1] = nullptr;
Array[size - 1] = 0;
size--;
}
template <class data>
inline void BTNode<data>::InsertSorted(BTNode<data>**& Array, int index) //SortsChildren
{
bool Swap = false;
BTNode<data>* temp;
for (int i = 0; i < index; i++)
for (int j = i + 1; j < index; j++)
if (Array[j] == nullptr)
continue;
else if (DefKey(Array[i], data()) > DefKey(Array[j], data()))
{
temp = Array[i];
Array[i] = Array[j];
Array[j] = temp;
}
}
template<class data>
inline BTNode<data>::BTNode(const int size) : Order(size)
{
Keys = new data[Order];
Links = new LinkedList<data>*[Order];
Children = new BTNode<data> *[Order+1];
KeyIndex = 0;
InitLinks();
ChildIndex = 0;
InitChildren();
}
template<class data>
inline BTNode<data>::BTNode(const int size, const data InitVal) : Order(size)
{
Keys = new data[Order];
Links = new LinkedList<data>*[Order];
Children = new BTNode<data> *[Order+1];
KeyIndex = 0;
ChildIndex = 0;
InitLinks();
InitChildren();
InsertKey(InitVal);
}
template<class data>
inline void BTNode<data>::InsertKey(const data val)
{
if (NoKeySpace())
return;
Keys[KeyIndex++] = val;
SortedInsertion(Keys, Links, KeyIndex - 1);
}
template<class data>
inline void BTNode<data>::InsertKey(const data val, LinkedList<data>*& link)
{
if (NoKeySpace())
return;
Links[KeyIndex] = link;
//link = nullptr;
Keys[KeyIndex++] = val;
SortedInsertion(Keys, Links, KeyIndex - 1);
}
template<class data>
inline void BTNode<data>::InsertLink(const data val)
{
for (int i = 0; i < KeyIndex; i++)
if (Keys[i] == val)
{
if (Links[i] == nullptr)
Links[i] = new LinkedList<data>;
Links[i]->Insert(val);
break;
}
}
template <class data>
void BTNode<data>::ClearNulls(BTNode<data>**& Array, short int& index)
{
for (int i = 0; i < index; i++)
if (Array[i] == nullptr)
ShiftLeft(Array, i, index);
}
template<class data>
inline void BTNode<data>::InsertChild(BTNode<data>* curr)
{
if (NoChildSpace())
return;
Children[ChildIndex++] = curr;
if (curr == nullptr) ChildIndex--;
ClearNulls(Children, ChildIndex);
InsertSorted(Children, ChildIndex);
}
template <class data>
void FillDeletionSpace(BTNode<data>**& Array, int StartIndex, const int size)
{
for (; StartIndex < size; StartIndex++)
Array[StartIndex] = Array[StartIndex + 1];
Array[StartIndex+1] = nullptr;
}
template<class data>
inline void BTNode<data>::DeleteChild(const int index)
{
if (index >= ChildIndex)
return;
cout << "Deleteing : "; Children[index]->Display();cout << endl;
delete Children[index];
Children[index] = nullptr;
FillDeletionSpace(Children, index, ChildIndex);
ChildIndex--;
}
template<class data>
inline int BTNode<data>::DeleteKey(const data val)
{
for (int i = 0; i < KeyIndex; i++)
if (Keys[i] == val)
{
Keys[i].Overwrite();
DeleteLinks(i);
Links[i] = nullptr;
//ShiftLeft(Keys, i, KeyIndex--);
return i;
}
}
template<class data>
inline const data BTNode<data>::MidKey()
{
if (Keys == nullptr)
return data();
return Keys[KeyIndex/2];
}
template<class data>
inline const data BTNode<data>::MaxKey()
{
if (Keys == nullptr)
return data();
return Keys[KeyIndex - 1];
}
template<class data>
inline const data BTNode<data>::MinKey()
{
if (Keys == nullptr)
return data();
return Keys[0];
}
template <class data>
bool AllRightNull(BTNode<data>**& Array, const int index, const int size)
{
for (int i = index; i < size; i++)
if (Array[i] != nullptr)
return false;
return true;
}
template<class data>
inline bool BTNode<data>::SearchKeys(const data val)
{
for (int i = 0; i < KeyIndex; i++)
if (val == Keys[i])
return true;
return false;
}
template<class data>
inline bool BTNode<data>::NoChildSpace()
{
return (KeyIndex == Order+1);
}
template<class data>
inline bool BTNode<data>::NoKeySpace()
{
return (KeyIndex == Order);
}
template<class data>
inline bool BTNode<data>::KeysFull()
{
return (KeyIndex == Order - 1);
}
template<class data>
inline bool BTNode<data>::ChildrenFull()
{
return (ChildIndex == Order);
}
template<class data>
inline bool BTNode<data>::IsLeafNode()
{
if (this == nullptr) return false;
return (Children[0] == nullptr);
}
template<class data>
inline void BTNode<data>::MergeNodes(BTNode<data>* Other)
{
if (this == Other)
return;
for (int i = 0; i < Other->KeyIndex; i++)
InsertKey(Other->Keys[i], Other->Links[i]);
for (int i = 0; i < Other->ChildIndex; i++)
InsertChild(Other->Children[i]);
}
template<class data>
inline void BTNode<data>::DeleteLinks(const int index)
{
if (Links[index] == nullptr)
return;
data DatField;
while (!Links[index]->IsEmpty())
{
DatField = Links[index]->Front();
DatField.Overwrite();
Links[index]->Delete();
}
}
template<class data>
inline void BTNode<data>::DisplaySpecific(const data req)
{
for (int i = 0; i < KeyIndex; i++)
if (Keys[i]== req)
{
cout << Keys[i] << endl;
if(Links[i]!= nullptr)
Links[i]->Display();
break;
}
}
template<class data>
inline void BTNode<data>::DisplayKeys()
{
for (int i = 0; i < KeyIndex; i++)
{
cout << Keys[i];
Links[i]->Display();
}
}
template<class data>
inline void BTNode<data>::Display()
{
if (this == nullptr) return;
if (IsLeafNode())
cout << "Leaf : ";
for (int i = 0; i < Order; i++)
{
cout << Keys[i] << " ";
Links[i].Display();
}
cout << endl;
cout << "|";
for (int i = 0; i < Order; i++)
Children[i]->Display();
}
template<class data>
inline BTNode<data>::~BTNode()
{
delete[] Keys;
delete[] Children;
//delete Links;
Children = nullptr;
Links = nullptr;
Keys = nullptr;
ChildIndex = 0;
KeyIndex = 0;
}
//======================================================================//
template <class data>
class BTree {
public:
const int Min;
const int Order;
BTNode<data>* root;
const string DefinedBy;
BTNode<data>* RecursiveInsert(BTNode<data>*&, const data, bool&);
BTNode<data>* RecursiveDelete(BTNode<data>*&, const data, bool&);
BTNode<data>* RecursiveSearch(BTNode<data>*&, const data);
BTNode<data>* SplitInTwo(BTNode<data>*);
void LDRTraversal(BTNode<data>*&);
void AttemptBorrow(BTNode<data>*&, const int);
void RecursiveRange(const BTNode<data>*, const data, const data);
typedef BTNode<data>* BTPtr;
public:
BTree(const int, const string);
//~BTree();
bool IsNull();
void Insert(const data val);
void Delete(const data val);
void Search(const data val);
BTNode<data>* Retrieve(const data val);
string GetDefinedBy();
void RangeSearch(const data, const data);
void InOrderTraversal();
void SaveTree();
void LoadTree();
};
template<class data>
inline BTNode<data>* BTree<data>::RecursiveSearch(BTNode<data>*& curr, const data val)
{
if (curr == nullptr)
return nullptr;
else if (curr->SearchKeys(val))
return curr;
else if (val < curr->MinKey() )
return RecursiveSearch(curr->Children[0], val);
else if (val > curr->MaxKey())
return RecursiveSearch(curr->Children[curr->ChildIndex-1], val);
else
{
for (int i = 0; i < curr->KeyIndex - 1; i++)
if (val > curr->Keys[i] && val < curr->Keys[i + 1])
return RecursiveSearch(curr->Children[i+1], val);
}
}
template<class data>
inline void BTree<data>::Insert(const data val)
{
bool NodesSplit = false;
if (root == nullptr)
root = new BTNode<data>(Order, val);
else
{
BTNode<data>* res = RecursiveInsert(root, val, NodesSplit);
if (!NodesSplit) root->MergeNodes(res);
else root = res;
}
//cout << endl;
//root->Display();
//cout << endl;
}
template<class data>
inline BTNode<data>* BTree<data>::RecursiveInsert(BTNode<data>*& curr, const data val, bool& Split)
{
BTNode<data>* res;
if (curr->SearchKeys(val))
curr->InsertLink(val);
else if (curr->IsLeafNode() && !curr->KeysFull())
return new BTNode<data>(Order, val);
else if (curr->IsLeafNode() && curr->KeysFull())
{
curr->InsertKey(val);
res = SplitInTwo(curr);
delete curr;
curr = nullptr;
Split = true;
return res;
}
else if (val < curr->Keys[0])
{
res = RecursiveInsert(curr->Children[0], val, Split);
if (Split)
{
curr->InsertChild(nullptr);
curr->ChildIndex--;
curr->MergeNodes(res);
}
else curr->Children[0]->MergeNodes(res);
Split = false;
}
else if (val > curr->Keys[curr->KeyIndex-1])
{
res = RecursiveInsert(curr->Children[curr->ChildIndex-1], val, Split);
if (Split)
{
curr->InsertChild(nullptr);
curr->ChildIndex--;
curr->MergeNodes(res);
}
else curr->Children[curr->ChildIndex - 1]->MergeNodes(res);
Split = false;
}
else
{
for (int i = 0; i < curr->KeyIndex - 1; i++)
if (val > curr->Keys[i] && val < curr->Keys[i + 1])
{
res = RecursiveInsert(curr->Children[i + 1], val, Split);
if (Split)
{
curr->InsertChild(nullptr);
curr->ChildIndex--;
curr->MergeNodes(res);
}
else curr->Children[i+1]->MergeNodes(res);
Split = false;
break;
}
}
if (curr->NoKeySpace())
{
res = SplitInTwo(curr);
delete curr;
curr = nullptr;
Split = true;
return res;
}
return curr;
}
template<class data>
inline void BTree<data>::Delete(const data val)
{
bool Merger = false;
RecursiveDelete(root, val, Merger);
}
template <class data>
void BTree<data>::AttemptBorrow(BTNode<data>*& curr, const int index)
{
data temp;
if (curr->Children[index - 1]->KeyIndex - 1 > Min)
{
temp = curr->Children[index - 1]->MaxKey();
curr->InsertKey(temp);
curr->Children[index - 1]->DeleteKey(temp);
}
else if (curr->Children[index]->KeyIndex-1 > Min)
{
temp = curr->Children[index]->MaxKey();
curr->InsertKey(temp);
curr->Children[index]->DeleteKey(temp);
}
else
{
curr->Children[index - 1]->MergeNodes(curr->Children[index]);
temp = curr->Children[index - 1]->MidKey();
curr->InsertKey(temp);
curr->Children[index - 1]->DeleteKey(temp);
curr->Children[index] = nullptr;
curr->InsertChild(nullptr);
}
}
template<class data>
inline BTNode<data>* BTree<data>::RecursiveDelete(BTNode<data>*& curr, const data val, bool& NodesMerged)
{
BTNode<data>* res;
if (curr == nullptr)
return nullptr;
else if (curr->SearchKeys(val))
curr->DeleteKey(val);
else if (val < curr->MinKey())
return RecursiveDelete(curr->Children[0], val, NodesMerged);
else if (val > curr->MaxKey())
return RecursiveDelete(curr->Children[curr->ChildIndex - 1], val, NodesMerged);
else for (int i = 0; i < curr->KeyIndex - 1; i++)
if (val > curr->Keys[i] && val < curr->Keys[i + 1])
return RecursiveDelete(curr->Children[i + 1], val, NodesMerged);
return curr;
}
template<class data>
inline BTNode<data>* BTree<data>::SplitInTwo(BTNode<data>* curr)
{
int MidPt;
if (curr->Order % 2 == 0) MidPt = (curr->Order + 1) / 2;
else MidPt = (curr->Order) / 2;
BTNode<data>* Parent = new BTNode<data>(Order, curr->Keys[MidPt]);
Parent->InsertChild(new BTNode<data>(Order));
Parent->InsertChild(new BTNode<data>(Order));
Parent->Links[0] = curr->Links[MidPt];
for (int i = 0; i < MidPt; i++)
{
Parent->Children[0]->InsertKey(curr->Keys[i], curr->Links[i]);
Parent->Children[0]->InsertChild(curr->Children[i]);
}
Parent->Children[0]->InsertChild(curr->Children[MidPt]);
for (int i = MidPt + 1; i < Order; i++)
{
Parent->Children[1]->InsertKey(curr->Keys[i], curr->Links[i]);
Parent->Children[1]->InsertChild(curr->Children[i]);
}
Parent->Children[1]->InsertChild(curr->Children[Order]);
return Parent;
}
template<class data>
inline void BTree<data>::LDRTraversal(BTNode<data>*& curr)
{
if (curr == nullptr)
return;
curr->Display();
cout << endl;
for (int index = 0; index < curr->ChildIndex; index++)
LDRTraversal(curr->Children[index]);
}
template<class data>
inline BTree<data>::BTree(const int size, const string Def) : Order(size) , DefinedBy(Def),
Min(size % 2 == 0 ? (size+1)/2 : (size)/2) {root = nullptr;}
template<class data>
inline bool BTree<data>::IsNull()
{
return root == nullptr;
}
template<class data>
inline void BTree<data>::Search(const data val)
{
BTNode<data>* curr = RecursiveSearch(root, val);
if (curr == nullptr)
{
cout << "No Record Found for : " << DefinedBy << " : " << val.Data << endl;
return;
}
curr->DisplaySpecific(val);
}
template<class data>
inline BTNode<data>* BTree<data>::Retrieve(const data val)
{
return RecursiveSearch(root,val);
}
template<class data>
inline string BTree<data>::GetDefinedBy()
{
return DefinedBy;
}
template<class data>
inline void BTree<data>::RangeSearch(const data Min, const data Max)
{
RecursiveRange(root, Min, Max);
}
template<typename data>
inline void BTree<data>::RecursiveRange(const BTNode<data>* curr, const data Min, const data Max)
{
if (curr == nullptr)
return;
if (curr->Keys[0] > Min)
RecursiveRange(curr->Children[0], Min, Max);
for(int i=0 ;i<curr->KeyIndex;i++)
if(curr->Keys[i] < Max && curr->Keys[i] > Min)
{
cout << curr->Keys[i] << endl;
if (curr->Links[i] != nullptr)
curr->Links[i]->Display();
}
for (int i = 1; i < curr->ChildIndex; i++)
RecursiveRange(curr->Children[i], Min, Max);
}
template<class data>
inline void BTree<data>::InOrderTraversal()
{
if (root == nullptr)
return;
root->Display();
}
template<class data>
inline void BTree<data>::LoadTree()
{
string filename = "IndexTrees/BTree_" + DefinedBy + ".csv";
ifstream file(filename, ios::in);
if (file.fail())
{
cout << "Unable to Open File" << endl;
return;
}
data Record;
string RecordData;
while (!file.eof())
{
getline(file, RecordData);
Record = CreateLoadNode(RecordData, Record.Data);
Insert(Record);
}
file.close();
}
template<class data>
inline void BTree<data>::SaveTree()
{
ofstream file("IndexTrees/BTree_" + DefinedBy + ".csv", ios::out);
if (file.fail())
{
cout << "Unable to Open File" << endl;
return;
}
string Record;
Queue<BTPtr> Que;
BTNode<data>* curr = root;
Que.Enqueue(curr);
while (!Que.IsEmpty())
{
curr = Que.Peek();
Que.Dequeue();
for (int i = 0; i < curr->KeyIndex; i++)
{
Record = CreateRecord(curr->Keys[i]);
file.write(Record.c_str(), Record.size());
if(curr->Links[i] != nullptr) curr->Links[i]->SaveFields(file);
}
for (int i = 0; i < curr->ChildIndex; i++)
if (curr->Children[i] != nullptr)
Que.Enqueue(curr->Children[i]);
}
file.close();
}