Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;


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...............................................
Posted
Updated 8-Jun-18 13:56pm

1 solution

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

First: read the code and particularly comments.
C++
// base case--we have reached an empty tree and need to insert our new
// node here

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.
C++
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;

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
Comments
BerthaDusStuf 9-Jun-18 6:32am    
Ok thanks I dont really know how to use the debugger and I plan on learning soon but I understand the code and it works as expected but I just think that it only works in some cases
Patrice T 9-Jun-18 7:16am    
Build test cases code and see if result is ok or not.
BerthaDusStuf 9-Jun-18 16:06pm    
ok ty

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900