1

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?

1 Answer 1

1

Your analysis of the problem is exactly right: the pointer is passed by value, so any changes that the function makes to the value of parent are not visible to the caller.

One way to fix the problem is by passing the parent pointer by reference:

void BinarySearchTree::BuildTreeR(int start, int end, Leaf *& parent){

(note the added &).

This way any changes made to parent inside the function will automatically be visible to the caller.

Sign up to request clarification or add additional context in comments.

3 Comments

I had tried that last night, before the project was due. Editing it in both the implementation and the header file, but I still had the same error for all of the data I was attempting to change.
I attempted to use this again and now it wont compile saying 'BinarySearchTree::BuildTreeR' : cannot convert parameter 3 from 'Leaf *' to 'Leaf *&'
@mcnnowak: You also need to change get_left() and get_right() to return a reference to pointer (or a pointer to pointer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.