I know there is few questions with a similar title, however I went over them and still couldn't solve my error.
This is the BST implementation:
struct node {
int val;
node* left;
node* right;
};
node* createNewNode(int x)
{
node* nn = new node;
nn->val = x;
nn->left = nullptr;
nn->right = nullptr;
return nn;
}
void bstInsert(node* &root, int x)
{
if(root == nullptr) {
root = createNewNode(x);
return;
}
if(x < root->val)
{
if(root->left == nullptr) {
root->left = createNewNode(x);
return;
} else {
bstInsert(root->left, x);
}
}
if( x > root->val )
{
if(root->right == nullptr) {
root->right = createNewNode(x);
return;
} else {
bstInsert(root->right, x);
}
}
}
Here is my main:
int main() {
node *root = nullptr;
for (int i = 0; i < 100000; i++) {
bstInsert(root, i);
}
}
If I try to insert 10000 elements then it works alright. however when I try to insert 100000 elements I get in the debugger:
Signal = SIGSEGV (Segmentation fault)
It happens when the value of I in the loop reaches 32469, what am I missing ?
Aucun commentaire:
Enregistrer un commentaire