i am getting this error "error C2664: 'bool __thiscall BST::insert(class BST_Node *& ,struct Key)' : cannot convert parameter 1 from 'class BST_Node *' to 'class BST_Node *& '
A reference that is not to 'const' cannot be bound to a non-lvalue" and i have no ideal why everything looks fine to me unless im overlooking something.
it points to this
bool BST::insert(BST_Node* &subRoot, Key key)
{
BST_Node* node = new BST_Node(key);
if(!subRoot)
{
subRoot = node;
return true;
}
if(key == root->getKey())
{
return false;
delete node;
}
if(key < subRoot->getKey())
{
if(subRoot->getLeft() == NULL)
{
root->setLeft(new BST_Node(key));
}
else
{
insert(subRoot->getLeft(), key.data);
}
}
}
#include <iostream>
using namespace std;
// structs/Classes
/*********************************************************************/
struct Key {
char* data; // string
Key();
Key(char* data) { this->data = new char[strlen(data)];
strcpy(this->data,data);}
~Key() {delete data;};
Key(Key& key);
operator= (Key& key);
bool operator== (Key& key) { return 0 == strcmp(this->data,key.data);} // is this == to that key
bool operator< (Key& key) { return -1 == strcmp(this->data,key.data);}// is this < that key
bool operator> (Key& key) { return 1 == strcmp(this->data,key.data);} // is this > that key
};
Key::Key()
{
data = NULL;
}
Key::Key(Key& key)
{
data = key.data;
}
/*********************************************************************/
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;
}
/*********************************************************************/
class BST {
private:
BST_Node* root; // root of tree
int size; // number of elements in tree
// Mutators - called by public methods
bool insert(BST_Node* &subRoot, Key key); // insert to subtree (recursive)
void preOrder(BST_Node* subRoot); // preOrder-Traversal of tree (recursive)
void inOrder(BST_Node* subRoot); // inOrder-Traversal of tree (recursive)
void postOrder(BST_Node* subRoot); // postOrder-Traversal of tree (recursive)
public:
// Managers
BST(); // Construct Empty Tree
BST(BST& bst); // Copy Constructor
~BST(); // Destruct tree
// Operators
BST& operator= (BST& bst); // Assignment
// Accessors
int getSize() {return size;}; // returns number of elements in tree
bool empty(); // is tree empty?
bool full(); // is memory available?
void preOrder(); // preOrder-Traversal of tree (recursive)
void inOrder(); // inOrder-Traversal of tree (recursive)
void postOrder(); // postOrder-Traversal of tree (recursive)
bool findKey(); // take input from user-keyboard
bool findKey(Key key, BST_Node* &parent, BST_Node* &foundNode); // given key-data, find node
// Mutators
bool insert(); // take input from user-keyboard
bool insert(Key key); // insert key-data into tree
bool delNode(); // take input from user-keyboard
bool delNode(Key key); // delete node containing key-data from tree
};
BST::BST()
{
size = 0;
root = NULL;
}
BST::BST(BST& bst)
{
root = bst.root;
size = bst.size;
}
BST& BST::operator= (BST& bst)
{
root = bst.root;
size = bst.size;
return *this;
}
bool BST::insert()
{
Key key;
cout << "Enter a key" << endl;
cin >> key.data;
}
bool BST::insert(BST_Node* &subRoot, Key key)
{
BST_Node* node = new BST_Node(key);
if(!subRoot)
{
subRoot = node;
return true;
}
if(key == root->getKey())
{
return false;
delete node;
}
if(key < subRoot->getKey())
{
if(subRoot->getLeft() == NULL)
{
root->setLeft(new BST_Node(key));
}
else
{
insert(subRoot->getLeft(), key.data);
}
}
}
bool BST::empty()
{
return root == NULL;
}
bool BST::full()
{
BST_Node* bstnode = new BST_Node;
if(!bstnode)
{
return true;
}
else
{
delete bstnode;
return false;
}
}
bool BST::insert(Key key)
{
insert(root, key);
}