Giả sử chúng ta có một cây tìm kiếm nhị phân. Chúng tôi sẽ lấy một khóa k và chúng tôi phải xóa khóa k đã cho khỏi BST và trả lại BST đã cập nhật. Vì vậy, nếu cây như -
Và khóa k =3, thì cây đầu ra sẽ là -
Để giải quyết vấn đề này, chúng tôi sẽ làm theo các bước sau -
-
Xác định một phương thức có tên deleteRoot () để xóa nút gốc, điều này sẽ hoạt động như sau
-
nếu root là null, thì trả về null
-
nếu gốc không có cây con bên phải, thì trả về bên trái của gốc
-
x:=inorder kế nhiệm của root
-
đặt bên trái của x as left:=left of root
-
trả về quyền root
-
Phương pháp xóa sẽ giống như
-
nếu root là null hoặc giá trị của root là key, thì trả về deleteRoot (root)
-
curr:=root
-
Tạo một vòng lặp vô hạn và thực hiện như sau
-
x:=giá trị của nút curr
-
phím if
-
nếu bên trái của curr =null hoặc giá trị bên trái của khóa curr =thì
-
left of curr:=deleteRoot (left of curr) và ra khỏi vòng lặp.
-
-
curr:=left of curr
-
-
nếu không thì
-
nếu bên phải của curr =null hoặc giá trị của bên phải của curr =key, thì
-
right of curr:=deleteRoot (bên phải của curr) và đi ra khỏi vòng lặp.
-
-
curr:=right of curr
-
-
-
trả về thư mục gốc
Hãy cùng chúng tôi xem cách triển khai sau để hiểu rõ hơn -
Ví dụ
#include <bits/stdc++.h>
using namespace std;
class TreeNode{
public:
int val;
TreeNode *left, *right;
TreeNode(int data){
val = data;
left = NULL;
right = NULL;
}
};
void insert(TreeNode **root, int val){
queue<TreeNode*> q;
q.push(*root);
while(q.size()){
TreeNode *temp = q.front();
q.pop();
if(!temp->left){
if(val != NULL)
temp->left = new TreeNode(val);
else
temp->left = new TreeNode(0);
return;
} else {
q.push(temp->left);
}
if(!temp->right){
if(val != NULL)
temp->right = new TreeNode(val);
else
temp->right = new TreeNode(0);
return;
} else {
q.push(temp->right);
}
}
}
TreeNode *make_tree(vector<int> v){
TreeNode *root = new TreeNode(v[0]);
for(int i = 1; i<v.size(); i++){
insert(&root, v[i]);
}
return root;
}
void tree_level_trav(TreeNode*root){
if (root == NULL) return;
cout << "[";
queue<TreeNode *> q;
TreeNode *curr;
q.push(root);
q.push(NULL);
while (q.size() > 1) {
curr = q.front();
q.pop();
if (curr == NULL){
q.push(NULL);
}
else {
if(curr->left)
q.push(curr->left);
if(curr->right)
q.push(curr->right);
if(curr == NULL || curr->val == 0){
cout << "null" << ", ";
} else {
cout << curr->val << ", ";
}
}
}
cout << "]"<<endl;
}
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root == NULL || root->val == key) return deleteRoot(root);
TreeNode* curr = root;
while(1) {
int x = curr->val;
if(key < x){
if(curr->left == NULL || curr->left->val == key){
curr->left = deleteRoot(curr->left);
break;
}
curr = curr->left;
} else {
if(curr->right == NULL || curr->right->val == key){
curr->right = deleteRoot(curr->right);
break;
}
curr = curr->right;
}
}
return root;
}
TreeNode* deleteRoot(TreeNode* root){
if(!root || root->val == 0)return NULL;
if(root->right == NULL) return root->left;
TreeNode* x = root->right;
while(x->left)x = x->left;
x->left = root->left;
return root->right;
}
};
main(){
vector<int> v = {5,3,6,2,4,NULL,7};
TreeNode *root = make_tree(v);
Solution ob;
tree_level_trav(ob.deleteNode(root, 3));
} Đầu vào
[5,3,6,2,4,null,7] 3
Đầu ra
[5, 4, 6, 2, null, 7, ]