-
Notifications
You must be signed in to change notification settings - Fork 0
/
risingCity.cpp
734 lines (680 loc) · 14.7 KB
/
risingCity.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
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <string>
#include <queue>
using namespace std;
#define MaxHp 2000 //Maximum size of the heap
int min=0;
int gc=0; //Global Counter that increments
bool color; //color is defined as a boolean with red=1 and black =0
int sz=0; //Size of the heap
int ttl=0; //Total time for the city
int tempsz=0; //Size of temp Heap
ofstream outFile;
vector<int> bNum_vector;
vector<int> eTime_vector;
vector<int> tTime_vector;
//struct of RBT node
struct RBNode
{
bool color;
int bNum, eTime, tTime;
struct RBNode *lft, *rght, *parent;
RBNode(int b, int t) //constructor for RBT Node
{
bNum=b;
eTime=0; //Initial eTime is set to 0
tTime=t;
color=1; //Initial default color is set to red(1)
parent=NULL;
lft=NULL;
rght=NULL;
}
};
//Prototype for RBNode functions
RBNode* sblngOf(struct RBNode *x);
RBNode* uncleOf(struct RBNode *x);
RBNode* gParent(struct RBNode *x);
bool withRdChild(struct RBNode *x);
bool isLChild(struct RBNode *x);
void goDwn(struct RBNode *newNode, struct RBNode *x);
//struct for RB Tree
struct RBTree
{
struct RBNode *root;
RBTree()
{
root=NULL;
}
};
//Prototype for RBTree functions
RBTree tree;
void swapClr(RBNode *x, RBNode *y);
void swapVlue(RBNode *x, RBNode *y);
void RBUpdate(int n);
void RBInsrtNode(int n, int m);
void RBDlt(int n);
void inOrdr(RBNode *x, int n);
void RBPrnt(int a);
void RBPrnt(int a,int b);
void LvlOrderPrnt();
void RRrotate(RBNode *x);
void LLrotate(RBNode *x);
void RRcorrect(RBNode *x);
RBNode *lftMost(RBNode *x);
RBNode *dltSubs(RBNode *x);
void BBcorrect(RBNode *x);
void dltNode(RBNode *x);
void lvlOrdr(RBNode *x);
RBNode* rtrnRoot();
RBNode* search(int n);
//struct for the minHeap
struct minHp
{
int eTime;
int bNum;
int tTime;
};
//Prototypes for minHeap functions
struct minHp hp[MaxHp]; //array of struct type MinHeap
void hpify(minHp *h, int i, int n);
int hParent(int i);
struct minHp tempHp[10];
bool isLChild(struct RBNode *x) //To check if the node is Left Child of parent
{
if(x==x->parent->lft)
return true;
else
return false;
}
bool withRdChild(struct RBNode *x) //To check if node has a Red Child
{
if((x->rght!=NULL and x->rght->color==1)or(x->lft!=NULL and x->lft->color==1))
return true;
else
{
return false;
}
}
RBNode* gParent(struct RBNode *x) //To find the GrandParent of a node
{
return x->parent->parent;
}
RBNode* sblngOf(struct RBNode *x) //To return the sibling of a node
{
if (x->parent==NULL)
return NULL;
else if (isLChild(x)==1)
return x->parent->rght;
else
return x->parent->lft;
}
RBNode* uncleOf(struct RBNode *x) //To return the uncle of a node
{
if ((x->parent==NULL)or(gParent(x)==NULL))
return NULL;
else if (isLChild(x->parent)==1)
return gParent(x)->rght;
else
return gParent(x)->lft;
}
RBNode* rtrnRoot() //Returns the root
{
return tree.root;
}
void goDwn(struct RBNode *newNode, struct RBNode *x) // Goes down the RBT to put the node in the right position
{
if (x->parent!=NULL)
{
if (isLChild(x)==0)
x->parent->rght=newNode;
else
x->parent->lft=newNode;
}
newNode->parent=x->parent;
x->parent=newNode;
}
void LLrotate(struct RBNode *x) //Rotates the the tree with the node in a LL position
{
RBNode *newParent=x->lft;
if (x==tree.root)
tree.root=newParent;
goDwn(newParent,x);
x->lft=newParent->rght;
if (newParent->rght != NULL)
newParent->rght->parent=x;
newParent->rght=x;
}
void RRrotate(struct RBNode *x) //Rotates the the tree with the node in a RR position
{
RBNode *newParent=x->rght;
if (x==tree.root)
tree.root=newParent;
goDwn(newParent,x);
x->rght=newParent->lft;
if (newParent->lft!=NULL)
newParent->lft->parent=x;
newParent->lft = x;
}
void RRcorrect(RBNode *x) //To correct a Red-Red condition
{
if (x==tree.root)
{
x->color= 0;
return;
}
RBNode *parent = x->parent;
RBNode *grandparent = gParent(x);
RBNode *uncle =uncleOf(x);
if (parent->color!=0)
{
if (uncle!=NULL&&uncle->color==1)
{
parent->color = 0;
uncle->color= 0;
grandparent->color = 1;
RRcorrect(grandparent);
}
else
{
if (isLChild(parent))
{
if (isLChild(x))
swapClr(parent, grandparent);
else
{
RRrotate(parent);
swapClr(x, grandparent);
}
LLrotate(grandparent);
}
else
{
if (isLChild(x))
{
LLrotate(parent);
swapClr(x, grandparent);
}
else
swapClr(parent, grandparent);
RRrotate(grandparent);
}
}
}
}
void swapClr(struct RBNode *x, struct RBNode *y) //To swap the colors of two nodes
{ bool temp; //creates temporary color
temp=x->color;
x->color=y->color;
y->color=temp;
}
void swapVlue(struct RBNode *x, struct RBNode *y) //To swap the data values in two nodes
{
RBNode *temp=new RBNode(0,0); //creates temporary node
temp->bNum=x->bNum;
temp->tTime=x->tTime;
temp->eTime=x->eTime;
x->bNum=y->bNum;
x->tTime=y->tTime;
x->eTime=y->eTime;
y->bNum=temp->bNum;
y->tTime=temp->tTime;
y->eTime=temp->eTime;
}
RBNode* lftMost(RBNode *x) //Returns the leftmost node in the tree
{
RBNode *temp=x;
while(temp->lft!=NULL)
temp=temp->lft;
return temp;
}
RBNode* dltSubs(RBNode *x) //Finds a substitute node for the node that is deleted
{
if (x->lft!=NULL and x->rght!=NULL)
return lftMost(x->rght);
if (x->lft==NULL and x->rght==NULL)
return NULL;
if (x->lft!=NULL)
return x->lft;
else
return x->rght;
}
void BBcorrect(RBNode *x)
{
if (x==tree.root)
return;
RBNode *sibling=sblngOf(x), *parent=x->parent;
if (sibling==NULL)
BBcorrect(parent);
else
{
if (sibling->color==1)
{
parent->color= 1;
sibling->color = 0;
if (isLChild(sibling))
LLrotate(parent);
else
RRrotate(parent);
BBcorrect(x);
}
else
{
if (withRdChild(sibling))
{
if (sibling->lft != NULL and sibling->lft->color==1)
{
if (isLChild(sibling))
{
sibling->lft->color=sibling->color;
sibling->color=parent->color;
LLrotate(parent);
}
else
{
sibling->lft->color = parent->color;
LLrotate(sibling);
RRrotate(parent);
}
}
else
{
if (isLChild(sibling))
{
sibling->rght->color=parent->color;
RRrotate(sibling);
LLrotate(parent);
}
else
{
sibling->rght->color=sibling->color;
sibling->color=parent->color;
RRrotate(parent);
}
}
parent->color = 0;
}
else
{
sibling->color=1;
if (parent->color==0)
BBcorrect(parent);
else
parent->color=0;
}
}
}
}
void dltNode(RBNode *x) //To Delete a node in the RBT
{
RBNode *u=dltSubs(x); //Node to be used for substitution
bool bothBlack=((u==NULL or u->color==0) and (x->color==0));
RBNode *parent=x->parent;
if (u==NULL)
{
if (x==tree.root)
tree.root=NULL;
else
{
if (bothBlack)
BBcorrect(x);
else
{
if (sblngOf(x)!= NULL)
sblngOf(x)->color=1;
}
if (isLChild(x))
parent->lft = NULL;
else
parent->rght = NULL;
}
delete x;
return;
}
if (x->lft==NULL or x->rght==NULL)
{
if (x==tree.root)
{
x->bNum=u->bNum;
x->tTime=u->tTime;
x->eTime=u->eTime;
x->lft=x->rght=NULL;
delete u;
}
else
{
if (isLChild(x))
parent->lft = u;
else
parent->rght = u;
delete x;
u->parent = parent;
if (bothBlack)
BBcorrect(u);
else
u->color= 0;
}
return;
}
swapVlue(u, x);
dltNode(u);
}
RBNode* search(int n) //Finds the node for the next insertion, deletion or Print based on bNum.
{
RBNode *temp=tree.root; //Creation of a temporary node
while (temp!=NULL)
{
if (n==temp->bNum)
break;
else if (n<temp->bNum)
{
if (temp->lft==NULL)
break;
else
temp=temp->lft;
}
else
{
if (temp->rght==NULL)
break;
else
temp=temp->rght;
}
}
return temp; //Returns search result
}
void RBInsrtNode(int n, int m) //Inserts a node with bNum and tTiem values
{
RBNode *newNode=new RBNode(n,m);
if (tree.root==NULL)
{
newNode->color=0;
tree.root=newNode; //Node is inserted as root
}
else
{
RBNode *t=search(n);
if (t->bNum==n)
{
cout<<"\nBuilding is already present!\n";
return;
}
newNode->parent=t;
if(n<t->bNum) //Added as left child
t->lft=newNode;
else //Added as right child
t->rght=newNode;
RRcorrect(newNode);
}
}
void RBDlt(int n) //Deletes node by a particular bNum
{
if (tree.root==NULL) //Tree is empty
return;
RBNode *v=search(n);
if (v->bNum!=n)
{
cout << "Unable to find node with bNum:" << n << endl;
return;
}
dltNode(v);
}
void RBPrnt(int a) //Prints RBT with one bNum
{
RBNode *sn=search(a);
if(sn->bNum==a)
outFile<<"("<<sn->bNum<<","<<sn->eTime<<","<<sn->tTime<<")"<<endl;
else
outFile<<"(0,0,0)"<<endl;
}
void RBPrnt(int a, int b) //
{ if(sz==0)
outFile<<"(0,0,0)"<<endl;
int FirstB=1;
int Bpresent=0;
for (int i=a;i<=b;i++)
{
RBNode *sn =search(i);
if (sn!=NULL && sn->bNum == i && FirstB==1)
{
outFile<< "(" << sn->bNum << "," << sn->eTime << "," << sn->tTime << ")";
Bpresent = 1;
FirstB = 0;
}
else if (sn!= NULL && sn->bNum == i && FirstB==0 )
{
outFile<< ",(" << sn->bNum << "," << sn->eTime << "," << sn->tTime << ")";
Bpresent = 1;
}
}
outFile<<endl;
}
void RBUpdate(int n)//Updates the eTime in the RBT when it changes in the heap
{
//int a=x->bNum;
RBNode *x=search(n);
x->eTime=x->eTime+1;
}
bool procd=true; //Tells when to proceed with the execution
void Insert(int bnum, int ttime) //Inserts into the heap with bNum and tTime
{
if (sz>MaxHp)
{
cout <<"Size has exceeded maximum size!\n";
return;
}
if(procd==true)
{
sz=sz+1;
int i=sz-1;
hp[i].tTime=ttime;
hp[i].bNum=bnum;
hp[i].eTime=0;
int j=hParent(i);
while (i!=0 and hp[i].eTime<=hp[j].eTime)
{
if(hp[j].eTime==hp[i].eTime)
{
if(hp[j].bNum>hp[i].bNum)
{
swap(hp[i], hp[j]);
i=j;
j=hParent(i);
}
else
{
i=j;
j=hParent(i);
}
}
else if(hp[j].eTime>hp[i].eTime)
{
swap(hp[i], hp[j]);
i =j;
j=hParent(i);
}
}
}
else
{
tempHp[tempsz].bNum=bnum;
tempHp[tempsz].tTime=ttime;
tempHp[tempsz].eTime=0;
tempsz++;
}
}
int hParent(int i) //Returns the parent of the heap
{
return floor((i-1)/2);
}
void pushTmp() //Pushes from the temp heao to the main
{
int i=0;
while(i<tempsz)
{
Insert(tempHp[i].bNum, tempHp[i].tTime);
tempsz--;
i++;
}
tempsz=0;
}
void constrFin() //Finishes the last part of Building construction
{
hpify(hp,sz,0);
procd=true;
if(tempsz>0)
{
pushTmp();
}
}
void construct() //Constructs a building
{
if(sz<=0)
return;
hp[0].eTime=hp[0].eTime+1;
RBUpdate(hp[0].bNum);
if(hp[0].eTime==hp[0].tTime)
{
if (sz==1)
ttl=gc; // Sets the total time when it is the last node
outFile<<"("<<hp[0].bNum<<","<<gc<<")"<<endl;
int q=hp[0].bNum;
RBDlt(q);
hp[0]=hp[sz-1];
sz=sz-1;
constrFin();
}
else if(hp[0].eTime%5==0)
{
constrFin();
}
else
procd=false;
}
void hpify(minHp *hp, int n, int i) //Restores the minHeap property
{
int small=i;
int l=2*i+1; //Left Sibling
int r=2*i+2; //Right Sibling
if((r<n)&&(hp[r].eTime==hp[i].eTime))
{ if(hp[r].eTime==hp[l].eTime)
{
if(hp[r].bNum<hp[l].bNum&&hp[r].bNum<hp[i].bNum)
small=r;
else if(hp[l].bNum<hp[r].bNum&&hp[l].bNum<hp[i].bNum)
small=l;
else
small=i;
}
else
{
if(hp[r].bNum<hp[i].bNum)
small=r;
else
small=i;
}
}
else if(l<n&&hp[l].eTime==hp[i].eTime)
{
if(hp[i].bNum > hp[l].bNum)
small=l;
else
small=i;
}
if(r<n&&hp[r].eTime<hp[small].eTime&&hp[l].eTime<hp[small].eTime)
{ if(hp[l].eTime==hp[r].eTime)
{
if(hp[l].bNum > hp[r].bNum)
small=r;
else
small=l;
}
}
else if(l<n&&hp[l].eTime<hp[small].eTime)
small=l;
else if(r<n&&hp[r].eTime<hp[small].eTime)
small=r;
if(small!=i)
{
swap(hp[small],hp[i]);
hpify(hp,n,small);
}
}
void swap(minHp a, minHp b) //Swaps two MinHeap nodes
{
struct minHp temp;
temp=a;
a=b;
b=temp;
}
int main (int argc, char** argv)
{ outFile.open("output_file.txt",ios::out|ios::trunc); //writes to output file
int ctr=0; //Counter
int prm1=0; //First paramter
int prm2=0; //Second paramter
int psn=0; //Position
int flag=0;
bool b=0;
string func,gcString,prm; //Declaration for function, Globalcounter String and paramter
ifstream inFile; //Input File
inFile.open(argv[1]);
if (!inFile) {
cout << "Failed to open Input File!";
}
else if (inFile)
{
while (getline(inFile, gcString, ':')) //Returns the Paramter passed by the Input
{
istringstream(gcString)>>ctr;
getline(inFile,func, '(');
getline(inFile,prm, ')');
while(gc<ctr)
{
gc=gc+1;
construct();
}
int InsCmpr=func.compare("Insert");
int InsCmpr2=func.compare(" Insert");
int PrntCmpr=func.compare(" PrintBuilding");
int PrntCmpr2=func.compare("PrintBuilding");
if(InsCmpr==0||InsCmpr2==0) //Checks if paramater is compare
{
psn=prm.find(',');
int prmL=prm.length();
istringstream(prm.substr(0,prm.find(',')))>>prm1;
istringstream(prm.substr(psn+1,prmL-1))>>prm2;
RBInsrtNode(prm1,prm2);
Insert(prm1,prm2);
}
else if(PrntCmpr==0||PrntCmpr2==0) ////Checks if paramater is Print
{
psn=prm.find(',');
int prmL=prm.length();
if (psn==std::string::npos)
{
istringstream(prm)>>prm1;
RBPrnt(prm1);
}
else
{
istringstream(prm.substr(0,psn))>>prm1;
istringstream(prm.substr(psn+1,prmL-1))>>prm2;
RBPrnt(prm1,prm2);
}
}
}
while(sz>0)
{
gc=gc+1; //Increases Global counter
construct();
}
inFile.close();
}
// cout<<"\nTime taken to for the city to be constructed "<<ttl<<endl;
outFile.close();
return 0;
}