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?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Purohit Iyer
  • 53
  • 1
  • 4
  • 4
    `display(n->left);` but `display` doesn't expect any parameter. Do you mean `void display(struct node *root) { ... }`? – David Ranieri Jun 25 '20 at 07:41
  • Minor: [Don't cast malloc result](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Ôrel Jun 25 '20 at 07:45
  • Read the documentation of your C compiler (e.g. [GCC](http://gcc.gnu.org/)...) and of your debugger (e.g. [GDB](https://www.gnu.org/software/gdb/)). Read also [*Modern C*](https://modernc.gforge.inria.fr/) and [*Introduction to Algorithms*](https://en.wikipedia.org/wiki/Introduction_to_Algorithms) – Basile Starynkevitch Jun 25 '20 at 08:46
  • 1
    Please post a valid MCVE [mcve] 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. – Shark Jun 25 '20 at 08: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. – Jabberwocky Jun 25 '20 at 10:01

1 Answers1

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);
    }
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 1
    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. – David Ranieri Jun 25 '20 at 08:24
  • @DavidRanieri...and the function doesn't need `n` as a variable but as a parameter. – Paul Ogilvie Jun 25 '20 at 08:39