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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:59:12+00:00 2026-06-02T19:59:12+00:00

I am creating a binary tree from a bitstring in c. ie 1100100 creates

  • 0

I am creating a binary tree from a bitstring in c. ie 1100100 creates a tree:

  1
 / \
1   1

I decided to use a recursive function to build this tree however i keep getting the error
Debug assertion failed…
Expression : CrtIsValidHeapPointer(pUserData)

here is a fragment of my code

typedef
struct Node {
  char key;
  struct Node *left;
  struct Node *right; 
} Node;

char string[1000];
int i = 0;

void insertRecursivePreorder(Node **node)
{
    Node* parent = *node;
    if(string[i] == '0')
    {
        parent = NULL;
        i++;
    }
    else
    {
        Node *newn = (Node*)malloc(sizeof(Node));
        newn->key = string[i];
        parent = newn;
        i++;
        insertRecursivePreorder(&newn->left); //errors occur here
        insertRecursivePreorder(&newn->right); //errors occur here
        free(newn);
        free(parent);
    }
}

int main(void)
{
    void printTree(Node* node);
    Node* root = NULL;
    scanf("%s", string);
    insertRecursivePreorder(&root);
    //... do other junk
}

i was wondering why this error comes about and what i can do to fix it.

  • 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-02T19:59:18+00:00Added an answer on June 2, 2026 at 7:59 pm

    The immediate problem is likely to be calling free on a pointer twice. In insertRecursivePreorder, you set parent to newn, and then call free on both. As an example of this, the following program fails (but works if you comment out one of the free(..)s):

    #include <stdlib.h>
    int main() {
      int *a = malloc(sizeof(int)),
          *b = a;
      free(a);
      free(b);
      return 0;
    }
    

    However, there are several problems with your logic here. You should only call free when you have completely finished with the pointer, so if you are using your tree later you can’t free it as you construct it. You should create a second function, recursiveDestroyTree, that goes through and calls free on the tree (from the bottom up!).

    And, you probably want *node = newn rather than parent = newn, since the latter is the only one that actually modifies node.

    (You could also change your function to return a Node * pointer, and then just go:

    root = insertRecursivePreorder();
    

    and

    newn->left = insertRecursivePreorder();
    newn->right = insertRecursivePreorder();
    

    instead of trying to keep track of pointers to pointers etc.)

    (Furthermore, on a stylistic point, using global variables is often bad practice, so you could have your insertRecursivePreorder take int i and char * string parameters and use them instead of global variables.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=
well i was creating a binary search tree using this code..as far as i
I'm currently having trouble creating an image from a binary string of data in
Let's say I'm creating a class for a binary tree, BT , and I
I am creating a PDF file from raw binary data and it's working perfectly
Given this binary tree (actually, the binary tree can be random and dynamic, this
In a binary search tree that takes a simple object.....when creating the getter and
I'm creating an Erlang application that needs to parse a binary TCP stream from
I am creating a binary search tree using Nodes with MovieInfo objects as keys.
I'm defining a binary search tree class like this: template <class T> class BSTNode

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.