简体   繁体   English

分段错误二叉搜索树

[英]Segmentation fault Binary search Tree

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:这是 BST 实现:

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.如果我尝试插入 10000 个元素,那么它可以正常工作。 however when I try to insert 100000 elements I get in the debugger:但是,当我尝试插入 100000 个元素时,我会进入调试器:

Signal = SIGSEGV (Segmentation fault)

It happens when the value of I in the loop reaches 32469, what am I missing?当循环中的 I 值达到 32469 时会发生这种情况,我错过了什么?

First of all, your Binary Search Tree is Right Skewed Binary tree because the new element will get added as the right most child of existing tree.首先,您的二叉搜索树是右偏二叉树,因为新元素将被添加为现有树的最右边的子元素。

That said, for every insertion, the recursion will go as deep as the value of i passed to bstInsert() and, for some big value of i , eventually it run out of space, while recursing, and crash.也就是说,对于每次插入,递归将 go 与传递给bstInsert()i的值一样深,并且对于一些较大的i值,最终它会在递归时耗尽空间并崩溃。 It's not good idea to use recursion for insertion in such a big tree.在这么大的树中使用递归插入并不是一个好主意。 Better to go for iterative implementation.最好使用 go 进行迭代实现。

Additional:额外的:
Handle the case of new operator failure.处理new操作员失败的情况。

PS: PS:
On my system your code is not crashing for 100000 elements insertion:).在我的系统上,您的代码不会因插入100000元素而崩溃:)。

interestingly your code works just fine for me on fedora g++ compiler.有趣的是,您的代码在 Fedora g++ 编译器上对我来说工作得很好。 it might be my compiler allocates 8Bytes to integer and yours might be allocating 4Bytes to integer so please try this and let me know.可能是我的编译器为 integer 分配了 8 个字节,而你的编译器可能为 integer 分配了 4 个字节,所以请试试这个并告诉我。

#include <iostream>

struct node {
    long val;
    node* left;
    node* right;
};

node* createNewNode(long x)
{
    node* nn = new node;
    nn->val = x;
    nn->left  = nullptr;
    nn->right = nullptr;

    return nn;
}

void bstInsert(node* &root, long 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);
        }
    }
}
int main() {
    node *root = nullptr;
    for (long i = 0; i < 100000; i++) {
        bstInsert(root, i);
        std::cout << i << std::endl; // printing output to see the results using short gives overlow.
    }
}

if that does not work than you computer memory is not available as much as required it could be you only have less than 785 KB of CPU CACHE since at runtime program is in cache and with 100000 probably uses ~ 785 KB memory which in this specific case is in cache because it is at runtime and no management applied to it yet.如果这对您的计算机不起作用 memory 无法满足要求,则可能是您只有不到 785 KB 的 CPU 缓存,因为在运行时程序在缓存中并且 100000 可能使用〜 785 KB memory 在这种特定情况下位于缓存中,因为它处于运行时且尚未对其应用任何管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM