Recursive binary search tree insert function

So I am trying to implement a recursive version of the insert function and this is my current attempt. I can get it to change the data stored in the root node but the child nodes don't seem to hold anything. I believe that it is a reference problem but I just don't know how to make this work correctly. So how can I do this correctly? Even just showing me the insert function arguments would help.


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
class Btree{

private:

struct Node{
    int data;
    Node * left;
    Node * right;
};

Node * root;

public:

Btree();
//~Btree();
void insert(int x);
void insert(Node * topNode, int x);
};

Btree::Btree()
{
root = NULL;
}

void Btree::insert(Node * topNode, int x)
{
Node * current;

if(root == NULL)
{
    Node * n = new Node;
    n->left = 0;
    n->right = 0;
    n->data = x;
    root = n;
}
else if(topNode == NULL)
{
    Node * n = new Node;
    n->left = 0;
    n->right = 0;
    n->data = x;
    topNode = n;
}
else{
    if(x < topNode->data)
    {
        current = topNode->left;
        insert(current, x);
    }
    else{
        current = topNode->right;
        insert(current, x);
        }
    }
}

void Btree::insert(int x)
{
insert(root, x);
}

int main()
{
Btree one;

one.insert(3);
one.insert(4);
//one.insert(2);
}
Last edited on
Solve this by passing references to your nodes rather than just the nodes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Btree::insert(int x) {
    insert(&root, x);
}

void Btree::insert(Node **root, int x) {
    if(*root == nullptr) {
        *root = new Node;
        (*root)->left = (*root)->right = nullptr;
        (*root)->data = x;
    }
    else if ((*root)->data < x) {
            // recursive call
    }
    else {
        // recursive call
    }
}
Last edited on
So I did what you told me and then modified the recursive calls but I guess pointers to pointers is just over my head right now.
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

void Btree::insert(Node ** root,int x)
{
    Node * current;

    if(*root == nullptr)
    {
        *root = new Node;
        (*root)->left = 0;
        (*root)->right = 0;
        (*root)->data = x;
    }
    else if(x < (*root)->data)
        {
            current = (*root)->left;
            insert(&current, x);
        }
    else{
            current = (*root)->right;
            insert(&current, x);
        }
}


void Btree::insert(int x)
{
    insert(&root, x);
}


A print statement like this makes my program crash.

1
2
3
4
5
6
7
void Btree::print()
{
    Node * current;

    current = root->left;
    cout << current->data;
}



Can someone explain what's going on in these 2 lines?
 
    insert(&root, x);


 
void Btree::insert(Node **root, int x) {


I know that a reference to a root pointer which points to a node object is being passed but in the function argument, of the second snippet, is that dereferencing the root pointer so that I can access the contents of the node object?
Last edited on
Topic archived. No new replies allowed.