我的二进制树的C ++代码有什么问题 [英] What is wrong in my C++ code for binary trees

查看:93
本文介绍了我的二进制树的C ++代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有编译器错误,但程序没有运行



我尝试过:



There is no compiler error but the program does not run

What I have tried:

#include <iostream>

using namespace std;
struct node
{
    int data;
    node*p_left;
    node*p_right;

};
node*insert(node*p_tree,int data)
{
    if(p_tree=NULL)
    {
        node*p_new_tree=new node;
        p_new_tree->p_left=NULL;
        p_new_tree->p_right=NULL;
        return p_new_tree;

    }

    if(data<p_tree->data)
    {
        insert(p_tree->p_left,data);
    }
    else if(data>p_tree->data)
    {
        insert(p_tree->p_right,data);
    }
}
int main()
{ node*p_temp;
    insert(p_temp,4);
    insert(p_temp,5);
    insert(p_temp,6);
    insert(p_temp,3);
    insert(p_temp,2);
    insert(p_temp,1);
    insert(p_temp,8);
    insert(p_temp,9);

}

推荐答案

不运行可能是您可以提供的最无用的错误报告:这不代表任何意思!这可能意味着我的程序立即结束,我的程序坐在那里什么都不做,甚至我的计算机着火并烧毁了建筑物。我们不知道。

所以你有一个问题,准确解释你没想到会发生什么,或者没有发生你做过的事!请记住,我们无法看到您的屏幕,访问您的硬盘或阅读您的想法 - 我们只能准确地获取您的工作内容!



基本上,在最好这是第一个选择:我的程序立即结束。

那是因为这正是你告诉它要做的。你告诉它加载树然后退出。它加载树后不必做任何事情,因为你没有告诉它。整个主要方法是对插入函数的八次调用,然后结束程序。因此,即使你的插入功能有效 - 我也不知道它是否适用 - 你没有简单的方法可以告诉你。



首先写一个show tree 遍历树并将其打印出来的功能。这样,您就可以了解您的树是否正确构建。如果是的话,一切都很好。如果不是 - 那么你需要使用调试器来跟踪你的代码并找出它为什么不正确!让它编译只是一个开始,简单的一点。现在来到了有趣的部分 - 让它完全按照您的意愿行事,而不是您告诉它的确切内容!这是一项技能 - 所以就像所有其他技能一样,你只能通过使用它来开发它。

试一试:看看你能找到什么。
"does not run" is probably the single most useless error report you can give: it means nothing! It could mean "my program immediately ends", "my program sits there and does nothing", or even "my computer catches fire and burns down the building". We don't know.
So you have a problem, explain exactly what happens that you didn't expect, or didn't happen that you did! Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work from!

And basically, at best it's the first option: "my program immediately ends".
That's because that is exactly what you told it to do. You have told it to load a tree and then exit. It doesn't have to do anything after it loads the tree, because you haven't told it to. The whole of your main method is eight calls to your insert function and then end the program. So even if your insert function works - and I have no idea if it does - there is no simple way for you to tell.

Start by writing a "show tree" function which traverses the tree and prints it out. That way, you'll get an idea if your tree is being built correctly. If it is, all well and good. If it isn't - then you need to use the debugger to follow your code through and find out why it isn't correct! Getting it to compile is only the start, the easy bit. Now comes the fun part - getting it to do exactly what you want, rather than exactly what you told it to! And that's a skill - so just like all other skills, you only develop it by using it.
Give it a try: see what you can find out.


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



您在新的数据的位置> node ?

如何在树中的上一个节点中链接新节点?
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Where are you storing data in the new node ?
How do you link a new node in previous node in the tree ?


插入函数中,当 p_tree 参数<$ c $时,不会分配数据成员c> NULL :

In your insert function the data member is not assigned when the p_tree parameter is NULL:
if(p_tree=NULL)
{
    node*p_new_tree=new node;
    p_new_tree->p_left=NULL;
    p_new_tree->p_right=NULL;
    // This is missing:
    p_new_tree->data=data;
    return p_new_tree;
}



在其他情况下, insert 函数不返回任何值。这应该引发编译器警告。



在第一次调用中,你传递的是一个可以引发编译器警告的unitialised参数:


In the other cases no value is returned by the insert function. This should raise a compiler warning.

In your first call you are passing an unitialised parameter which should raise a compiler warning too:

node*p_temp;
insert(p_temp,4);



p_temp 不是 NULL 在这里,程序崩溃(访问冲突)。当它是 NULL 时,会分配一个新节点但是没有存储以供以后使用(不使用返回值)。



总的来说,你应该重新考虑你的实施。它不会这样。


When p_temp is not NULL here, the program crashes (access violation). When it is NULL, a new node is allocated but nowhere stored to be later used (return value not used).

Overall you should rethink your implementation. It would not work this way.


这篇关于我的二进制树的C ++代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆