【问题标题】:Segmentation error when inserting more than 30,000 elements into a binary search tree将超过 30,000 个元素插入二叉搜索树时出现分段错误
【发布时间】:2018-07-15 04:07:07
【问题描述】:

我目前正在开发一个程序,该程序应生成至少 100,000 个 URL (www.google.com.my) 并将它们插入到二叉搜索树 (BST) 和哈希表中。我把它们都说对了,但是当我尝试将 100,000 个 URL 插入 BST 时,我得到以下信息:Process returned -1073741571 (0xC00000FD) execution time: 21.358 s。当我运行调试器时,我得到以下信息:Program received signal SIGSEGV, Segmentation fault.In ?? () ()。调试器没有显示错误在哪一行,那么问题出在哪里,如何解决?

这是我的代码:

main.cpp

#include <iostream>
#include <ctime>
#include <vector>
#include <stdint.h>
#include <cstdlib>
#include <fstream>
#include <string>
#include <conio.h>
#include <stdexcept>
#include "HashTable.cpp"
#include "BinarySearch.h"

using namespace std;

HashTable<long long> ht(0);
treeNode *root = NULL;

vector<long long> dataList;
bool error_msg = false;
static long long nextIC = 1;
long long getNextIC()
{
    return ++nextIC;
}

.
.
.
.
ifstream file2(fileName);
        while (getline(file2, str))
        {
            root = insertNode(root, countData);
            countData++;
        }
        file2.close();

        end = clock();
        elapsed_secs = double(end - begin) / ( CLOCKS_PER_SEC / 1000);

BinarySearch.h

#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

struct treeNode
{
    long long data;
    treeNode *left;
    treeNode *right;
};

treeNode *insertNode(treeNode *node,long long data)
{
    if(node==NULL)
    {
        treeNode *temp = new treeNode();
        temp -> data = data;
        temp -> left = temp -> right = NULL;
        return temp;
    }
    if(data >(node->data))
    {
        node->right = insertNode(node->right,data);
    }
    else if(data < (node->data))
    {
        node->left = insertNode(node->left,data);
    }
    /* Else there is nothing to do as the data is already in the tree. */
    return node;
}

treeNode * searchNode(treeNode *node, long long data)
{
    if(node==NULL)
    {
        /* Element is not found */
        return NULL;
    }
    if(data > node->data)
    {
        /* Search in the right sub tree. */
        return searchNode(node->right,data);
    }
    else if(data < node->data)
    {
        /* Search in the left sub tree. */
        return searchNode(node->left,data);
    }
    else
    {
        /* Element Found */
        return node;
    }
}
void displayInorder(treeNode *node)
{
    if(node==NULL)
    {
        return;
    }
    displayInorder(node->left);
    cout<<" " << node->data<<" ";
    displayInorder(node->right);
}
void displayPreorder(treeNode *node)
{
    if(node==NULL)
    {
        return;
    }
    cout<<" " <<node->data<<" ";
    displayPreorder(node->left);
    displayPreorder(node->right);
}
void displayPostorder(treeNode *node)
{
    if(node==NULL)
    {
        return;
    }
    displayPostorder(node->left);
    displayPostorder(node->right);
    cout<<" " <<node->data<<" ";
}

【问题讨论】:

  • 0xC00000FD堆栈溢出 的错误代码。你是否对递归太深入了?
  • 您应该在问题中说您使用的是 Windows。
  • 对于解决方案,probably dupeOr this。请在 Stack Overflow 上asking 之前做一些研究。
  • @user202729 增加堆栈大小不是一个好的解决方案。最好减少递归的深度。

标签: c++ data-structures binary-search-tree


【解决方案1】:

树木的最大深度是有限的,即从根到最远叶子的距离。这种限制是由树算法的递归性质强加的,因为递归深度本身受堆栈内存大小的限制。递归深度受到堆栈空间的限制,因为每个函数调用都会在堆栈上放置一个新的调用帧。

这个最大深度限制对于非平衡 BST 来说是个问题,因为这种树可能会变得不平衡。在最坏的情况下,当元素按排序(或反向)插入时,深度线性增长。

要支持任何插入顺序的大量元素,您可以使用平衡的 BST,例如红黑树或 AVL 树。对于平衡树来说,深度限制不是问题,因为它们的最大深度呈对数增长。

【讨论】:

  • (旁注:std::set 通常用红黑树实现)
猜你喜欢
  • 1970-01-01
  • 2022-11-05
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
  • 2020-01-31
相关资源
最近更新 更多