Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:21:17+00:00 2026-06-07T21:21:17+00:00

I was just trying to write a simple binary search tree program where the

  • 0

I was just trying to write a simple binary search tree program where the user can insert nodes and view all the nodes in the tree in either inorder,preorder or postorder mode. My code is


#include <stdio.h>
#include <stdlib.h>

struct treenode
{
int data;
struct treenode *lchild;
struct treenode *rchild;
}*root;

void insertnode(struct treenode *p,int d)
{
    if(p==NULL)
    {
         // means the tree is empty
         p=(struct treenode *)malloc(sizeof(struct treenode));
         p->data=d;
         p->lchild=NULL;
         p->rchild=NULL;
    }

    else
    {
         // start comparing the new data from root
         if( d<p->data )
             insertnode((&(p->lchild)),d);

        else
             insertnode((&(p->lchild)),d);
    }
}

void preorder(struct treenode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
    }

    else
    {
        printf("%d",p->data);
        preorder(p->lchild);
        preorder(p->rchild);
    }
}

void postorder(struct treenode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
    }

    else
    {
        preorder(p->lchild);
        preorder(p->rchild);
        printf("%d",p->data);
    }
}

void inorder(struct treeode *p)
{
    if(p==NULL)
    {
        printf("\nThe list is empty");
    }

    else
    {
        preorder(p->lchild);
        printf("%d",p->data);
        preorder(p->rchild);
    }
}

int main(void)
{
    root=NULL;
    int choice,data;

    while(1)
    {
         printf("\nPress 1 for inserting a node in BST fashion: ");
         printf("\nPress 2 for traversing the tree in preorder fashion :");
         printf("\nPress 3 for traversing the tree in postorder fashion :");
         printf("\nPress 4 for traversing the tree in inorder fashion :");
         printf("\nPress 5 to exit :");


         printf("\nEnter your choice: ");
         scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("\nEnter the data to be inserted:");
            scanf("%d",&data);
            insertnode(root,data);
            break;

        case 2: preorder(root);
            break;

        case 3: postorder(root);
            break;

        case 4: inorder(root);
            break;

        case 5: exit(0);
            break;

        default: printf("\nYou have enetred an invalid choice. Please try again");
    }
}

return 0;
}

There are bunch of error messages saying


dereferencing pointer to incomplete type

What is the problem ? Also i am not very comfortable with double indirection pointers, so can someone please explain how i can pass and retrieve double indirection pointers ( if i need to pass them at all in the above program).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:21:18+00:00Added an answer on June 7, 2026 at 9:21 pm

    The following are just compilation errors, so fix these and your program will compile.

    Problem #1:
    You define your struct as:

    struct treenode
    {
        /* Blah blah... */
    } *root;  /* Mistake: should be root, not *root */
    

    Instead, name it root, not *root.

    Problem #2:
    You’re calling insertnode() wrong. Instead of this:

    insertnode((&(p->lchild)),d);  /* Mistake: taking the address of the pointer */
    

    You should call it like this:

    insertnode(p->lchild,d);
    

    Problem #3:
    You’re defining root in main() wrong:

    root = NULL;  /* Mistake: root is implicitly declared as int */
    

    What you should do instead is:

    struct treenode* root = NULL;
    

    Problem #4:

    You have a typo in the declaration of inorder():

    void inorder(struct treeode *p)  /* Typo: should be treenode and not treeode */
    

    It should be:

    void inorder(struct treenode *p)
    

    The next set of problems is logical errors in the program:

    Problem #5:
    in insertnode() you’re always inserting a new value d into the left node. You should change either one of the recursive insertnode(p->lchild, d); calls to:

    insertnode(p->rchild, d);
    

    Depending on how you want to organize your tree.

    Problem #6:
    Just like AndersK pointed out, the passed pointer root never changes after it is passed to insertnode(), so that’s a major bug.

    In your case double indirection pointers are necessary when you want to change the passed pointer itself (i.e. point it to another address), and not change the pointee itself.

    You want to change root inside insertnode(), so add another level of indirection and pass the address of root, i.e. &root, so that root can also be changed within the function.

    Correspondingly, the declaration of insertnode() should be:

    insertnode(struct treenode** p, int d)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple program in VC++ which will just initialize the
I'm just starting out writing trying to write a simple program in C and
Possible Duplicate: Binary tree - Dereferencing pointers I was just trying to write a
I'm trying to write (or just find an existing) PHP method that can take
I'm trying to write a simple geometry shader what just passes through vertices before
I am trying to write a simple script in JavaScript, just for testing purposes.
I am trying to write a simple bash script. It just puts out a
I'm trying to write a simple script around Lame to customize the program for
I am trying to write a simple view that renders a single field that
I am just trying to write a simple script that verifies the username and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.