Binary search tree not printing

Hello,
I am trying to print a binary search tree. By debugging i know that root remains equal to 'nullptr' every time it enters the function.
 
void BSTree::AddNode(int a, Node * ptr )


Thus nothing is printed ny print function aswell. Can some body help me with fixing this code? Would be of great help.

Regards
Saherch



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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;

class Node
{
private:
    int data;
    Node * left;
    Node * right;

public:
    Node();
    void SetData(int);
    int getData();
    void Setleft(Node *);
    Node * getleft();
    void Setright(Node *);
    Node * getright();
};

Node::Node()
{
    data = 0;
    left = nullptr;
    right = nullptr;
}

void Node::SetData(int a)
{
    data = a;
}

int Node::getData()
{
    return data;
}

void Node::Setleft(Node * ptr)
{
    left = ptr;
}

Node * Node::getleft()
{
    return left;
}

void Node::Setright(Node * ptr)
{
    right = ptr;
}


Node * Node::getright()
{
    return right;
}


class BSTree
{
private:
    Node * root;

public:
    BSTree();
    void setRoot(Node *);
    Node * getRoot();
    void AddNode(int, Node *);
    void print(Node *);
};

BSTree::BSTree()
{
    root = nullptr;
}

void BSTree::setRoot(Node * p)
{
    root = p;
}

Node * BSTree::getRoot()
{
    return root;
}

void BSTree::AddNode(int a, Node * ptr )
{
    if (ptr == nullptr)
    {
        Node * temp = new Node();
        temp->SetData(a);
        temp->Setleft(nullptr);
        temp->Setright(nullptr);
        ptr = temp;

    }
    else
    {
        if (a < ptr->getData())
        {
            AddNode(a, ptr->getleft());
        }
        else if (a> ptr->getData())
        {
            AddNode(a, ptr->getright());
        }
    }
}

void BSTree::print(Node * n)
{
    cout<<n->getData()<<", ";
    print(n->getleft());
    print(n->getright());
}


int main(int argc, char *argv[])
{
    BSTree tree;

    tree.AddNode(5,tree.getRoot());
    tree.AddNode(2,tree.getRoot());
    tree.AddNode(10,tree.getRoot());

    tree.print(tree.getRoot());

    return 0;
}



> void BSTree::AddNode(int a, Node * ptr )
pass a copy of the pointer.

> ptr = temp;
modifies the copy of the pointer.

> tree.AddNode(10,tree.getRoot());
that looks cumbersome
tree.Add(10);


> #include <opencv2/core/core.hpp>
completely unrelated.
Try to create a minimal example.
1)
Isn't a pointer an address itself. When I am passing a address, should it not work?

2)
If I try to change the above function prototype for passing pointer by reference, like
 
void BSTree::AddNode(int a, Node * & ptr )


i still get some error when calling it with statement
 
tree.AddNode(5,tree.getRoot());


The error says "No matching functio to call". I usually don't have to make any changes to 'function calling' when pass by reference is used. What is the appropriate way to make the two work together?
Last edited on
The problem I see is you never set root when you add the first node.

You probably should have 2 functions for AddNode instead of 1. AddNode(int a) would check to see if the root is null. If so, create a node and store it as root. If root is not null, call the other AddNode function, passing it the right or left branch pointer, as necessary (or just returning if a = root->getData()).

Edit: I think this is the same thing ne555 is trying to say, but with different words.
Last edited on
> Isn't a pointer an address itself. When I am passing a address, should it not work?
sure, if you take the address of the pointer.
1
2
3
4
void BSTree::AddNode(int value, Node ** ptr ){
   //...
   *ptr = new Node(value);
}

You are copying the pointer and modifying the copy, so the original remains unchanged.


> i still get some error when calling it
> The error says "No matching functio to call".
¿description of the function match?
¿candidates?
That information was (or should have) in the error message of your compiler. If you do not understand something that's more a reason to not edit it away.

You cannot bind a non-constant reference to a temporary.
`getRoot()' returns a copy of the root pointer, a temporary.


> I think this is the same thing ne555 is trying to say
no.
Topic archived. No new replies allowed.