-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTree.cpp
314 lines (295 loc) · 10.8 KB
/
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
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
//
// Created by Rohan Chander on 8/1/22.
//
// #pragma once
#include "Tree.h"
Tree::Tree() {
}
Tree::Tree(int maxNumChildren, int blockSize) {
this->maxNumChildren = maxNumChildren;
this->blockSize = blockSize;
this->root = nullptr;
this->size = 0;
}
void Tree::leafNodeInsertion(Car* toBeInserted, int indexFirstCarInNode, int indexLastCarInNode) {
if(leafNodes.empty()){
leafNodes.push_back(toBeInserted);
toBeInserted->setIndexInLeaves(0);
}
else{
int indexThatIsReplaced = indexFirstCarInNode;
if(indexLastCarInNode==0){
indexLastCarInNode++;
}
for(auto iter = leafNodes.begin() + indexFirstCarInNode; iter != leafNodes.begin() + indexLastCarInNode + 1; iter++) {
if((*iter)->getYear() > toBeInserted->getYear()) {
leafNodes.insert(iter, toBeInserted);
break;
}
else if((*iter)->getYear() == toBeInserted->getYear()) {
leafNodes.insert(iter+1, toBeInserted);
indexThatIsReplaced++;
break;
}
if(iter == leafNodes.begin()+indexLastCarInNode) {
leafNodes.insert(iter+1, toBeInserted);
indexThatIsReplaced++;
break;
}
indexThatIsReplaced++;
}
for(auto iter = leafNodes.begin() + indexThatIsReplaced; iter != leafNodes.end(); iter++) {
(*iter)->setIndexInLeaves(indexThatIsReplaced);
indexThatIsReplaced++;
}
}
}
//Can remove Node* as a parameter//
vector<Car *> Tree::Search(Node* root, int lowerYear, int upperYeat) {
Node* lowerBoundNode = this->root;
Node* upperBoundNode = this->root;
vector<Car*> outputVect;
while(!lowerBoundNode->leaf){
for(int i = 0; i < lowerBoundNode->size; i++){
//Passes the search onto the according children block
if(lowerYear < lowerBoundNode->block[i]->getYear()){
lowerBoundNode = lowerBoundNode->children[i];
break;
}
if(i == lowerBoundNode->size-1){
//Makes sure moves onto final children block//
lowerBoundNode = lowerBoundNode->children[i+1];
break;
}
}
}
while(!upperBoundNode->leaf){
for(int i = 0; i < upperBoundNode->size; i++){
//Passes the search onto the according children block
if(upperYeat < upperBoundNode->block[i]->getYear()){
upperBoundNode = upperBoundNode->children[i];
break;
}
if(i == upperBoundNode->size-1){
//Makes sure moves onto final children block//
upperBoundNode = upperBoundNode->children[i+1];
break;
}
}
}
int lowerIndexInLeaves = 0;
for(auto iter = lowerBoundNode->block.begin(); iter != lowerBoundNode->block.end(); iter++){
if((*iter)->getYear() < lowerYear){
lowerIndexInLeaves = (*iter)->getIndexInLeaves()+1;
break;
}
}
int upperIndexInLeaves = 0;
for(auto iter = upperBoundNode->block.begin(); iter != upperBoundNode->block.end(); iter++){
if((*iter)->getYear() > upperYeat){
upperIndexInLeaves = (*iter)->getIndexInLeaves();
break;
}
}
for(auto iter = leafNodes.begin() + lowerIndexInLeaves; iter != leafNodes.begin() + upperIndexInLeaves; iter++){
outputVect.push_back((*iter));
}
return outputVect;
}
void Tree::BlockInsertion(vector<Car*>& block, Car* car) {
for (auto iter = block.begin(); iter < block.end(); iter++) {
if ((*iter)->getYear() > car->getYear()) {
block.insert(iter, car);
break;
}
if(iter+1 == block.end()){
block.insert(iter+1, car);
break;
}
}
}
void Tree::ChildBlockInsertion(Node *parent, Node *child, Car* car) {
int indexChecker = -1;
for(auto iter = parent->block.begin(); iter < parent->block.end(); iter++){
indexChecker++;
if ((*iter)->getYear() > car->getYear()) {
parent->block.insert(iter, car);
break;
}
if(iter+1 == parent->block.end()){
parent->block.insert(iter+1, car);
indexChecker++;
break;
}
}
int checker = 0;
for(auto iter = parent->children.begin(); iter< parent->children.end(); iter++){
if(checker==indexChecker+1){
parent->children.insert(iter, child);
break;
}
if(iter+1 == parent->children.end()){
parent->children.insert(iter+1, child);
break;
}
checker++;
}
}
void Tree::ParentalInsert(Node *parent, Node *child, Car* car) {
if(parent->size < maxNumChildren-1){
ChildBlockInsertion(parent, child, car);
parent->size++;
}
else{
Node* anotherNode = new Node();
anotherNode->parent = parent->parent;
vector<Car*> parentBlockCopy;
for(int i = 0; i< parent->block.size(); i++){
parentBlockCopy.push_back(parent->block[i]);
}
BlockInsertion(parentBlockCopy, car);
vector<Node*> childrenOfParentCopy;
for(int i = 0; i< parent->size+1; i++){
childrenOfParentCopy.push_back(parent->children[i]);
}
childrenOfParentCopy[parent->size+1] = nullptr;
//Maybe can use blockInsertion instead//
int replacementIndex = 0;
for(int i = 0; i< parent->size; i++){
if(car->getYear() < parentBlockCopy[i]->getYear()){
replacementIndex = i;
break;
}
if(i == parent->size-1){
replacementIndex = parent->size-1;
break;
}
}
int indexCheck = 0;
for(auto iter = childrenOfParentCopy.begin(); iter< childrenOfParentCopy.end(); iter++){
if(indexCheck==replacementIndex){
childrenOfParentCopy.insert(iter, child);
break;
}
indexCheck++;
}
//Splitting of parent node into 2//
parent->size = maxNumChildren/2;
if(maxNumChildren % 2 == 0){
anotherNode->size = maxNumChildren/2-1;
}
else{
anotherNode->size = maxNumChildren/2;
}
//Copying data over from ParentBlockCopy back into anotherNode and Parent node//
parent->block.clear();
parent->children.clear();
for(int i = 0; i< parent->size; i++){
parent->block.push_back(parentBlockCopy[i]);
parent->children.push_back(childrenOfParentCopy[i]);
}
parent->children.push_back(childrenOfParentCopy[parent->size]);
// parent->children[parent->size] = childrenOfParentCopy[parent->size];
for(int i = 0; i< anotherNode->size; i++){
anotherNode->block.push_back(parentBlockCopy[parent->size+i+1]);
anotherNode->children.push_back(childrenOfParentCopy[i+1+parent->size]);
anotherNode->children[i]->parent = anotherNode;
}
anotherNode->children.push_back(childrenOfParentCopy[anotherNode->size+parent->size+1]);
//anotherNode->children[anotherNode->size] = childrenOfParentCopy[anotherNode->size+parent->size+1];
anotherNode->children[anotherNode->size]->parent = anotherNode;
//Handles pushing up to parent//
//In case is root//
Car* c = parentBlockCopy[maxNumChildren/2];
if(parent->parent == nullptr){
Node* newRoot = new Node(c);
newRoot->size++;
parent->parent = newRoot;
anotherNode->parent = newRoot;
newRoot->children.push_back(parent);
newRoot->children.push_back(anotherNode);
this->root = newRoot;
}
else{
ParentalInsert(parent->parent, anotherNode, c);
}
}
}
void Tree::Insert(Car* car) {
if(this->root == nullptr){
this->root = new Node(car);
this->size++;
this->root->leaf = true;
this->root->size++;
leafNodeInsertion(car, 0, 0);
}
else{
Node* search = root;
//Searches inside tree for appropriate place to put, traverses down to leaves//
while(!search->leaf){
for(int i = 0; i < search->size; i++){
//Passes the search onto the according children block
if(car->getYear() < search->block[i]->getYear()){
search = search->children[i];
break;
}
if(i == search->size-1){
//Makes sure moves onto final children block//
search = search->children[i+1];
break;
}
}
}
if(search->size < this->maxNumChildren-1){
BlockInsertion(search->block, car);
search->size++;
leafNodeInsertion(car, search->block[0]->getIndexInLeaves(), search->block[search->size-1]->getIndexInLeaves());
}
else{
Node* splitNode = new Node();
splitNode->leaf = true;
splitNode->parent = search->parent;
vector<Car*> copyBlock;
for(int i = 0; i< search->size; i++){
copyBlock.push_back(search->block[i]);
}
leafNodeInsertion(car, copyBlock[0]->getIndexInLeaves(), copyBlock[copyBlock.size()-1]->getIndexInLeaves());
//Inserting into copy//
BlockInsertion(copyBlock, car);
//Resizing//
search->size = this->maxNumChildren/2;
if(this->maxNumChildren % 2 == 0){
splitNode->size = this->maxNumChildren;
}
else{
splitNode->size = this->maxNumChildren /2 + 1;
}
//CHECK IF OTHER CARS IN BLOCK R DELETED
search->block.clear();
for(int i = 0; i< search->size; i++){
search->block.push_back(copyBlock[i]);
}
for(int i = 0; i< splitNode->size; i++){
splitNode->block.push_back(copyBlock[search->size+i]);
}
//Something with parent//
Car* c = splitNode->block[0];
//Root case
if(search->parent == nullptr){
Node* rootBlock = new Node(c);
rootBlock->size++;
search->parent = rootBlock;
splitNode->parent = rootBlock;
rootBlock->children.push_back(search);
rootBlock->children.push_back(splitNode);
this->root = rootBlock;
}
else{
ParentalInsert(search->parent, splitNode, c);
}
}
}
}
Tree::Node *Tree::getRoot() const {
return root;
}