I'm really stuck on how to implement an deletion method for an avltree in C++. I'm not really sure where to begin. I have methods to find a node, and rotation methods, and insertion working but just really unsure on how to start this deletion method. I know that this is a vague, and there are resources online, but I don't find them very clear. Can someone please explain the process to be please?
If there is any code that you would like to see, please ask :)
Any and all advice would be greatly appreciated. Many thanks in advance!
----- EDIT -----
Find node method:
Node* AVLtree::findNode(string cityID){
Node *thisNode = this->rootNode;
while(thisNode!=0){
int location = thisNode->getCity()->getName().compare(cityID);
if(location==0){return thisNode;
}else if(location<0){//cout<<"NODE: " << thisNode->getCity()->getName()<<endl;
thisNode = thisNode->getChildR();
}else{thisNode = thisNode->getChildL();}
}
return thisNode;
}
Rotate left:
Node* AVLtree::rotateLeft(Node* node){
Node* tempParent= node->getParent();
Node* tempNode = node->getChildL();
node->setChildL(tempNode->getChildR());
tempNode->setChildR(node);
if(tempParent==0){tempNode->removeParent();this->rootNode=tempNode;tempNode->getNewHeight();}
else{tempParent->setChildR(tempNode);
}
return tempNode;
}
Rotate right:
Node* AVLtree::rotateRight(Node* node){
Node* tempParent = node->getParent();
Node* tempNode = node->getChildR();
node->setChildR(tempNode->getChildL());
tempNode->setChildL(node);
if(tempParent==0){tempNode->removeParent();this->rootNode=tempNode;tempNode->getNewHeight();}
else{tempParent->setChildR(tempNode);}
return tempNode;
}
Double rotation, for when inserting into the left subtree of the right child of the unbalance node:
Node* AVLtree::rotateTwoRights(Node* node){
node = rotateLeft(node->getChildR());
node = rotateRight(node->getParent());
return node;
}
Double rotation for right,left condition:
Node* AVLtree::rotateTwoLefts(Node* node){
node = rotateRight(node->getChildL());
node = rotateLeft(node->getParent());
return node;
}
Insert method:
Node* AVLtree::insertNode(Node* parent, Node* node, City *city, int side){
if(node == 0){
if(side==0){
node = parent->setChildL(new Node(city));
}else if(side==1){
node = parent->setChildR(new Node(city));
}
}else if(node->getCity()->getName().compare(city->getName())<0){ //Right case
parent = node;
node = insertNode(parent, node->getChildR(), city, 1);
parent->getNewHeight();
if(parent->getBalance()==2){
if(parent->getChildR()->getCity()->getName().compare(city->getName())<0){
node = rotateRight(parent);
}else{
node = rotateTwoRights(parent);
}
}
}else if(node->getCity()->getName().compare(city->getName())>0){ //Left case
parent = node;
node = insertNode(parent, node->getChildL(), city, 0);
parent->getNewHeight();
if(parent->getBalance()==-2){
if(parent->getChildL()->getCity()->getName().compare(city->getName())>0){
node = rotateLeft(parent);
}else{
node = rotateTwoLefts(parent);
}
}
}else{
node=0;
}
return node;
}