So I was working on a Project for a computer science class in which we need to create a binary search tree and a corresponding index. We needed to use recursion for this project.
Here is my class implementation:
class Leaf;
struct indexEntries;
class BinarySearchTree{
public:
BinarySearchTree();
~BinarySearchTree();
//Helper Functions for recursive calls
std::string search(std::string);
void BuildTree(std::string);
void inOrderPrint();
private:
//Recursive Functions
void BuildTreeR(int start, int end, Leaf * r);
void inOrderPrint(Leaf * start);
Leaf * search(std::string inquiry, Leaf * start);
void DeallocateTree(Leaf * start);
//Data members
Leaf * root;
std::vector<indexEntries> BSTindex;
};
class Leaf{
public:
Leaf(){
indexID = "";
AccNum = "";
left = NULL;
right = NULL;
};
void set_index(std::string input) {indexID = input;};
void set_AccNum(std::string input) {AccNum = input;};
void set_left(Leaf* newLeft) {left = newLeft;};
void set_right(Leaf* newRight) {right = newRight;};
std::string get_index() {return indexID;};
std::string get_AccNum() {return AccNum;};
Leaf * get_left() {return left;};
Leaf * get_right() {return right;};
private:
std::string indexID;
std::string AccNum;
Leaf * left;
Leaf * right;
};
And when I try to pass Leaf * BinarySearchTree::root to the function void BinarySearchTree::BuildTreeR(int, int, Leaf*) the Leaf that the root is pointing to goes unchanged.
Here is my BuildTreeR() function:
void BinarySearchTree::BuildTreeR(int start, int end, Leaf * parent){
int mid = (start+end)/2;
if(parent == NULL){
parent = new Leaf;
parent->set_index((BSTindex[mid]).indexID);
std::string fullEntry = BSTindex[mid].dataBaseEntry;
parent->set_AccNum(fullEntry.substr(4, 3));
}
if((mid-1)>start){
BuildTreeR(start, mid-1, parent->get_left());
}
if((mid+1)<end){
BuildTreeR(mid+1, end, parent->get_right());
}
}
Using the debugger, I found that the leaf pointed to by Leaf * parent gets changed, but these changes are not carried over to Leaf * BinarySearchTree::root, which stops my program from working.
The debugger says that the value of the data I'm trying to change is
CXX0030: Error: expression cannot be evaluated
Has anyone had this happen before/ know how to fix it?