Skip to content

Commit 32efc87

Browse files
committed
Homework seems to be done
1 parent 1513996 commit 32efc87

File tree

1 file changed

+175
-25
lines changed

1 file changed

+175
-25
lines changed

HW3/main.cpp

Lines changed: 175 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Node::Node(int key, int ordinal) {
2525

2626
class RBTree{
2727
public:
28+
int elementCount;
29+
int redElements;
30+
int blackElements;
2831
Node *root;
2932
// Necessary nodes for delete fix(So we have no memory leak)
3033
Node *replacingNode;
@@ -39,6 +42,7 @@ class RBTree{
3942
void InOrderWalk(Node *node);
4043
Node *Select(Node *begin, int i);
4144
Node* TreeMinimum(Node *begin);
45+
void CountElements(Node *begin);
4246
RBTree();
4347
};
4448

@@ -122,7 +126,7 @@ void RBTree::Insert(Node *node) {
122126
* we call another function called InsertFix that fixes any coloring
123127
* issues.
124128
*/
125-
129+
elementCount += 1;
126130
Node *temp = nullptr;
127131
Node *traverse = this->root;
128132
while (traverse){
@@ -250,14 +254,15 @@ void RBTree::Delete(Node *node) {
250254
* paths that lead to leaves which would violate the red-black tree
251255
* properties.
252256
*/
257+
elementCount -= 1;
253258
Node *temp = node;
254259
Node *replace = nullptr;
255260
bool originalIsRed = temp->isRed;
256261
bool rep = false;
257262

258263
if (node->left == nullptr){
259264
replace = node->right;
260-
Node *traverse = temp;
265+
Node *traverse = temp->parent;
261266
while(traverse){
262267
traverse->size -= 1;
263268
traverse = traverse->parent;
@@ -277,7 +282,7 @@ void RBTree::Delete(Node *node) {
277282
Transplant(node, node->right);
278283

279284
} else if (node->right == nullptr){
280-
Node *traverse = temp;
285+
Node *traverse = temp->parent;
281286
while(traverse){
282287
traverse->size -= 1;
283288
traverse = traverse->parent;
@@ -286,7 +291,7 @@ void RBTree::Delete(Node *node) {
286291
Transplant(node, node->left);
287292
} else {
288293
temp = TreeMinimum(node->right);
289-
Node *traverse = temp;
294+
Node *traverse = temp->parent;
290295
while(traverse){
291296
traverse->size -= 1;
292297
traverse = traverse->parent;
@@ -320,6 +325,19 @@ void RBTree::Delete(Node *node) {
320325

321326
if(!(originalIsRed))
322327
DeleteFix(replace, rep);
328+
329+
if (rep){
330+
rep = false;
331+
if(!replacingNode->parent){
332+
delete replacingNode;
333+
} else if (replacingNode->parent->left == replacingNode){
334+
replacingNode->parent->left = nullptr;
335+
delete replacingNode;
336+
} else if (replacingNode->parent->right == replacingNode) {
337+
replacingNode->parent->right = nullptr;
338+
delete replacingNode;
339+
}
340+
}
323341
}
324342

325343
void RBTree::DeleteFix(Node *node, bool rep) {
@@ -401,25 +419,25 @@ void RBTree::DeleteFix(Node *node, bool rep) {
401419

402420
if (replaceChange){
403421
replaceChange = false;
404-
if (replacingNode->parent->left == replacingNode){
422+
if(!replacingNode->parent){
423+
delete replacingNode;
424+
} else if (replacingNode->parent->left == replacingNode){
405425
replacingNode->parent->left = nullptr;
406-
// replacingNode->parent->size -= 1;
407426
delete replacingNode;
408427
} else if (replacingNode->parent->right == replacingNode) {
409428
replacingNode->parent->right = nullptr;
410-
// replacingNode->parent->size -= 1;
411429
delete replacingNode;
412430
}
413431
}
414432
if (siblingChange){
415433
siblingChange = false;
416-
if (siblingNode->parent->left == siblingNode){
434+
if(!siblingNode->parent){
435+
delete siblingNode;
436+
} else if (siblingNode->parent->left == siblingNode){
417437
siblingNode->parent->left = nullptr;
418-
// siblingNode->parent->size -= 1;
419438
delete siblingNode;
420439
} else if (siblingNode->parent->right == siblingNode){
421440
siblingNode->parent->right = nullptr;
422-
// siblingNode->parent->size -= 1;
423441
delete siblingNode;
424442
}
425443
}
@@ -429,13 +447,13 @@ void RBTree::DeleteFix(Node *node, bool rep) {
429447
}
430448

431449
void RBTree::InOrderWalk(Node *node) {
432-
if (!node) std::cout << "nil" << std::endl;
450+
//if (!node) std::cout << "nil" << std::endl;
433451
if(node != nullptr) {
434452

435-
std::cout << node->key <<", " << node->ordinal << ", " << node->size<< "'s left: ";
453+
//std::cout << node->key <<", " << node->ordinal << ", " << node->size<< "'s left: ";
436454
InOrderWalk(node->left);
437455
std::cout << "(" <<node->key << ", "<< node->ordinal << ", " << node->size << ")\t" << (node->isRed ? "Red" : "Black") << std::endl;
438-
std::cout << node->key <<", " << node->ordinal << ", " << node->size<< "'s right: ";
456+
//std::cout << node->key <<", " << node->ordinal << ", " << node->size<< "'s right: ";
439457
InOrderWalk(node->right);
440458
}
441459
}
@@ -452,10 +470,13 @@ Node *RBTree::TreeMinimum(Node *begin) {
452470

453471
RBTree::RBTree() {
454472
root = replacingNode = siblingNode = nullptr;
473+
elementCount = blackElements = redElements = 0;
474+
455475
}
456476

457477
Node *RBTree::Select(Node *begin, int i) {
458478
int s = 1;
479+
if(!begin) return nullptr;
459480
if (begin->left) s += begin->left->size;
460481
int rank = s;
461482
if (i == rank){
@@ -467,20 +488,149 @@ Node *RBTree::Select(Node *begin, int i) {
467488
}
468489
}
469490

470-
int main() {
471-
RBTree tree;
472-
Node *arr[15] = {new Node(974), new Node(834), new Node(204), new Node(463), new Node(332),
473-
new Node(293), new Node(706), new Node(126), new Node(980), new Node(630),
474-
new Node(718), new Node(760), new Node(76), new Node(607), new Node(805)};
475-
for(int i = 0; i < 15; i++){
476-
tree.Insert(arr[i]);
491+
void RBTree::CountElements(Node *begin) {
492+
if(begin != nullptr) {
493+
CountElements(begin->left);
494+
if (begin->isRed) redElements += 1;
495+
else blackElements += 1;
496+
CountElements(begin->right);
497+
}
498+
}
499+
500+
class Warehouse{
501+
public:
502+
RBTree storage;
503+
Node *arr;
504+
int minOrdinal;
505+
bool accept;
506+
std::vector<Node*> cargo;
507+
Warehouse(int *sizes, int *ordinals, int count);
508+
void sendPackages(int i, bool start=false);
509+
void receivePackages(std::vector<Node*> packages);
510+
void findMinimum();
511+
};
512+
513+
Warehouse::Warehouse(int *sizes, int *ordinals, int count) {
514+
this->arr = new Node[count];
515+
accept = true;
516+
517+
for (int i = 0; i < count; i++){
518+
arr[i].key = sizes[i];
519+
arr[i].ordinal = ordinals[i];
520+
storage.Insert(&arr[i]);
521+
}
522+
}
523+
524+
void Warehouse::findMinimum() {
525+
int minSize = storage.TreeMinimum(storage.root)->key;
526+
while (minSize == storage.TreeMinimum(storage.root)->key){
527+
cargo.push_back(storage.TreeMinimum(storage.root));
528+
storage.Delete(storage.TreeMinimum(storage.root));
529+
}
530+
}
531+
532+
void Warehouse::sendPackages(int i, bool start) {
533+
534+
if (!(storage.Select(storage.root, i))){
535+
findMinimum();
536+
if (!start) accept = false;
537+
} else {
538+
cargo.push_back(storage.Select(storage.root, i));
539+
540+
//storage.InOrderWalk(storage.root);
541+
Node * temp = storage.Select(storage.root, i);
542+
storage.Delete(temp);
543+
//storage.InOrderWalk(storage.root);
477544
}
545+
}
546+
547+
void Warehouse::receivePackages(std::vector<Node*> packages) {
548+
minOrdinal = packages[0]->ordinal;
549+
550+
for(auto it = packages.begin(); it != packages.end(); ++it){
551+
if ((*it)->ordinal < minOrdinal) minOrdinal = (*it)->ordinal;
552+
}
553+
554+
cargo.clear();
555+
sendPackages(minOrdinal);
556+
557+
if (accept){
558+
for(auto it = packages.begin(); it != packages.end(); ++it){
559+
storage.Insert(*it);
560+
}
561+
}
562+
563+
accept = true;
564+
}
565+
566+
HW3_Result hw3( int eastWarehousePackageCount,
567+
int eastWarehousePackageSizes [],
568+
int eastWarehousePackageOrdinals [],
569+
int westWarehousePackageCount,
570+
int westWarehousePackageSizes [],
571+
int westWarehousePackageOrdinals [] ){
572+
573+
Warehouse East(eastWarehousePackageSizes, eastWarehousePackageOrdinals, eastWarehousePackageCount);
574+
Warehouse West(westWarehousePackageSizes, westWarehousePackageOrdinals, westWarehousePackageCount);
575+
576+
East.sendPackages(0, true);
577+
std::vector<Node*> cargo = East.cargo;
578+
579+
int packageCount = 0;
580+
int redNodeCount = 0;
581+
int blackNodeCount = 0;
582+
583+
while(true){
584+
West.receivePackages(cargo);
585+
cargo = West.cargo;
586+
if(West.storage.elementCount == 0) {
587+
for(auto it = cargo.begin(); it != cargo.end(); ++it){
588+
East.storage.Insert(*it);
589+
}
590+
packageCount = East.storage.elementCount;
591+
East.storage.CountElements(East.storage.root);
592+
redNodeCount = East.storage.redElements;
593+
blackNodeCount = East.storage.blackElements;
594+
break;
595+
}
596+
597+
East.receivePackages(cargo);
598+
cargo = East.cargo;
599+
if(East.storage.elementCount == 0) {
600+
for(auto it = cargo.begin(); it != cargo.end(); ++it){
601+
West.storage.Insert(*it);
602+
}
603+
packageCount = West.storage.elementCount;
604+
West.storage.CountElements(West.storage.root);
605+
redNodeCount = West.storage.redElements;
606+
blackNodeCount = West.storage.blackElements;
607+
break;
608+
}
609+
}
610+
611+
HW3_Result result;
612+
result.blackNodeCount = blackNodeCount;
613+
result.redNodeCount = redNodeCount;
614+
result.packageCount = packageCount;
615+
return result;
616+
}
617+
618+
int main() {
619+
int eastWarehousePackageCount = 10;
620+
int eastWarehousePackageSizes[10] = {1, 5, 34, 6, 7, 23, 5, 6, 34, 5};
621+
int eastWarehousePackageOrdinals[10] = {7, 43, 59, 80, 925, 89, 27, 45, 89, 72};
622+
623+
int westWarehousePackageCount = 10;
624+
int westWarehousePackageSizes[10] = {10, 38, 51, 7, 38, 47, 8, 53, 2, 94};
625+
int westWarehousePackageOrdinals[10] = {5, 62, 86, 73, 74, 83, 68, 35, 7, 95};
626+
627+
HW3_Result result = hw3(eastWarehousePackageCount, eastWarehousePackageSizes, eastWarehousePackageOrdinals,
628+
westWarehousePackageCount, westWarehousePackageSizes, westWarehousePackageOrdinals);
629+
478630

479-
// std::cout << tree.Select(tree.root, 5)->key;
480-
tree.Delete(arr[3]);
481-
tree.Delete(arr[1]);
482-
tree.Delete(arr[11]);
483-
tree.InOrderWalk(tree.root);
631+
std::cout << "Package Count: " << result.packageCount << std::endl;
632+
std::cout << "Red Node Count: " << result.redNodeCount << std::endl;
633+
std::cout << "Black Node Count: " << result.blackNodeCount << std::endl;
484634

485635
return 0;
486636
}

0 commit comments

Comments
 (0)