二叉搜索树崩溃
我正在用 C++ 实现二叉搜索树 我有以下代码来向树添加条目:
void tree::add(int n)
{
int found;
leaf *t,*parent;
findparent(n,found,parent);
if(found==YES)
cout<<"\nSuch a Node Exists";
else
{
t=new leaf;
t->data=n;
t->l=NULL;
t->r=NULL;
if(parent==NULL)
p=t;
else
parent->data > n ? parent->l=t : parent->r=t;
}
}
在我的 main 中,我使用映射将从文本文件读取的值存储为整数。现在,当我将值传递给添加函数时,它会使程序崩溃。
int main()
{
tree t;
map<int,int> word;
map<int,int>::iterator count;
string str;
int num;
string space ="";
while((str=value(cin))!=space )
{
num = atoi(str.c_str());
++word[num];
}
int size = (int) word.size();
int data[size];
int x = 0;
for(count = word.begin(); count!=word.end(); ++count){
data[x] = (*count).first;
x = x+1;
}
for (int iter = 0; iter<size; iter++){
int x = 3 * data[iter];
t.add(x);
}
return 0;
}
我在这里所做的是,使用 atoi 将用户输入转换为整数,然后将它们添加到地图中。然后我获取地图的大小并使用它来用元素填充数组。现在,当我迭代数组并尝试使用 add 函数将数组元素传递到树时,它会使程序崩溃。当我尝试添加固定数组元素时,程序运行良好。例如:
int data[] = {6,7,8,9};
如果我的 main 中有这个固定数组并传递要添加的元素,它工作正常,这让我觉得 add 方法没有问题。请帮助我找到问题,我很困惑
Pastebin 整个程序的链接: http://pastebin.com/SWqTccJf
I am implementing a Binary Search tree in C++
I have the following code to add an entry to the tree:
void tree::add(int n)
{
int found;
leaf *t,*parent;
findparent(n,found,parent);
if(found==YES)
cout<<"\nSuch a Node Exists";
else
{
t=new leaf;
t->data=n;
t->l=NULL;
t->r=NULL;
if(parent==NULL)
p=t;
else
parent->data > n ? parent->l=t : parent->r=t;
}
}
In my main, I am using a map to store the values read from a text file as integer. Now, when I pass the value to the add function it crashes the program.
int main()
{
tree t;
map<int,int> word;
map<int,int>::iterator count;
string str;
int num;
string space ="";
while((str=value(cin))!=space )
{
num = atoi(str.c_str());
++word[num];
}
int size = (int) word.size();
int data[size];
int x = 0;
for(count = word.begin(); count!=word.end(); ++count){
data[x] = (*count).first;
x = x+1;
}
for (int iter = 0; iter<size; iter++){
int x = 3 * data[iter];
t.add(x);
}
return 0;
}
What I am doing here is, I convert the user input to integers using atoi and then add them to map. Then I get the size of the map and use that to fill an array with the elements. Now, when I iterate through the array and try to pass the array elements to the tree using add function, it is crashing the program. The program works fine, when I am trying to add a fixed array element. For eg:
int data[] = {6,7,8,9};
if I have this fixed array in my main and pass the elements to add, it works fine, this makes me feel there is no issue with add method. Please help me in finding the issue, am confused
Pastebin Link of entire program: http://pastebin.com/SWqTccJf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 Tyler Hyndman 是对的,它不是标准 C++。然而,例如 g++ 确实支持这种 C99 风格的“语言扩展”。它并没有消除这样一个事实:在堆栈上分配数组通常是一种不好的做法,其大小在编译时未知,并且实际上可能非常大。
我的猜测是代码中存在尚未提供的错误。一些涉及内存/指针问题的错误可以根据内存的内容被掩盖,这可能会让它们令人沮丧。无论如何,我在提供的代码中没有看到错误。我会特别注意它崩溃的具体位置,以及崩溃时程序的状态。
为了获得最佳帮助,请提供最少(但足够)的代码来重现问题。如果看起来太多,也许可以使用pastebin?
//编辑// 好的,我查看了您的完整代码清单。
请参阅此修订后的代码。主要区别:
1) 清除树的代码中有一个错误。 Tree::del() 不应在此处调用,因为它用于删除特定值。它必须找到要删除的节点,然后处理各种特殊情况。当您清除树的内容时,您可以继续递归删除所有节点。简单&快的!
2) 我将你的两个查找功能合二为一。
3) Tree::del 中存在一个错误,即被删除的节点是根节点的情况。当根节点被删除时,父节点将为 NULL...
4) 从输入读取数字的逻辑发生了变化。
我希望你能学习代码并看看有什么变化&为什么。还要记住查看代码崩溃的确切位置&获取尽可能多的有关崩溃的信息,而不是仅仅说“天啊,它崩溃了!”。
此致 :)
I think Tyler Hyndman is right that it's not standard C++. However, g++ for example does support this C99-ish "language extension". It doesn't take away from the fact that it's generally a bad practice to allocate arrays on the stack whose size is not known at compile-time, and could in fact be quite large.
My guess is that there's a bug in code which has not been supplied. Some bugs that involve memory/pointer issues can be masked depending on the contents of memory, which can make them frustrating. Anyway I don't see a bug in the supplied code. I would pay attention to exactly where it crashes, and what is the program's state at the point of the crash.
For best help, provide the minimum (yet sufficient) code to reproduce the problem. Maybe use pastebin if it seems like too much?
//EDIT// OK I looked at your full code listing.
See this revised code. The main differences:
1) You have a bug in the code the clears out the tree. Tree::del() should not be invoked here, because it's for removing a particular value. It has to find the node to be removed, then deal with various special cases. When you're wiping out the contents of the tree, you can just go ahead and recursively delete all the nodes. Easy & quick!
2) I unified your two find functions into one.
3) There was a bug in Tree::del for the case where the node being removed is the root. When the root node is being removed, parent will be NULL...
4) The logic for reading numbers from the input is changed.
I hope you'll study the code & see what's changed & why. Also remember to see exactly where your code crashes & get as much information as you can about the crash instead of just saying "aw darn, it crashed!".
Best regards :)
不能使用
在 C++ 中,当编译时不知道
size
时, 。要么使用或者,因为这是 C++,所以使用
std::vector
In C++ you can not use
when
size
is not known at compile time. Either useOr, since this is C++, use a
std::vector