Coding Problem with Binary Tree

Hi all,

I am having an issue with the following. I created a Binary Tree and I'm having trouble accessing the value of a node after calling the search() function below. Here is my code.

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
class BNode
{
public:
    int my_value;
    BNode *left;
    BNode *right;
    BNode()
    {
        
    };
    
    BNode(int val)
    {
      my_value = val;  
    };
};

class BTree
{
public:
    BNode *root;
    int size;
    
    BTree()
    {
      root = NULL;  
      size = 0;
    };
    
    ~BTree()
    {
        destroy_tree();
    }
    
    void insert(int key)
    {
        if(root!=NULL){
            insert(key, root);
        }
        else {
            root = new BNode(key);
            root->left = NULL;
            root->right = NULL;
            size++;
        }
    };
    
    void insert(int key, BNode *leaf)
    {
        if(key < leaf->my_value){
            if(leaf->left != NULL){
                insert(key, leaf->left);
            }
            else{
                leaf->left = new BNode(key);
                leaf->left->left = NULL;
                leaf->left->right = NULL;
                size++;
            }
        }
        else if(key >= leaf->my_value){
            if(leaf->right != NULL){
                insert(key, leaf->right);
            }
            else{
                leaf->right = new BNode(key);
                leaf->right->left = NULL;
                leaf->right->right = NULL;
                size++;
            }
        }
    };
    
    BNode * search(int key)
    {
        return search(key, root);
    };
    
    BNode * search(int key, BNode *leaf)
    {
        if(leaf != NULL){
            if(key == leaf->my_value)
                return leaf;
            
            if(key < leaf->my_value){
                search(key, leaf->left);
            }
            else if(key >= leaf->my_value){
                search(key, leaf->right);
            }
        }
        else{
            return NULL;
        }
    };
    
    void destroy_tree()
    {
        destroy_tree(root);
    };
    
    void destroy_tree(BNode *leaf)
    {
        if(leaf != NULL){
            destroy_tree(leaf->left);
            destroy_tree(leaf->right);
            delete leaf;
        }
    };
};

int main(int argc, char** argv) 
{
    int list[10] = {20, 15, 13, 25, 27, 21, 49, 17, 5, 7};
    BTree *bt = new BTree();
    for(int i=0; i < 10; i++){
        bt->insert(list[i]);
    }
    cout << bt->size << endl;

    // The problem occurs here. It runs, but is not able to execute the following. 
    cout << " found 27 " << bt->search(27)->my_value << endl;

return 0;
}


On the line that states:
cout << " found 27 " << bt->search(27)->my_value << endl;

The program runs and then fails at this line. The binary tree search() function should return the BNode containing 27. For some reason, it has trouble printing my_value (which is an int). Do you know why?
Hi guys - I am getting a Segmentation Fault on this. I am not sure why...
Here:

1
2
3
4
5
6
if(key < leaf->my_value){
    search(key, leaf->left);
}
else if(key >= leaf->my_value){
    search(key, leaf->right);
}

You are simply invoking the search function. You should be returning the result in each case.
Ahh!!!!!! Thank you very much.
Topic archived. No new replies allowed.