首页 > 其他 > 详细

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

时间:2015-03-07 21:14:29      阅读:242      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!