【发布时间】: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 dupe。 Or this。请在 Stack Overflow 上asking 之前做一些研究。
-
@user202729 增加堆栈大小不是一个好的解决方案。最好减少递归的深度。
标签: c++ data-structures binary-search-tree