I have implemented a Binary Search tree in C++.
For the delete method, everything works except for the last case, when the only tree is the parent and it points to two null children.
Now the problem is:
I wish to print out what the left and right subtrees to the parent equal to after I have deleted the subtree. They both as well as the parent should be NULL but when I try to output these values I get a STATUS_ACCESS _VIOLATION.
Here is the code for delete in question.
I wish to delete the parent node, and set the tree->left = tree->right = NULL.
void BST_delete(string key, BST & tree) {
if ((!BST_has(key, tree) || BST_isEmpty(tree))) {
return;
} else if (tree->key == key) {
if (tree->right == NULL && tree->left == NULL) {
tree = NULL; // <-- THIS IS IN QUESTION
} else if (tree->left == NULL) {
...
} ....
}
MAIN:
int main() {
BST bst;
BST_init(bst);
BST_insert("a",bst);
BST_print(bst);
cout << endl;
BST_delete("a",bst);
BST_print(bst); // <-- doesnt print anything (which is right)
cout << bst->right; //<-- Gives me error
return 0;
}
bst = NULL), why do you still want to dobst->right? - A Lan