Else condition for Case 3 on Insertion Correction
This commit is contained in:
parent
e7c7132261
commit
ab1af2ead0
Binary file not shown.
Binary file not shown.
BIN
build/libRBlib.a
BIN
build/libRBlib.a
Binary file not shown.
BIN
build/main
BIN
build/main
Binary file not shown.
@ -19,7 +19,6 @@ RB::RB()
|
||||
|
||||
void RB::ins(Node* node, Node* newnode){
|
||||
int key = newnode -> get_key();
|
||||
std::cout << "Inserting " << key << std::endl;
|
||||
if (root == nullptr){
|
||||
root = newnode;
|
||||
newnode -> set_color(black);
|
||||
@ -47,7 +46,6 @@ void RB::ins(Node* node, Node* newnode){
|
||||
Node* p = newnode -> get_parent();
|
||||
Node* gp;
|
||||
|
||||
std::cout << "Inserted, but now correcting" << std::endl;
|
||||
while ((p != nullptr) && (p -> get_color() == red)){
|
||||
gp = p -> get_parent();
|
||||
if (gp == nullptr)
|
||||
@ -71,10 +69,18 @@ void RB::ins(Node* node, Node* newnode){
|
||||
p = newnode -> get_parent();
|
||||
//gp = p -> get_parent();
|
||||
left_rot(newnode);
|
||||
p = newnode -> get_parent();
|
||||
gp = p -> get_parent();
|
||||
}
|
||||
|
||||
//Case 3:
|
||||
else{
|
||||
//Case 3
|
||||
p -> set_color(black);
|
||||
gp -> set_color(red);
|
||||
right_rot(gp);
|
||||
p = newnode -> get_parent();
|
||||
gp = p -> get_parent();
|
||||
}
|
||||
} //end if p = lchild of gp
|
||||
else{
|
||||
@ -96,16 +102,22 @@ void RB::ins(Node* node, Node* newnode){
|
||||
p = newnode -> get_parent();
|
||||
//gp = p -> get_parent();
|
||||
left_rot(newnode);
|
||||
//Case 3
|
||||
}
|
||||
|
||||
//Case 3
|
||||
else{
|
||||
p = newnode -> get_parent();
|
||||
gp = p -> get_parent();
|
||||
p -> set_color(black);
|
||||
gp -> set_color(red);
|
||||
right_rot(gp);
|
||||
p = newnode -> get_parent();
|
||||
gp = p -> get_parent();
|
||||
}
|
||||
}
|
||||
//this -> get_root() -> set_color(black);
|
||||
}
|
||||
this -> get_root() -> set_color(black);
|
||||
std::cout << "Finished correcting" << std::endl;
|
||||
}
|
||||
|
||||
void RB::del(int key){
|
||||
|
||||
@ -14,7 +14,6 @@ int main(int argc, char *argv[])
|
||||
for (int k : init_keys){
|
||||
Node* newnode = new Node(k);
|
||||
tree.ins(tree.get_root(), newnode);
|
||||
tree.display(tree.get_root(), nullptr, false);
|
||||
}
|
||||
|
||||
std::cout << "After insertion" << std::endl;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user