0001 template <typename T> bool Splay<T>::remove(const T& e) { 0002 search(e); //首先做一次查找 0003 if (!_root || e != _root->data) return false; //若成功,则命中节点必已伸展至根 0004 BNP<T> L = _root->lc, R = _root->rc; delete _root; _size--; //记下子树L、R后,释放之 0005 if (!R) { 0006 if (_root = L) //若R空,则 0007 _root->parent = nullptr; //L即是余树 0008 } else { //否则,在R中再次查找e:尽管这次查找必然失败... 0009 (_root = R)->parent = nullptr; search(e); //...但R的最小节点会伸展为根,且无左孩子 0010 _root->attachLc(L); _root->updateHeight(); //故只需令L作为其左子树,便成一棵完整的树 0011 } //else 0012 return true; 0013 } //remove