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 8246153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:24:44+00:00 2026-06-07T22:24:44+00:00

Possible Duplicate: Binary tree – Dereferencing pointers I was just trying to write a

  • 0

Possible Duplicate:
Binary tree – Dereferencing pointers

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;
};

void insertnode(struct treenode **n,int d)
{
struct treenode *p=&n;
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
{
    // strat comparing the new data from root
    if( d<p->data )
        insertnode(((p->lchild)),d);

    else
        insertnode(((p->rchild)),d);
}
}

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

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

}

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

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

}

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

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

}

int main(void)
{
struct treenode *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;
}

The program runs successfully, however after i have entered the node, the explorer detects an error and the program stops working. Is there any bug in the code ?

  • 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-07T22:24:45+00:00Added an answer on June 7, 2026 at 10:24 pm

    Spoiler:

    #include <stdio.h>
    #include <stdlib.h>
    
    struct treenode {
        struct treenode *lchild;
        struct treenode *rchild;
        int data;
        } *root = NULL;
    
    void insertnode(struct treenode **pp,int d)
    {
        for( ;*pp; )
        {
             if (d < (*pp)->data) pp = &(*pp)->lchild;
             else  pp = &(*pp)->rchild;
        }
        *pp = malloc (sizeof **pp);
        (*pp)->data = d;
        (*pp)->lchild = NULL;
        (*pp)->rchild = NULL;
    }
    
    void preorder(struct treenode *p)
    {
        if(p==NULL)
        {
            printf("\nThe list is empty");
            return;
        }
    
        printf("%d,",p->data);
        if (p->lchild) preorder(p->lchild);
        if (p->rchild) preorder(p->rchild);
    }
    
    void postorder(struct treenode *p)
    {
        if(p==NULL)
        {
            printf("\nThe list is empty");
            return;
        }
    
        if (p->lchild) preorder(p->lchild);
        if (p->rchild) preorder(p->rchild);
        printf("%d,",p->data);
    }
    void inorder(struct treenode *p)
    {
        if(p==NULL)
        {
            printf("\nThe list is empty");
            return;
        }
    
        if (p->lchild) preorder(p->lchild);
        printf("%d,",p->data);
        if (p->rchild) 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 entered an invalid choice. Please try again");
        }
    }
    
    return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why can't decimal numbers be represented exactly in binary? I am trying
Possible Duplicate: Hex to binary in ruby In Python, I can do the following:
Possible Duplicate: Why can't decimal numbers be represented exactly in binary? I am developing
Possible Duplicate: Why can't decimal numbers be represented exactly in binary? i get very
Possible Duplicate: Post order traversal of binary tree without recursion I was going through
Possible Duplicate: What is The Rule of Three? I just finished my AVL tree
Possible Duplicate: Can I use a binary literal in C or C++? I am
Possible Duplicate: Can I use a binary literal in C or C++? In C
Possible Duplicate: Searching a log file Where I can either enter a binary file
Possible Duplicate: In Java, can I define an integer constant in binary format? In

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.