textfix.cpp
来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 39 行
CPP
39 行
template <class Record>
Error_code Search_tree<Record>::remove_root(Binary_node<Record>
*&sub_root)
/*
Pre: sub_root is either NULL or points to a subtree of
the Search_tree.
Post: If sub_root is NULL, a code of not_present is returned.
Otherwise, the root of the subtree is removed in such a way
that the properties of a binary search tree are preserved.
The parameter sub_root is reset as
the root of the modified subtree, and
success is returned.
*/
{
if (sub_root == NULL) return not_present;
Binary_node<Record> *to_delete = sub_root; // Remember node to delete at end.
if (sub_root->right == NULL) sub_root = sub_root->left;
else if (sub_root->left == NULL) sub_root = sub_root->right;
else { // Neither subtree is empty.
to_delete = sub_root->left; // Move left to find predecessor.
Binary_node<Record> *parent = sub_root; // parent of to_delete
while (to_delete->right != NULL) { // to_delete is not the predecessor.
parent = to_delete;
to_delete = to_delete->right;
}
sub_root->data = to_delete->data; // Move from to_delete to root.
if (parent == sub_root) sub_root->left = to_delete->left;
else parent->right = to_delete->left;
}
delete to_delete; // Remove to_delete from tree.
return success;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?