void SplayTree::zigzigzigzag(BSTNode* aNode){ BSTNode* parent = aNode->parent(); BSTNode* grandParent = parent->parent(); if(parent ==NULL || grandParent==NULL){ return; } if(grandParent->right() == parent && parent->left() ==aNode){ cout<<"performing zigLRzagRL "<<endl; zigLRzagRL( aNode, parent, grandParent); return; } else if(grandParent->left() == parent && parent->right() ==aNode){ cout<<"performing zagRLzigLR "<<endl; zagRLzigLR ( aNode, parent, grandParent); return; } else if(grandParent->left() == parent && parent->left() ==aNode){ cout<<"performing zigLRzigLR "<<endl; zigLRzigLR( aNode, parent, grandParent); return; } else if(grandParent->right() == parent && parent->right() ==aNode){ cout<<"performing zigRLzigRL "<<endl; zigRLzigRL ( aNode, parent, grandParent); return; } else cout<<"sum sing wong: "<<aNode->key()<<"'s parent: "<<parent->key()<<" grandparent: "<<grandParent->key()<<endl; return; }
void SplayTree::splay(BSTNode* aNode){ if(aNode ==NULL || aNode->parent() ==NULL){ //two special cases return; } BSTNode* parent = aNode->parent(); BSTNode* grandParent = parent->parent(); cout<<"splaying the node: "<<aNode->key()<<endl; while(parent != NULL && grandParent != NULL){ //all zigzig zigzag zigzigzigzag(aNode); parent = aNode->parent(); //updating parent and grandparent if(parent ==NULL){ break; //effectively end the function } grandParent = parent->parent(); if(grandParent == NULL){ break; } } if(grandParent == NULL && parent != NULL){ //parent is the root if(parent->left() ==aNode){ zigLR(aNode, parent); }else{ zigRL(aNode, parent); } } debug_print(); //finly, dump the tree structe in Graphviz format }
void SplayTree::zigRLzigRL(BSTNode* aNode, BSTNode* aParent, BSTNode* aGrandParent){ BSTNode* ggParent = aGrandParent->parent(); SplayTree::Orientation orient = SplayTree::NONE; if(ggParent !=NULL){ if(ggParent->left() == aGrandParent){ orient = SplayTree::LEFT; }else{ orient = SplayTree::RIGHT; } } zigRL(aParent, aGrandParent); //rotate right to left around grandparent first zigRL(aNode, aParent); if(orient ==SplayTree::LEFT){ ggParent->setLeft(aNode); aNode->setParent(ggParent); }else if(orient == SplayTree::RIGHT){ ggParent->setRight(aNode); aNode->setParent(ggParent); } // debug_print(); }
void SplayTree::zigLRzagRL(BSTNode* aNode, BSTNode* aParent, BSTNode* aGrandParent){ BSTNode* ggParent = aGrandParent->parent(); SplayTree::Orientation orient = SplayTree::NONE; if(ggParent!=NULL){ if(ggParent->left() == aGrandParent){ //in the case of left subtree orient = SplayTree::LEFT; }else{ orient = SplayTree::RIGHT; } } zigLR(aNode, aParent); zigRL(aNode, aGrandParent); if(orient ==SplayTree::LEFT){ ggParent->setLeft(aNode); aNode->setParent(ggParent); }else if(orient == SplayTree::RIGHT){ ggParent->setRight(aNode); aNode->setParent(ggParent); } debug_print(); }
void SplayTree::zagRLzigLR(BSTNode* aNode, BSTNode* aParent, BSTNode* aGrandParent){ // aNode is a right child of a left child BSTNode* ggParent = aGrandParent->parent(); SplayTree::Orientation orient = SplayTree::NONE; if(ggParent !=NULL){ if(ggParent->left() == aGrandParent){ orient = SplayTree::LEFT; }else{ orient = SplayTree::RIGHT; } } zigRL(aNode, aParent); //rotate from right to left around parent zigLR(aNode, aGrandParent); if(orient ==SplayTree::LEFT){ ggParent->setLeft(aNode); aNode->setParent(ggParent); }else if(orient == SplayTree::RIGHT){ ggParent->setRight(aNode); aNode->setParent(ggParent); } // debug_print(); }