diff --git a/build/CMakeFiles/RBlib.dir/src/Red-Black.cpp.o b/build/CMakeFiles/RBlib.dir/src/Red-Black.cpp.o index b5d0f72..b4372e2 100644 Binary files a/build/CMakeFiles/RBlib.dir/src/Red-Black.cpp.o and b/build/CMakeFiles/RBlib.dir/src/Red-Black.cpp.o differ diff --git a/build/CMakeFiles/main.dir/src/main.cpp.o b/build/CMakeFiles/main.dir/src/main.cpp.o index fd56bd5..dab0bb3 100644 Binary files a/build/CMakeFiles/main.dir/src/main.cpp.o and b/build/CMakeFiles/main.dir/src/main.cpp.o differ diff --git a/build/libRBlib.a b/build/libRBlib.a index f6f0fcf..4e383a0 100644 Binary files a/build/libRBlib.a and b/build/libRBlib.a differ diff --git a/build/main b/build/main index 4b9f9ed..22e00fa 100755 Binary files a/build/main and b/build/main differ diff --git a/src/Red-Black.cpp b/src/Red-Black.cpp index e93b635..8424da7 100644 --- a/src/Red-Black.cpp +++ b/src/Red-Black.cpp @@ -126,11 +126,13 @@ void RB::del(Node* node, int key){ if (node -> get_lchild() == nullptr && node -> get_rchild() == nullptr){ if (node -> get_parent() -> get_lchild() == node){ Node* p = node -> get_parent(); + node -> set_parent(nullptr); p -> set_lchild(nullptr); node = p; } else{ Node* p = node -> get_parent(); + node -> set_parent(nullptr); p -> set_rchild(nullptr); node = p; } @@ -138,36 +140,41 @@ void RB::del(Node* node, int key){ //Case 2: No rchild else if (node -> get_lchild() != nullptr && node -> get_rchild() == nullptr){ Node* p = node -> get_parent(); - if (p -> get_lchild() == node){ - node = node -> get_lchild(); - p -> set_lchild(node); - node -> set_parent(p); + Node* lchild = node -> get_lchild(); + node -> set_parent(nullptr); + node -> set_lchild(nullptr); + if (p -> get_lchild() == node){ + p -> set_lchild(lchild); } else{ - node = node -> get_lchild(); - p -> set_rchild(node); - node -> set_parent(p); + p -> set_rchild(lchild); } + lchild -> set_parent(p); + node = lchild; } //Case 3: Get successor else{ - Node* rtree = node -> get_rchild(); Node* p = node -> get_parent(); Node* l = node -> get_lchild(); Node* r = node -> get_rchild(); - bool left = (p -> get_lchild() == node); + + node = r; while(node -> get_lchild() != nullptr){ node = node -> get_lchild(); } if (node -> get_rchild() != nullptr){ - node -> get_parent() -> set_lchild(node -> get_rchild()); - } node -> get_rchild() -> set_parent(node -> get_parent()); - + if(node != r){ + node -> get_parent() -> set_lchild(node -> get_rchild()); + node -> get_rchild() -> set_parent(node -> get_parent()); + } + } + left ? p -> set_lchild(node) : p -> set_rchild(node); node -> set_parent(p); node -> set_lchild(l); - node -> set_rchild(r); + if(node != r) + node -> set_rchild(r); } } else{ diff --git a/src/main.cpp b/src/main.cpp index 602a77c..355b3af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,7 +49,7 @@ int main(int argc, char *argv[]) std::cout << "After insertion:" << std::endl; tree2.display(tree2.get_root(), nullptr, false); std::cout << "" << std::endl; - /* + std::cout << "Deleting 127:" << std::endl; tree2.del(tree2.get_root(), 127); tree2.display(tree2.get_root(), nullptr, false); @@ -59,5 +59,4 @@ int main(int argc, char *argv[]) tree2.del(tree2.get_root(), 221); tree2.display(tree2.get_root(), nullptr, false); std::cout << "" << std::endl; - */ } \ No newline at end of file