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

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);
}
原文地址:https://www.cnblogs.com/wuOverflow/p/4320824.html