二叉树的示例实现不适用于大量值
Sample Implementation of Binary Trees doesn't work for large number of values
我最近试图实现一个二值树,它似乎工作小于1000个值,但之后它最终给我堆栈溢出错误
#include<iostream>
#include<math.h>
using namespace std;
struct Node{
long long int val;
Node *left;
Node *right;
};
struct BinaryTree{
Node *head = (Node*) malloc(sizeof(Node));
bool headSet = false;
Node* findLast(Node *asgn,int val){
if (val > asgn->val){
if (asgn->right != NULL)
asgn = findLast(asgn->right, val);
else
return asgn;
}
else{
if (asgn->left != NULL)
asgn = findLast(asgn->left, val);
else
return asgn;
}
return asgn;
}
void insert(long long int vals){
if (headSet){
Node *asgn = (Node*) malloc(sizeof(Node));
asgn = findLast(head,vals);
Node *fix = (Node*)malloc(sizeof(Node));
fix->val = vals;
fix->left = NULL;
fix->right = NULL;
if (vals > asgn->val){
asgn->right = fix;
asgn->left = NULL;
}
else{
asgn->right = NULL;
asgn->left = fix;
}
}
else{
head->val = vals;
head->right = NULL;
head->left = NULL;
headSet = true;
}
}
};
int main(){
BinaryTree a;
for (long long int i = 0; i < 100;i++)
a.insert(i);
return 0;
}
例如: -如果我改变
for (long long int i = 0; i < 100;i++)
a.insert(i);
for (long long int i = 0; i < 10000;i++)
a.insert(i);
它给我错误。我似乎无法理解为什么会发生这种情况,堆栈溢出在哪里?
您的堆栈溢出来自您的findLast
方法,一旦二叉树变得太大,递归就会变得太多,并在某一点上溢出调用堆栈。你应该把它转换成一个非递归的方法,把搜索信息存储在某种结构中,并动态地分配,这样你的堆栈就不会被填满。
p。如果在c++中使用new
而不是malloc
和delete
来清除分配的内存,您当前正在泄漏内存
迭代版本:
Node* findLast(Node *asgn,int val){
while (1)
{
if (val > asgn->val) {
if (asgn->right != NULL)
asgn = asgn->right;
else
return asgn;
}
else {
if (asgn->left != NULL)
asgn = asgn->left;
else
return asgn;
}
}
}
很容易,不是吗?
和insert
的更正版本:
void insert(long long int vals){
if (headSet){
// Node *asgn = (Node*) malloc(sizeof(Node)); // removed
Node *asgn = findLast(head,vals); // line changed slighty
Node *fix = (Node*)malloc(sizeof(Node));
fix->val = vals;
fix->left = NULL;
fix->right = NULL;
if (vals > asgn->val){
asgn->right = fix;
//asgn->left = NULL; // removed
}
else{
//asgn->right = NULL; // removed
asgn->left = fix;
}
}
else{
head->val = vals;
head->right = NULL;
head->left = NULL;
headSet = true;
}
}
这不是一个真正的答案。我只是想建议一个代码更改,作为原始代码的附录,并附上Michael Walz的更正。我的建议是省略headSet
成员并将head
成员初始化为NULL
,并在第一次插入时分配head
,如下所示:
struct BinaryTree{
Node *head = (Node *)NULL;
Node* findLast(Node *asgn,int val){ // Courtesy of Michael Walz
while (1){
if (val > asgn->val){
if (asgn->right != NULL)
asgn = asgn->right;
else
return asgn;
}
else{
if (asgn->left != NULL)
asgn = asgn->left;
else
return asgn;
}
}
}
void insert(long long int vals){
Node *fix = (Node*)malloc(sizeof(Node));
fix->val = vals;
fix->left = NULL;
fix->right = NULL;
if (head != NULL){
Node *asgn = findLast(head,vals);
if (vals > asgn->val){
asgn->right = fix;
}
else{
asgn->left = fix;
}
}
else{
head = fix;
}
}
}
相关文章:
- OpenGL - 在 NDC 中计算位置适用于着色器,但不适用于'regular'程序
- 使用模板参数重载C++方法:如何使其适用于模板的子类?
- 如何修复我的最大公约数代码?它适用于除零和零以外的所有数字
- 选择排序C++(已修改)并非适用于所有情况
- 无法让"std::enable_if"适用于无作用域枚举
- 请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例
- 确定夏令时是否适用于特定日期
- 是否有一种 STL 算法可以最后找到,但它也适用于指针?
- 十进制到二进制的实现不能完全适用于我大学的检查器。问题或提示可能是什么
- 使用程序集嵌入数据时"Undefined reference"错误,使用适用于窗口的 mingw-w64 编译(COFF 而不是 ELF)
- 为什么 std::vector 适用于类定义中的不完整类型?
- 为什么哈希<常量字符*>适用于字符串而不是字符串变量?
- Lambda适用于最新的Visual Studio,但在其他地方不起作用
- 我该如何文档文档以使文档适用于类成员而不是匿名类型
- 计算 c# 中二进制文件符号的频率不起作用,但适用于等效的 c++ 代码
- 为什么 fstream.open(文件名) 适用于文字而不是生成的字符串?
- 模板重载和 SFINAE 仅适用于函数而不是类
- 为什么链接器不抱怨多个函数定义(仅适用于模板化函数)
- 我可以使用' == '来比较两个向量吗?我试过了,似乎工作正常。但我不知道它是否适用于更复杂的情况
- 延长临时的生命周期,适用于块范围的聚合,但不是通过"新";为什么?