ツリー内の2つのノードの回転

2231 ワード

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); }