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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:11:52+00:00 2026-06-16T02:11:52+00:00

I am trying to make a binary search tree, but when I try to

  • 0

I am trying to make a binary search tree, but when I try to insert any value, or more precisely when a NULL pointer gets passed to the function, it just freezes for a little while and then it crashes. Here’s the code:

void create(int co, struct node **leaf){
 if(*leaf==0){
   (*leaf)=malloc(sizeof(**leaf));
   (*leaf)->val=co;
   (*leaf)->left=0;
   (*leaf)->right=0;
   }
 else if(co<(*leaf)->val){
   create(co, &(*leaf)->left);
   }
 else if(co>=(*leaf)->val){
   create(co, &(*leaf)->right);
   }
}

I can’t figure out why it does that. Can you explain?

EDIT: The first call of the function looks like this:

struct node *root;
 root=0;
 for(i=0;i<c;i++){
   create(f[i], &root);
   }

Where c is the number of elements in the array. And this is the definition of the struct:

struct node{
        int val;
        struct node *left;
        struct node *right;
        };

So the problem is not in the code I posted here, the entire code can be found here If I should just rewrite the whole question and just post the whole code here please saz so in the commnets, will try to rectify ASAP.

FOUND MY ANSWER
After I got it to actually get past create safely, I was able to find the last one mistake that screwed up my program. It was *i++;. Apparently ++ doesn’t work well with values being pointed to. After I rewrote it to *i=*i+1; it finally works, so I’ like to thank all the people who helped me and ask one final question: What is the difference between *i++; and *i=i+1;?

  • 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-16T02:11:53+00:00Added an answer on June 16, 2026 at 2:11 am

    I took your struct definition and insert function exactly as they were and took your other code and dumped into a main() function as such:

    int main()
    {
        struct node *root;
        int i, c = 10;
        root=0;
        for(i=0;i<c;i++){
            create(i, &root);
        }
        return 0;
    }
    

    Seems it works just fine. I also tried a number of different ordered elements:

    int f[] = {6, 1, 9, 2, 0, 18, 2, -8, 10000, 5};
    

    And again, no crashes and I get the correct ordering…

    Did you verify that c, which you use in the condition: i<c has f[] number of elements? You could remove the c by just using: sizeof(f)/sizeof(int).

    What input makes this function fail? What is the exact error message it fails with?

    When you “walk” your tree, did you check for NULL before printing values?


    After you posted the whole code, I can see it crashes here:

    int *pole, i, count=3;
    pole[0]=25;   <----
    

    You didn’t give pole any memory, so you’re deferencing an uninitialized pointer.

    pole = malloc(3 * sizeof(int));
    

    fixes that, but there’s more.

    Next you’ll die here:

    void getorder(struct node *leaf, int *f, int *i){
        if(leaf->left!=NULL){
            getorder(leaf->left, f, i);
         }
         f[*i]=leaf->val;  <-- this will kill you
    

    Because again, you don’t give j any memory:

    int *j;
    ...
    *j=0;
    getorder(root, f, j);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a breadth first search function for a binary search tree,
I'm trying to make a list of all items in a binary search tree.
I'm trying to make a divide and conquer version of binary search, but one
I'm trying to make a search method using a binary tree(no, it's not a
I'm trying to make a binary tree in scala and need to make a
I am trying to create a custom Binary Search Tree, and I have everything
i have made an binary tree, and im trying to make an postorder traversal.
I'm trying to write code for a binary search tree, the first method I'm
I'm trying to make the node-mongodb-native driver from git, but when i try and
I am trying to search through a binary search tree and store each node

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.