C++ 八字树之字形&;Z字形旋转问题

C++ 八字树之字形&;Z字形旋转问题,c++,data-structures,binary-tree,splay-tree,C++,Data Structures,Binary Tree,Splay Tree,好的,这里是splay算法,如果你想检查的话。 这是我的splay函数: template <class WA> void SplayTree<WA>::Do_Splay(SplayNODE<WA> *temp) //temp is the node which is to be splayed { if (temp==root) //if temp is root then { return; } //Do Nothin

好的,这里是splay算法,如果你想检查的话。

这是我的splay函数:

template <class WA>
void SplayTree<WA>::Do_Splay(SplayNODE<WA> *temp)   //temp is the node which is to be splayed
{
    if (temp==root) //if temp is root then
    {   return;     }   //Do Nothing

    else if (root->left==temp || root->right==temp) //else if temp is a child of root
    {
        if (root->left==temp)   //if temp is left child of root then
        {
            Zig(root);  //perform ZIG
        }
        else if (root->right==temp) //else if temp is right child of root then
        {
            Zag(root);  //perform ZAG
        }
    }
    else    //if temp is some node lower in tree then
    {
        SplayNODE<WA> *father = findfather(temp, root);
        SplayNODE<WA> *grandfather = findfather(father, root);
        //cout<<"\n\tf = "<<father->key<<"\tgf = "<<grandfather->key;
        ////--------------------------------------------------------------------//-------////
        if ( grandfather->left == father)   //if father itself is left child
        {
            if(father->left == temp)    //if temp is left child of father then
            {       //CASE = ZIG ZIG
                cout<<"\tZig(father)---Zag(grandfather)";
                Zig(grandfather);   
                Zig(father);        
            }

            else if ( father->right == temp )   //if temp is right child of father then
            {       //CASE = ZIG ZAG
                cout<<"\tZig(father)---Zag(grandfather)";
                Zig(father);    
                Zag(grandfather);
            }
        }
        //--------------------------------------------------------------------//-------////
        if (grandfather->right == father)   //if father itself is right child
        {
            if(father->right == temp) 
            {       //CASE = ZAG ZAG
                cout<<"\tZag(grandfather)---Zag(father)";
                Zag(grandfather);
                Zag(father);
            }
            else if (father->left == temp)
            {       //CASE = ZAG ZIG
                cout<<"\tZag(father)---Zig(grandfather)";
                Zag(father);
                Zig(grandfather);
            }
        }
        ////--------------------------------------------------------------------//-------////
    }
}
搜索5时,答案应为

 5
  \
  10
    \
    20
   /  \
  15  25
但我的输出变成:

   20
  /  \
15   25
我想不出是什么问题。试运行这东西100次,但是:(
感谢所有帮助。请提前感谢更改算法并尝试

{“X是一个左右的孙子吗?”}--是的-->{右旋转p,左旋转g}

{“X是一个左右的孙子吗?”}--是的-->{左绕p旋转,右绕g旋转}

        20
       /  \
     10   25
    /  \
   5   15
 5
  \
  10
    \
    20
   /  \
  15  25
   20
  /  \
15   25