0

I tried writing a tree program in C, starting with the insert function as shown below:

void insert(int val)
{
    struct node * nn = (struct node *)malloc(sizeof(struct node));
    nn->data = val;
    nn->left = NULL;
    nn->right = NULL;

    if(root == NULL)
    {
        root = nn;
    }

    else
    {
        struct node *ptr = root;

        while(ptr->left != NULL && ptr->right != NULL)
        {
            if(nn->data < ptr->data)
            {
                ptr = ptr->left;
            }

            else
            {
                ptr = ptr->right;
            }
        }

        if(ptr->data < nn->data)
        {
            ptr->right = nn;
        }

        else
        {
            ptr->left = nn;
        }
}

Then I wrote the code for displaying the nodes of the tree so formed:

void display()
{
    struct node *n;
    
    if(root == NULL)
    {
        printf("Tree is Empty\n");
    }

    else
    {
        n = root;
        
        if(n!=NULL)
        {
            printf("%d",n->data);
            display(n->left);
            display(n->right);
        }    
    }
}

This is the main function:

int main()
{
    int ch,val;

    while(ch!=3)
    {
        printf("1. Insert\n2. Display\n3. Exit\nEnter your choice: ");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
            printf("Enter value to be inserted: ");
            scanf("%d",&val);
            insert(val);
            break;

            case 2:
            display();
            break;

            case 3:
            ch=3;
            printf("Exiting....\n");
            break;

            default:
            printf("Invalid choice, please try again\n");
            break;
        }
    }

    return 0;
}

But when I tried executing the display function after inserting a few nodes, it only printed the first node in an infinite loop.

Could someone please help me get around this problem?

5
  • 4
    display(n->left); but display doesn't expect any parameter. Do you mean void display(struct node *root) { ... }? Commented Jun 25, 2020 at 7:41
  • Minor: Don't cast malloc result Commented Jun 25, 2020 at 7:45
  • Read the documentation of your C compiler (e.g. GCC...) and of your debugger (e.g. GDB). Read also Modern C and Introduction to Algorithms Commented Jun 25, 2020 at 8:46
  • 1
    Please post a valid MCVE minimal reproducible example that reflects the code you're running, otherwise we're on a infinite pointless bugchase - namely you're passing stuff to display(...) while in this code it expects no arguments. Commented Jun 25, 2020 at 8:47
  • This is a great opportunity to start learning how to use your debugger. Programming is so much easier with a debugger than without one. Commented Jun 25, 2020 at 10:01

1 Answer 1

2

With each iteration (recursion) you start with root again. That will continue forever, of course.

Your function signature should be

void display(struct node *n)

and then call it in main as:

display(root);

Fixing the display function itself with this feed back I leave to you.


EDIT: the function should be:
void display(struct node *n)
{
    if (n) {
        printf("%d\n",n->data);
        display(n->left);
        display(n->right);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Should be void display(struct node *root), n is already defined in the function, anyway OP talks about infinite recursion, so I assume root is declared at file scope or as global, otherwise the code should not compile.
@DavidRanieri...and the function doesn't need n as a variable but as a parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.