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;
}

Recommended Answers

All 5 Replies

Try

p->setLeft( root->getLeft() );

and likewise.

that fixed the first error but the others i cant do that to

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);

BST_Node::getLeft() is a function. You can't assign values to functions. You have to pass values to them. I dont know what you are trying to do. I dont know if you are trying to delete the left node or what. In anycase getLeft does not take a bool value as parameters, which is the datatype returned by delNode. You should be able to work out what the problem is after the first solution was given. In anycase just try

delNode(key);

only and see. I didnt do any testing nor did I read the whole program, so no guarantees.

what i am trying to do with the root->getRight() and root->getLeft() is basically just delete the left or right node of the tree depending on what the key is.

Member Avatar for iamthwee

what i am trying to do with the root->getRight() and root->getLeft() is basically just delete the left or right node of the tree depending on what the key is.

You do realise that to delete a node in a binary search tree, whilst still retaining the inherent structure is a non-trivial exercise.

But if you choose to do this there are three cases

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.