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