0

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;
       }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Student
  • 195
  • 1
  • 12
  • Hint: Always delete at the leaf level. – ChiefTwoPencils Nov 24 '13 at 01:19
  • are you referring to shifting all the nodes up, so that the node to delete will be at the bottom? – Student Nov 24 '13 at 01:57
  • No, the point is that you shouldn't *have* to shuffle anything. I could have been more clear, it would be a leaf in terms of the largest or smallest, but it's not easily explained. [The link supplied by strigon33](http://en.wikipedia.org/wiki/AVL_tree#Deletion) does a better job. – ChiefTwoPencils Nov 24 '13 at 02:05
  • Ah ok then. Would that method ruin the balance of the tree? – Student Nov 24 '13 at 02:46
  • It could. Read the instructions one by one - "7.Retrace the path back up the tree (starting with node Z's parent) to the root, adjusting the balance factors as needed." – ChiefTwoPencils Nov 24 '13 at 03:18

1 Answers1

0

The internet has many examples of such algorithms explained abstractly.

Specific help is hard to provide without source code since I have no idea what you called your methods.

HonestHeuristic
  • 336
  • 2
  • 14