static uint64_t RemoveRecursive(const uint256& key, const unsigned int level, WeightedTrieNode*& node) { bool deleteNode = false; uint64_t deletedWeight = 0; if (node == NULL) { return 0; // not found } if (node->IsLeafNode()) { // Given a key, the path through the trie is deterministic. // Thus if we end up at a leaf node, this is the only place where // the key to remove could possibly be; there is no need to check // any other branches. WeightedTrieLeafNode* leafNode = (WeightedTrieLeafNode*)node; if (key == leafNode->key) { deleteNode = true; deletedWeight = node->weight; } } else { WeightedTrieNonLeafNode* nonLeafNode = (WeightedTrieNonLeafNode*)node; deletedWeight = RemoveRecursive(key, level + 1, nonLeafNode->SelectChildFromKey(key, level)); node->weight -= deletedWeight; if (node->weight == 0) { // Non-leaf node is now empty; delete it deleteNode = true; } } if (deleteNode) { delete node; // Set node to NULL to signify to other functions that the child no // longer exists. node = NULL; } return deletedWeight; }
void RemoveRecursive(const QString& path) { QDir dir(path); foreach (const QString& child, dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Hidden)) RemoveRecursive(path + "/" + child); foreach (const QString& child, dir.entryList(QDir::NoDotAndDotDot | QDir::Files | QDir::Hidden)) QFile::remove(path + "/" + child); dir.rmdir(path); }
void RemoveRecursive(Node* sub, K k) { if(k < sub->key) this->RemoveRecursive(sub->left, k); else if(k > sub->key) this->RemoveRecursive(sub->right, k); else { // delete it if(sub->left != nullptr && sub->right != nullptr) { // we have two children Node* next = 0; if(this->removedsuccessor) next = this->Minimum(sub->right); else next = this->Minimum(sub->left); this->removedsuccessor = !this->removedsuccessor; sub->key = next->key; sub->value = next->value; RemoveRecursive(next, next->key); } else if(sub->left != nullptr) { // only left child this->ReplaceNode(sub, sub->left); } else if(sub->right != nullptr) { // only right child this->ReplaceNode(sub, sub->right); } else { // no children this->ReplaceNode(sub, nullptr); } } }
bool RemoveRecursive(const QString& path) { QDir dir(path); for (const QString& child : dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Hidden)) { if (!RemoveRecursive(path + "/" + child)) return false; } for (const QString& child : dir.entryList(QDir::NoDotAndDotDot | QDir::Files | QDir::Hidden)) { if (!QFile::remove(path + "/" + child)) return false; } if (!dir.rmdir(path)) return false; return true; }
/** * Recursively look for `ItemToRemove` in `RemoveFrom` and any of the descendants, and remove `ItemToRemove`. * @return true when successful. */ static bool RemoveRecursive(TArray< TSharedPtr< FTestData > >& RemoveFrom, const TSharedPtr<FTestData>& ItemToRemove) { int32 ItemIndex = RemoveFrom.Find(ItemToRemove); if (ItemIndex != INDEX_NONE) { RemoveFrom.RemoveAt(ItemIndex); return true; } // Did not successfully remove an item. Try all the children. for (ItemIndex = 0; ItemIndex < RemoveFrom.Num(); ++ItemIndex) { if (RemoveRecursive(RemoveFrom[ItemIndex]->Children, ItemToRemove)) { return true; } } return false; }
bool WeightedTrie::Remove(const uint256& key) { return RemoveRecursive(key, 0, this->root) != 0; }