插入树的代码不适用于空树吗? [英] Does this code to insert to a tree not work for empty trees?

查看:57
本文介绍了插入树的代码不适用于空树吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读跳转到c ++,其中有一些代码插入到树中:



I am reading jumping into c++ and in it there is this code that is made to insert into a tree:

node* insert (node *p_tree, int key)
{
// base case--we have reached an empty tree and need to insert our new
// node here
if ( p_tree == NULL )
{
node* p_new_tree = new node;
p_new_tree->p_left = NULL;
p_new_tree->p_right = NULL;
p_new_tree->key_value = key;
return p_new_tree;
}
// decide whether to insert into the left subtree of the right subtree
// depending on the value of the node
if( key < p_tree->key_value )
{
// build a new tree based on p_tree->left by adding the key. Then
// replace the existing p_tree->left pointer with a pointer
// to the new tree. We need to set the p_tree->p_left pointer
// in case p_tree->left is NULL. (If it is not NULL,
// p_tree->p_left won't actually change but it doesn’t hurt to
// set it.)
p_tree->p_left = insert( p_tree->p_left, key );
}
else
{
// Insertion into the right is exactly symmetric to insertion
// into the left
p_tree->p_right = insert( p_tree->p_right, key );
}
return p_tree;





我的问题是,如果你有一棵空树,这会不起作用对于一棵空树?所以,如果你只有一个项目指向null而你试图插入它,你只需创建一个名为p_new_tree的变量吧?



我是什么尝试过:



没有.......................... .....................



My question is, if you have an empty tree, will this not work for an empty tree? So if you just have one item pointing to null and you try to insert to it, you will just be creating a variable called p_new_tree right?

What I have tried:

nothing...............................................

推荐答案

引用:

这个插入树的代码是否适用于空树?

Does this code to insert to a tree not work for empty trees?



首先:阅读代码,特别是评论。


First: read the code and particularly comments.

// base case--we have reached an empty tree and need to insert our new
// node here



秒:有一种简单的方法可以知道答案,制作一个测试程序,使用NULL树调用例程并查看它是否有效。

第三:帮助您了解此代码的作用及其工作原理,请使用调试器a nd观看它。



你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么,你的任务是与之比较它应该做什么。

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,只是通过向你展示什么是帮助你继续当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

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

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

-----

学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。


second: there is an easy way to know the answer, make a testing program that call the routine with a NULL tree and see if it works.
third: To help you understand what this code does and how it work, use the debugger and watch it.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.

node* insert (node *p_tree, int key)
{
    // base case--we have reached an empty tree and need to insert our new
    // node here
    if ( p_tree == NULL )
    {
        node* p_new_tree = new node;
        p_new_tree->p_left = NULL;
        p_new_tree->p_right = NULL;
        p_new_tree->key_value = key;
        return p_new_tree;
    }
    // decide whether to insert into the left subtree of the right subtree
    // depending on the value of the node
    if( key < p_tree->key_value )
    {
        // build a new tree based on p_tree->left by adding the key. Then
        // replace the existing p_tree->left pointer with a pointer
        // to the new tree. We need to set the p_tree->p_left pointer
        // in case p_tree->left is NULL. (If it is not NULL,
        // p_tree->p_left won't actually change but it doesn’t hurt to
        // set it.)
        p_tree->p_left = insert( p_tree->p_left, key );
    }
    else
    {
        // Insertion into the right is exactly symmetric to insertion
        // into the left
        p_tree->p_right = insert( p_tree->p_right, key );
    }
    return p_tree;



专业程序员的编辑有这个功能和其他的例如括号匹配和语法高亮。

Notepad ++ Home [ ^ ]

ultraedit [ ^ ]

这篇关于插入树的代码不适用于空树吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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