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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:45:52+00:00 2026-06-04T19:45:52+00:00

#include <stdio.h> #include <stdlib.h> typedef struct nod{ int data; struct nod *left,*right; }NOD; NOD

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

typedef struct nod{
    int data;
    struct nod *left,*right;
}NOD;

NOD * generate(NOD * root)
{
    NOD *r,*p;
    int d=-1,value,line,position,i,f,v;
    if(root==NULL)
    {
        do{
            printf("Would you like to create the root node?\n\n1 - yes\n0 - no\n");
            scanf("%d",&d);
            switch(d)
            {
                case 1:
                    printf("Value=");
                    scanf("%d",&value);
                    root=add_root(value);
                    break;
                case 0:
                    return NULL;
                    break;
                default:
                    printf("Command unrecognized!\n");
                    break;
            }
        } while(d==-1);
        if(root!=NULL)
                printf("Root node successfully created!\n");
        else
            printf("Error: could not create root node!\n");
        d=-1;
        do{
            printf("Continue adding nodes?\n\n1 - yes\n0 - no\n");
            scanf("%d",&d);
            switch(d)
            {
                case 1:
                    printf("Insert the line and the position of the node you wish to add (root node has line=0, position=0)\nLine=");
                    scanf("%d",&line);
                    printf("Position ( less or equal with 2^$line-1 )=");
                    scanf("%d",&position);
                    printf("Value=");
                    scanf("%d",&value);
                    r=p=root;
                    for(i=line-1;i=0;i--)  
                    {
                        f=power(2,i);
                        if(position & f == f)   // if the i-(st,nd,rd,th) bit of "position" is 1, then (position & f == f) is true and *r will go right
                        {
                            p=r;
                            r=r->right;
                            v=1;
                        }
                        else
                        {
                            p=r;
                            r=r->left;
                            v=0;
                        }
                    }
                    if(v==0)
                        p=add_left(&r,value);
                    if(v==1)
                        p=add_right(&r,value);

                    break;
                case 0:
                    return root;
                    break;
                default:
                    printf("Command unrecognized!\n");
                    break;
            }
        } while(d==-1);
    }
    else
    {
        ...
    }

NOD * add_left(NOD **p,int value)
{
    NOD * r;
    r=malloc(sizeof(NOD));
    r->data=value;
    r->left=NULL;
    r->right=NULL;
    (*p)->left=r;
    return r;
}

NOD * add_right(NOD **p,int value)
{
    NOD * r;
    r=malloc(sizeof(NOD));
    r->data=value;
    r->left=NULL;
    r->right=NULL;
    (*p)->right=r;
    return r;
}

NOD * add_root(int value)
{
    NOD * x;
    x=malloc(sizeof(NOD));
    x->data=value;
    x->left=NULL;
    x->right=NULL;
    return x;
}

}
int main() {
    NOD *root=NULL;
    root=generate(root);
    return 0;
}

I tried make a program that creates a binary tree but I keep getting SIGSEGV Segmentation fault and I don’t understand why. Can you please tell me what I did wrong?

if(position & f == f)   // if the i-(st,nd,rd,th) bit of "*position*" is 1,
                        // then (position & f == f) is true and *r will go right

What do you think about this part?

  • line is the level of the tree (root has line=0)

  • position is the position of node from left to right (root has position=0)

  • 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-04T19:45:54+00:00Added an answer on June 4, 2026 at 7:45 pm

    You already have bolded a problematic part:

    if(position & f == f) 
    

    == has higher precedence than &, so that is parsed as

    if(position & (f == f))
    

    and is the same as if (position & 1), not what you want.

    Further, you have the wrong loop condition

    for(i=line-1;i=0;i--)
    

    the test should probably be i >= 0, otherwise the loop is never executed (i = 0 is an assignment that evaluates to 0).

    If these are fixed and the loop is executed, after the first iteration r is a nullpointer, then the next loop iteration causes the crash in r = r->right;, or, if the loop iterates only once, add_left(&r,value); (or add_right) dereferences a nullpointer on the penultimate line trying to access its left (resp. right) pointer:

    NOD * add_left(NOD **p,int value)
    {
        NOD * r;
        r=malloc(sizeof(NOD));
        r->data=value;
        r->left=NULL;
        r->right=NULL;
        (*p)->left=r;          // *p == NULL here
        return r;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct dict_pair { void *key; void *value;
#include <stdio.h> #include <stdlib.h> typedef struct { char *Name; int grade; int cost; }Hotel;
people, i've an issue now.. #include <stdio.h> #include <stdlib.h> typedef struct a { int
#include <stdio.h> #include <pthread.h> #include <time.h> #include <stdlib.h> typedef struct pr_struct{ int owner; int
This produces an incompatibility warning: #include <stdlib.h> #include <stdio.h> typedef struct { int key;
#include <stdio.h> #include <stdlib.h> typedef int element; struct cell { element e; struct cell
I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node;
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> typedef struct
I have this snippet of C code: #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct Date {
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct

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.