-2

I have the following code for inserting nodes into a tree. The problem is that the code is not working, there is no compilation error, but the output isn't proper. The code is as follows:

#include <stdio.h>

struct node {
    int data;
    node *left;
    node *right;
};

node * insertNode(node *root, int value) {
    if(root == NULL) {
        printf("%s\n", "root is null, making new node");
        node * new_node = new node;
        new_node->data = value;
        new_node->left = NULL;
        new_node->right = NULL;
        root = new_node;
        printf("%s\n", "root assigned to new node");
    }
    else {
        if(root->data < value) {
            printf("%s\n", "Right subtree");
            insertNode(root->right, value);
        } else {
            printf("%s\n", "Left subtree");
            insertNode(root->left, value);
        }
    }
    return root;
}

void printTree(node *root) {
    if(root != NULL) {
    if(root->left != NULL) {
        printTree(root->left);
    }
    printf("%d ", root->data);
    if(root->right != NULL) {
        printTree(root->right);
    }
    }
    else {
        printf("%s\n", "root is null");
        return;
    }
}

int main()
{
    node *root = new node;
    root->data = 1;
    root->left = NULL;
    root->right = NULL;
    root = insertNode(root, 2);
    printTree(root);
    return 0;
}

Where am I going wrong?

praxmon
  • 5,009
  • 22
  • 74
  • 121
  • 2
    You should step through your program with the debugger to find the source of the error. – πάντα ῥεῖ Feb 01 '16 at 08:09
  • Maybe post what the runtime error is? Just writing "there is no compilation error, but a run-time error" doesn't give too much information about your problem. – rbaleksandar Feb 01 '16 at 08:20
  • @rbaleksandar I mean to say, that during compilation I am facing no issues but at run-time the output isn't proper. My bad, it is misleading. – praxmon Feb 01 '16 at 10:09
  • I understood that however again "the output isn't proper" is just beating around the bush. For the future my advice to you is: provide as much information as possible in a as compact form as possible. – rbaleksandar Feb 01 '16 at 11:42
  • why don't you use std::set? – Peter VARGA Feb 01 '16 at 23:07

1 Answers1

2

You forgot to assigne the return value of recursive function insertNode. Change insertNode(root->right, value) to root->right = insertNode(root->right, value); and insertNode(root->left, value) to root->left = insertNode(root->left, value);. The first paramter of your function insertNode is an input paramter only, the output is your return value. Adapt your code like this:

node * insertNode(node *root, int value) {
    if( root == NULL ) {
        printf("%s\n", "root is null, making new node");
        node * new_node = new node;
        new_node->data  = value;
        new_node->left  = NULL;
        new_node->right = NULL;
        root = new_node;
        printf("%s\n", "root assigned to new node");
    }
    else {
        if( root->data < value ) {
            printf("%s\n", "Right subtree");
            root->right = insertNode( root->right, value );
          //            ^ assigne possibly new right node
        } else {
            printf("%s\n", "Left subtree");
            root->left = insertNode( root->left, value );
          //           ^ assigne possibly new left node
        }
    }
    return root;
}

An other solution wold be to change signature of function insertNodeand pass the parameter by pointer:

void insertNode(node **root, int value) {
                 //  ^^ in and output parameter
    if( *root == NULL ) {
        printf("%s\n", "root is null, making new node");
        node * new_node = new node;
        new_node->data  = value;
        new_node->left  = NULL;
        new_node->right = NULL;
        *root = new_node;
        printf("%s\n", "root assigned to new node");
    }
    else {
        if( (*root)->data < value ) {
            printf("%s\n", "Right subtree");
            insertNode( &((*root)->right), value );
        } else {
            printf("%s\n", "Left subtree");
            insertNode( &((*root)->left), value );
        }
    }
}

int main()
{
    node *root  = new node;
    root->data  = 1;
    root->left  = NULL;
    root->right = NULL;
    insertNode( &root, 2 );
             // ^
    printTree(root);
    return 0;
}

Note you never delete your allocated nodes.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174