Binary Search Tree

Hey, everyone.
I'm working on a school project to make a binary tree, but I cannot make it work.
I am not allowed to change the function definitions, so I am being pigeonholed into figuring it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
template<typename T>
void BinarySearchTree<T>::add(T value, Node<T> * subtree)
{

    if (subtree == nullptr)
    {
        Node<T>* node = new Node<T>;
        node->setValue(value);
        subtree = node;
        return;
    }
    else if (value < subtree->getValue())
    {
        add(value, subtree->getLeft()    );
    }
    else if (value > subtree->getValue())
    {
        add(value, subtree->getRight()    );
    }
    else
    {
        std::cout << "Value already in tree" << std::endl;
    }

}


I have another function that calls the one above like this:
 
add(value, m_root);


I know WHY it isn't working. I know that I'm creating copies of the pointers when I make the function call, but I cannot figure how to successfully add nodes to my tree using the above function definition. I'm not allowed to change the function's signature :(

EDIT: I know I need to change the address of the pointer inside the tree, but it is not allowing me to do so.

Any help that anyone can give me would be greatly appreciated.


Nevermind. Figured it out.
Last edited on
Topic archived. No new replies allowed.