码迷,mamicode.com
首页 > 其他好文 > 详细

二叉搜索树中两个节点的旋转

时间:2015-03-07 21:15:36      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

struct TreeNode
{
   //...
    PTreeNode& Child (Direction dir)
    {
        return dir == left? leftChild : rightChild;
    }
};

class BST
{
private:
  // ...
  void      Routate (PTreeNode, Direction);
public:
  // ...
  void      LeftRoutate   (PTreeNode);
   void      RightRoutate  (PTreeNode);
}


void BST::Routate (PTreeNode node, Direction dir)
{
    auto childDir = dir == TreeNode::left
                  ? TreeNode::right
                  : TreeNode::left;

    auto child = node->Child (childDir);
    node->Child (childDir) = child->Child (dir);
    child->Child (dir)->parent = node;
    TransferParent (node, child);
    child->Child (dir) = node;
    node->parent = child;
}

void BST::LeftRoutate (PTreeNode node)
{
    Routate (node, TreeNode::left);
}

void BST::RightRoutate (PTreeNode node)
{
    Routate (node, TreeNode::right);
}

 

二叉搜索树中两个节点的旋转

标签:

原文地址:http://www.cnblogs.com/wuOverflow/p/4320824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!