Binary Search Tree Construction Problem

Hello,
I was wondering if someone could help with my construction of a Binary Search Tree.

My code looks like it should work when I compare to others but when I inspect my debugger it shows that my root_ node has no connection to any other nodes right (RL_) or left (LL_). Hopefully someone can 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
//Node Struct 
struct Node{
public:
    Node* RL_ = NULL; //Right Link
    Node* LL_ = NULL; //Left Link
    int key_;
    Node(int d) : key_(d){};
};

//BST Class
class BST{
private:
    Node* root_ = NULL;
    
public:
    BST(vector<int>);
    void initialize(int);
    void insert(int, Node*);
};

void BST::initialize(int key_in){root_ = new Node(key_in);}

BST::BST(vector<int> keys){ 
    initialize(keys[0]);
    
    for(int i=1; i<keys.size(); ++i)
        insert(keys[i], root_);
}

void BST::insert(int key_in, Node* ptr){  
    if (ptr == NULL)
        ptr = new Node(key_in);
    
    else if (key_in < ptr->key_)
        insert(key_in, ptr->LL_);
    
    else
        insert(key_in, ptr->RL_);
}


Note: keys is a vector<int>.
Last edited on
because your `ptr' argument is passed by value, any change done to it are local to the function.

you could pass by reference instead void insert(int, Node* &);
You're a hero.
Topic archived. No new replies allowed.