Question: Remove in a binary search tree in C++ Having trouble implementing this function. Not sure what I did wrong but would appreciate help. Thank you.
Remove in a binary search tree in C++
Having trouble implementing this function. Not sure what I did wrong but would appreciate help. Thank you.
Code:


#include using namespace std; / * The data structure of a binary search tree node struct BinaryTreeNode { int key BinaryTreeNode* left = 0; BinaryTreeNode* right = 0; }; //Initialized to NULL //Initialized to NULL @functionality :- If tree is NULL then we do nothing. Otherwise, we compare paramater key with the key of the tree. If the parameter key is less than the key of the tree, we continue to remove on the left sub-tree. However, if it greater than the key of the tree, it will continue to remove on the right sub-tree. If they are equal, the node from the binary search tree will be removed. Now, if the node to be removed has left and right children, the successors key will be copied to the node and the successor node will be removed from the right sub-tree. However, if the node has either of right or left childern, it will point to that child and release the memory of the subject node. Lastly, if the node subject that would be removed does not have any children, the memory will be released from the subject node and a NULL will be assigned to its pointer. void remove (BinaryTreeNode*s root, const int key) { if (root == NULL) { return; 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 -0 if (key root->key) { remove (root->left, key); } else if (key > root->key) { remove (root->right, key); } else if ((root->left != NULL) 4 (root->right != NULL)) { root->key = findMin (root->right) ->key; remove (root->right, root->key); } else { BinaryTreeNode* nodeToRemove = root; root = (root->left != NULL) ? root->left : root->right; delete nodeToRemove