c++ - Splay Tree Zig-Zag & Zag-Zig 旋转问题

标签 c++ data-structures binary-tree splay-tree

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

这是我的 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);
            }
        }
        ////--------------------------------------------------------------------//-------////
    }
}

Zig(单向左旋转)和Zag(单向右旋转)方法如下

template <class WA>
void SplayTree<WA>::Zig(SplayNODE<WA> * & k2)   //k2 is temp node where imbalance is present & we have to rotate it
{
    SplayNODE<WA> *k1 = k2->left;   //create copy of temp's left & store it in k1
    k2->left = k1->right;   //make k1's right child as temp's left child
    k1->right = k2; //make k1 as parent of temp node    
    k2 = k1;    //assign k1 as new temp node (this is done because temp was passed by reference)
}
//==============================//==============================//
template <class WA>
void SplayTree<WA>::Zag(SplayNODE<WA> * & k2)
{
    SplayNODE<WA> *k1 = k2->right;  //create copy of temp's right child & store it in k1
    k2->right = k1->left;   //store k1's left child as temp's right child
    k1->left = k2;  //make k2 as left child of k1
    k2 = k1;    //assign k1 as temp node (due to reference of k2)
}
//==============================//==============================//

& 这是我该死的问题。 首先,我只在搜索功能中展开。即,如果找到具有特定 key 的节点,则展开。

我的搜索功能:

template <class WA>
SplayNODE<WA>* SplayTree<WA>::search(SplayNODE<WA> *temp, WA value) /////PVT DEFINITION
{
    SplayNODE<WA> *to_searched = 0; //created new node pointer in which address of required node will be saved
    if (temp!=0)    //if arg. given temp node is not empty, then
    {
        if (temp->key==value)   //if temp node has user-specified value then
        {   
            to_searched = temp;     //assign address of temp to to_searched node, which will be return @ the end
            Do_Splay(temp); //perform splay at node which is found
        }   
        if (to_searched==0) //if node is still empt then
        {   to_searched = search(temp->left, value);    }   //recursive call to search in left sub-tree of temps
        if (to_searched==0) //if node is still empt then
        {   to_searched = search(temp->right, value);   }   //recursive call to search in right sub-tree of temps
    }
    return to_searched; //Finally return the searched node
}
//==============================//==============================//

如果节点是根的子节点,则以下工作正常。 - 之字形单 - 锯齿形单 但是一旦我们关闭一个节点,问题就开始了。我无法成功执行任何这些事情。 之字字 - 之字形 - 之字形 - 之字形

这是示例树。 (Zig Zig 大小写)

        20
       /  \
     10   25
    /  \
   5   15

当搜索到 5 时,答案应该是。

 5
  \
  10
    \
    20
   /  \
  15  25

但是我的输出变成了:

   20
  /  \
15   25

无法弄清楚是什么问题。干运行这个东西 100 次但是。 :( 感谢所有帮助。提前致谢

最佳答案

在算法更改和尝试...

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

{“X 是左右孙子吗?”} ---是---> {左旋转关于 p,右旋转关于 g}

关于c++ - Splay Tree Zig-Zag & Zag-Zig 旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14042446/

相关文章:

c++ - 删除 C++ 中的枚举可见性

algorithm - 高斯分布+哈希表

c - 将字符串插入二叉搜索树 C

javascript - 计算二叉树 Javascript 的高度/深度

c++ - 逆时针旋转阵列,运行时间问题

c - 二叉树 - 打印叶到叶路径 [C]

c++ - 在 Linux 上编程 C++ 与在 Windows 中的 Linux VM 上编程 C++

c++ - 在参数中使用模板<>

c++ - QNetworkReply "unable to write"

java - 具有不断变化的键值的优先级队列