i dont even know if im on the right track with this one but here it goes.
im trying to write a delete function for a BST and this is what i have
bool BST::delNode(Key key)
{
BST_Node* node;
BST_Node* p;
if(findKey(key,root))
{
if(root == NULL)
return false;
if(key == root->getKey())
{
delete key.data;
return true;
}
else if(root->getLeft() == NULL)
{
p = root->getRight();
free(root);
return false;
}
else if(root->getRight() == NULL)
{
p = root->getLeft();
free(root);
return false;
}
else
{
node = root->getRight();
p = root->getRight();
while (p->getLeft())
{
p = p->getLeft();
}
p->getLeft() = root->getLeft();
free(root);
return true;
}
if(root->getKey() < key)
{
root->getLeft() = delNode(key);
return true;
}
else
{
root->getRight() = delNode(key);
return true;
}
delete key.data;
}
else
return false;
}
now i dunno if its even close to what it should be but i am getting 3 errors in it
error C2106: '=' : left operand must be l-value
p->getLeft() = root->getLeft();
error C2440: '=' : cannot convert from 'bool' to 'class BST_Node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
root->getLeft() = delNode(key);
error C2440: '=' : cannot convert from 'bool' to 'class BST_Node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
root->getRight() = delNode(key);
so if you could help me out with them that would be awsome
here is my BST class
class BST_Node {
private:
Key key; // key holds the data
BST_Node* left; // ptr to left subtree
BST_Node* right; // ptr to right subtree
public:
// Managers
BST_Node();
BST_Node(Key key); // Construct given key-data
BST_Node(BST_Node& node); // Copy Constructor
~BST_Node(); // Destruct node
// Operators
BST_Node& operator= (BST_Node& node); // Assignment
// Accessors
Key getKey() {return key;}; // get Key Data
BST_Node* getLeft() {return left;}; // get root of left subtree
BST_Node* getRight() {return right;}; // get root of right subtree
void setLeft(BST_Node* node);
void setRight(BST_Node* node);
};
BST_Node::BST_Node()
{
key.data = NULL;
left = NULL;
right = NULL;
}
BST_Node::BST_Node(Key key)
{
cout << "enter key" << endl;
cin >> key.data;
}
BST_Node::BST_Node(BST_Node& node)
{
right = node.right;
left = node.left;
//key = node.key;
}
BST_Node::~BST_Node()
{
delete left;
delete right;
}
BST_Node& BST_Node::operator= (BST_Node& node)
{
right = node.right;
left = node.left;
//key = node.key;
return *this;
}
void BST_Node::setLeft(BST_Node* node)
{
this->left = node;
}
void BST_Node::setRight(BST_Node* node)
{
this->right = node;
}