-
Notifications
You must be signed in to change notification settings - Fork 45
/
insertion.cpp
230 lines (195 loc) · 9.75 KB
/
insertion.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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include "B+ Tree.h"
using namespace std;
void BPTree::insert(int key, FILE* filePtr) { //in Leaf Node
/*
1. If the node has an empty space, insert the key/reference pair into the node.
2. If the node is already full, split it into two nodes, distributing the keys
evenly between the two nodes. If the node is a leaf, take a copy of the minimum
value in the second of these two nodes and repeat this insertion algorithm to
insert it into the parent node. If the node is a non-leaf, exclude the middle
value during the split and repeat this insertion algorithm to insert this excluded
value into the parent node.
*/
if (root == NULL) {
root = new Node;
root->isLeaf = true;
root->keys.push_back(key);
new (&root->ptr2TreeOrData.dataPtr) std::vector<FILE*>;
//// now, root->ptr2TreeOrData.dataPtr is the active member of the union
root->ptr2TreeOrData.dataPtr.push_back(filePtr);
cout << key << ": I AM ROOT!!" << endl;
return;
} else {
Node* cursor = root;
Node* parent = NULL;
//searching for the possible position for the given key by doing the same procedure we did in search
while (cursor->isLeaf == false) {
parent = cursor;
int idx = std::upper_bound(cursor->keys.begin(), cursor->keys.end(), key) - cursor->keys.begin();
cursor = cursor->ptr2TreeOrData.ptr2Tree[idx];
}
//now cursor is the leaf node in which we'll insert the new key
if (cursor->keys.size() < maxLeafNodeLimit) {
/*
If current leaf Node is not FULL, find the correct position for the new key and insert!
*/
int i = std::upper_bound(cursor->keys.begin(), cursor->keys.end(), key) - cursor->keys.begin();
cursor->keys.push_back(key);
cursor->ptr2TreeOrData.dataPtr.push_back(filePtr);
if (i != cursor->keys.size() - 1) {
for (int j = cursor->keys.size() - 1; j > i; j--) { // shifting the position for keys and datapointer
cursor->keys[j] = cursor->keys[j - 1];
cursor->ptr2TreeOrData.dataPtr[j] = cursor->ptr2TreeOrData.dataPtr[j - 1];
}
//since earlier step was just to inc. the size of vectors and making space, now we are simplying inserting
cursor->keys[i] = key;
cursor->ptr2TreeOrData.dataPtr[i] = filePtr;
}
cout << "Inserted successfully: " << key << endl;
} else {
/*
DAMN!! Node Overflowed :(
HAIYYA! Splitting the Node .
*/
vector<int> virtualNode(cursor->keys);
vector<FILE*> virtualDataNode(cursor->ptr2TreeOrData.dataPtr);
//finding the probable place to insert the key
int i = std::upper_bound(cursor->keys.begin(), cursor->keys.end(), key) - cursor->keys.begin();
virtualNode.push_back(key); // to create space
virtualDataNode.push_back(filePtr); // to create space
if (i != virtualNode.size() - 1) {
for (int j = virtualNode.size() - 1; j > i; j--) { // shifting the position for keys and datapointer
virtualNode[j] = virtualNode[j - 1];
virtualDataNode[j] = virtualDataNode[j - 1];
}
//inserting
virtualNode[i] = key;
virtualDataNode[i] = filePtr;
}
/*
BAZINGA! I have the power to create new Leaf :)
*/
Node* newLeaf = new Node;
newLeaf->isLeaf = true;
new (&newLeaf->ptr2TreeOrData.dataPtr) std::vector<FILE*>;
//// now, newLeaf->ptr2TreeOrData.ptr2Tree is the active member of the union
//swapping the next ptr
Node* temp = cursor->ptr2next;
cursor->ptr2next = newLeaf;
newLeaf->ptr2next = temp;
//resizing and copying the keys & dataPtr to OldNode
cursor->keys.resize((maxLeafNodeLimit) / 2 +1);//check +1 or not while partitioning
cursor->ptr2TreeOrData.dataPtr.resize((maxLeafNodeLimit) / 2 +1);
for (int i = 0; i <= (maxLeafNodeLimit) / 2; i++) {
cursor->keys[i] = virtualNode[i];
cursor->ptr2TreeOrData.dataPtr[i] = virtualDataNode[i];
}
//Pushing new keys & dataPtr to NewNode
for (int i = (maxLeafNodeLimit) / 2 + 1; i < virtualNode.size(); i++) {
newLeaf->keys.push_back(virtualNode[i]);
newLeaf->ptr2TreeOrData.dataPtr.push_back(virtualDataNode[i]);
}
if (cursor == root) {
/*
If cursor is root node we create new node
*/
Node* newRoot = new Node;
newRoot->keys.push_back(newLeaf->keys[0]);
new (&newRoot->ptr2TreeOrData.ptr2Tree) std::vector<Node*>;
newRoot->ptr2TreeOrData.ptr2Tree.push_back(cursor);
newRoot->ptr2TreeOrData.ptr2Tree.push_back(newLeaf);
root = newRoot;
cout << "Created new Root!" << endl;
} else {
// Insert new key in the parent
insertInternal(newLeaf->keys[0], &parent, &newLeaf);
}
}
}
}
void BPTree::insertInternal(int x, Node** cursor, Node** child) { //in Internal Nodes
if ((*cursor)->keys.size() < maxIntChildLimit - 1) {
/*
If cursor is not full find the position for the new key.
*/
int i = std::upper_bound((*cursor)->keys.begin(), (*cursor)->keys.end(), x) - (*cursor)->keys.begin();
(*cursor)->keys.push_back(x);
//new (&(*cursor)->ptr2TreeOrData.ptr2Tree) std::vector<Node*>;
//// now, root->ptr2TreeOrData.ptr2Tree is the active member of the union
(*cursor)->ptr2TreeOrData.ptr2Tree.push_back(*child);
if (i != (*cursor)->keys.size() - 1) { // if there are more than one element
// Different loops because size is different for both (i.e. diff of one)
for (int j = (*cursor)->keys.size() - 1; j > i; j--) { // shifting the position for keys and datapointer
(*cursor)->keys[j] = (*cursor)->keys[j - 1];
}
for (int j = (*cursor)->ptr2TreeOrData.ptr2Tree.size() - 1; j > (i + 1); j--) {
(*cursor)->ptr2TreeOrData.ptr2Tree[j] = (*cursor)->ptr2TreeOrData.ptr2Tree[j - 1];
}
(*cursor)->keys[i] = x;
(*cursor)->ptr2TreeOrData.ptr2Tree[i + 1] = *child;
}
cout << "Inserted key in the internal node :)" << endl;
} else { //splitting
cout << "Inserted Node in internal node successful" << endl;
cout << "Overflow in internal:( HAIYAA! splitting internal nodes" << endl;
vector<int> virtualKeyNode((*cursor)->keys);
vector<Node*> virtualTreePtrNode((*cursor)->ptr2TreeOrData.ptr2Tree);
int i = std::upper_bound((*cursor)->keys.begin(), (*cursor)->keys.end(), x) - (*cursor)->keys.begin(); //finding the position for x
virtualKeyNode.push_back(x); // to create space
virtualTreePtrNode.push_back(*child); // to create space
if (i != virtualKeyNode.size() - 1) {
for (int j = virtualKeyNode.size() - 1; j > i; j--) { // shifting the position for keys and datapointer
virtualKeyNode[j] = virtualKeyNode[j - 1];
}
for (int j = virtualTreePtrNode.size() - 1; j > (i + 1); j--) {
virtualTreePtrNode[j] = virtualTreePtrNode[j - 1];
}
virtualKeyNode[i] = x;
virtualTreePtrNode[i + 1] = *child;
}
int partitionKey; //exclude middle element while splitting
partitionKey = virtualKeyNode[(virtualKeyNode.size() / 2)]; //right biased
int partitionIdx = (virtualKeyNode.size() / 2);
//resizing and copying the keys & TreePtr to OldNode
(*cursor)->keys.resize(partitionIdx);
(*cursor)->ptr2TreeOrData.ptr2Tree.resize(partitionIdx + 1);
(*cursor)->ptr2TreeOrData.ptr2Tree.resize(partitionIdx + 1);
for (int i = 0; i < partitionIdx; i++) {
(*cursor)->keys[i] = virtualKeyNode[i];
}
for (int i = 0; i < partitionIdx + 1; i++) {
(*cursor)->ptr2TreeOrData.ptr2Tree[i] = virtualTreePtrNode[i];
}
Node* newInternalNode = new Node;
new (&newInternalNode->ptr2TreeOrData.ptr2Tree) std::vector<Node*>;
//Pushing new keys & TreePtr to NewNode
for (int i = partitionIdx + 1; i < virtualKeyNode.size(); i++) {
newInternalNode->keys.push_back(virtualKeyNode[i]);
}
for (int i = partitionIdx + 1; i < virtualTreePtrNode.size(); i++) { // because only key is excluded not the pointer
newInternalNode->ptr2TreeOrData.ptr2Tree.push_back(virtualTreePtrNode[i]);
}
if ((*cursor) == root) {
/*
If cursor is a root we create a new Node
*/
Node* newRoot = new Node;
newRoot->keys.push_back(partitionKey);
new (&newRoot->ptr2TreeOrData.ptr2Tree) std::vector<Node*>;
newRoot->ptr2TreeOrData.ptr2Tree.push_back(*cursor);
//// now, newRoot->ptr2TreeOrData.ptr2Tree is the active member of the union
newRoot->ptr2TreeOrData.ptr2Tree.push_back(newInternalNode);
root = newRoot;
cout << "Created new ROOT!" << endl;
} else {
/*
::Recursion::
*/
insertInternal(partitionKey, findParent(root, *cursor), &newInternalNode);
}
}
}