[Glacier] Implement delete for binary tree.
This commit is contained in:
parent
a16dcc2aa9
commit
9ba26195d2
|
@ -68,8 +68,40 @@ void BinaryTree<K, V>::Insert(K key, V&& value) {
|
||||||
|
|
||||||
template <typename K, typename V>
|
template <typename K, typename V>
|
||||||
void BinaryTree<K, V>::Delete(K key) {
|
void BinaryTree<K, V>::Delete(K key) {
|
||||||
// TODO: Implement Delete.
|
auto node = FindOrInsertionParent(key);
|
||||||
return;
|
if (node.empty() || node->key != key) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPtr<BinaryNode> new_child = nullptr;
|
||||||
|
if (!node.left) {
|
||||||
|
// No children.
|
||||||
|
// Right child only.
|
||||||
|
new_child = node.right;
|
||||||
|
} else if (!node.right) {
|
||||||
|
// Left child only.
|
||||||
|
new_child = node.left;
|
||||||
|
} else {
|
||||||
|
// Find Successor.
|
||||||
|
auto successor = node.right;
|
||||||
|
while (successor.left) {
|
||||||
|
successor = successor.left;
|
||||||
|
}
|
||||||
|
new_child = successor;
|
||||||
|
if (successor != node.right) {
|
||||||
|
successor.parent.left = successor.right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node == root_) {
|
||||||
|
root_ = new_child;
|
||||||
|
} else {
|
||||||
|
if (node.parent.right == node) {
|
||||||
|
node.parent.right = new_child;
|
||||||
|
} else {
|
||||||
|
node.parent.left = new_child;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename K, typename V>
|
template <typename K, typename V>
|
||||||
|
|
Loading…
Reference in New Issue