Question: BELOW is my code for a remove function in C++ for a binary search tree. This code doesnt seem to be working and I was

BELOW is my code for a remove function in C++ for a binary search tree. This code doesnt seem to be working and I was hoping for suggestions on how to perfect my helper function so that it would successfully find, remove, and remake the binary search tree so it is legal, then update the size

BinaryTreeNode* parent;

virtual void removeHelper(BinaryTreeNode* node, const Key& key) { if (node == nullptr) { return; } else if (keyLessThan(key, node->_key)) { parent = node; removeHelper(node->_left, key); } else if (keyLessThan(node->_key, key)) { parent = node; removeHelper(node->_right, key); } else if (keyEquals(key, node->_key)) { //if node to delete has no children if (node->_left == nullptr && node->_right == nullptr) { node = nullptr; } //if node to delete has two children else if (node->_left != nullptr && node->_right != nullptr) { //and it is the parents left child if (keyEquals(parent->_left->_key, node->_key)) { parent->_left->_key = minHelper(node->_right)->_key; } else //it is the parents right child { parent->_right->_key = minHelper(node->_right)->_key; } } //if node to delete has one child else { //and it is the parents left child if (keyEquals(parent->_left->_key, node->_key)) { node->_left == nullptr ? parent->_left->_key = node->_right->_key : parent->_left->_key = node->_left->_key; } else //it is the parents right child { node->_left == nullptr ? parent->_right->_key = node->_right->_key : parent->_right->_key = node->_left->_key; } } }

return; node->_size = 1 + size(node->_left) + size(node->_right);

}

public:

// remove key (and its value) from table virtual void remove(const Key& key) { removeHelper(_root, key); }

HERE IS THE CODE FOR A BINARY TREE ELEMENT

protected:

struct BinaryTreeNode { Key _key; Value _value; BinaryTreeNode* _left; BinaryTreeNode* _right; unsigned _size;

BinaryTreeNode(const Key& key = Key{}, const Value& value = Value{}, unsigned size = 0, BinaryTreeNode* ell = nullptr, BinaryTreeNode* r = nullptr) : _key{ key }, _value{ value }, _size{ size }, _left{ ell }, _right{ r } {}

BinaryTreeNode(const BinaryTreeNode& that) : _key{ that._key }, _value{ that._value }, _size{ that._size }, _left{ that._left }, _right{ that._right } {}

~BinaryTreeNode() { if (_left != nullptr) { delete _left; _left = nullptr; } if (_right != nullptr) { delete _right; _right = nullptr; } _size = 0; } };

// Key value comparison (less than) bool keyLessThan(const Key& lhs, const Key& rhs) const { return lhs < rhs; }

// Equality of key values bool keyEquals(const Key& lhs, const Key& rhs) const { return lhs == rhs; }

// Equality of key values bool keyLessThanOrEquals(const Key& lhs, const Key& rhs) const { return keyEquals(lhs, rhs) || keyLessThan(lhs, rhs); }

// The container of the pairs BinaryTreeNode* _root;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!