| | | | | |

Threaded Rotations

Non-threaded code:

  Node * RotateLeft(Node * n)
  {
    if (0 == n || 0 == n->rchild_)
      return n;
    Node * p = n->rchild_;
    n->rchild_ = p->lchild_;
    p->lchild_ = n;
    return p;
  }

Threaded Code:

  Node * RotateLeft(Node * n)
  {
    // Require(n->HasRightChild() && n->rchild_->IsRed());
    if (nullptr == n || nullptr == n->rchild_ || n->IsRightThreaded()) return n;
    if (!(n->rchild_->IsRed()))
    {
      std::cerr << " ** RotateLeft called with black right child\n";
      return n;
    }
    Node * p = n->rchild_;
    if (p->HasLeftChild())
      n->SetRightChild(p->lchild_);
    else
      n->SetRightThread(p);
    p->SetLeftChild(n);
    n->IsRed()? p->SetRed() : p->SetBlack();
    n->SetRed();
    return p;
  }

| | Top of Page | 14. BST Iterators - 32 of 41