bool _Remove(BSTNode<K,V>*& root, const K& key) { if (root) { if(root->_key > key) { return _Remove(root->_left, key); } else if(root->_key < key) { return _Remove(root->_right, key); } else { // 1.如果左or右子树为空,则用一个子树进行填补。 // 2.如果左右均不为空,则使用右子树中序的第一个节点填补 if (root->_left == NULL) { BSTNode<K,V>* del = root; root = root->_right; delete del; } else if(root->_right == NULL) { BSTNode<K,V>* del = root; root = root->_left; delete del; } else { // 查找右子树的中序遍历的第一个节点 BSTNode<K,V>* right = root->_right; while (right->_left) { right = right->_left; } swap(root->_value, right->_value); swap(root->_key, right->_key); _Remove(root->_right, right->_key); } return true; } } return false; }
bool SdfNamespaceEdit_Namespace::Apply( const SdfNamespaceEdit& edit, std::string* whyNot) { // If newPath is empty we want to remove the object. if (edit.newPath.IsEmpty()) { return _Remove(edit.currentPath, whyNot); } else if (edit.currentPath != edit.newPath) { return _Move(edit.currentPath, edit.newPath, whyNot); } else { // Reorder -- Ignore the reorder in our virtual namespace. return true; } }
bool Remove_R(const K& key) { return _Remove(_root, key); }
int remove(const char *name) { return _Remove(name); }