Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class bstdata>
- void BST<bstdata>::remove(bstdata data)
- {
- assert(!isEmpty());
- removeHelper(root, data);
- }
- template <class bstdata>
- BST::Node* BST<bstdata>::removeHelper(BST::Node* root, bstdata data)
- {
- if (root == NULL)
- {
- return root;
- }
- else if (data < root->data)
- {
- root->left = removeHelper(root->left,data);
- }
- else if (data > root->data)
- {
- root->right =removeHelper(root->right,data);
- }
- else
- {
- if(root->left == NULL && root->right == NULL)
- {
- delete root;
- root = NULL;
- }
- else if(root->right == NULL && root->left != NULL)
- {
- root->left = root; //not sure if this is correct or if
- delete root;
- }
- else if(root->right != NULL && root->left == NULL)
- {
- root->right = root;
- delete root;
- }
- else
- {
- int i = root->right.minimum();
- root->data = i;
- root->right = removeHelper(root->right, i);
- }
- return root;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement