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::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; }
//delete the entry void SplayTree::cut(string aKey){ cout<<"<<<------internal transcript----->>>"<<endl; BSTNode* node = BST::find(aKey); //the node contains the exact key if((node->key()!= aKey) || node ==NULL ){ splay(node); //splay it anyway [2] cout<<"\nsorry, the musician: "<<aKey<<" is not in the list"<<endl; return; } if(node->parent() != NULL && (node->key() ==aKey)){ cout<<"node: "<<node->key()<<" has parent"<<endl; node = node->parent(); //node now pting to its parent cout<<"node now pting to its parent: "<<node->key()<<endl; BST::remove(aKey); //remove the key splay(node); //splay the parent to the root [2] }else if (node->parent() ==NULL){ //in the case node is the root BST::remove(aKey); } cout<<"delete sucessful!"<<endl; }