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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:07:17+00:00 2026-06-05T06:07:17+00:00

I wanna make pop function to delete the node and subtree of the node.

  • 0

I wanna make pop function to delete the node and subtree of the node. Here is my code

void pop(struct data *node,int num)
{
    if(node)
    {
        if(node->num==num)
        {
            pop(node->left,num);
            pop(node->right,num);
            free(node);
            node=NULL;
        }
        else
        {
            if(num> node->num)
                pop(node->right,num);
            else if (num< node->num)
                pop(node->left,num);
        }
    }
}
void pre(struct data *node)
{
    if(node)
    {
        printf("%d ",node->num);
        pre(node->left);
        pre(node->right);
    }
}
void main()
{
    push(&root,37);
    push(&root,20);
    push(&root,45);
    push(&root,5);
    push(&root,15);
    push(&root,40);
    push(&root,50);
    pre(root);
    pop(root,5);
    pre(root);
    getchar();
}

Pre function works well before I use pop. But after I used the pop function, it’s break. Could anyone knows where’s the mistake?

  • 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-05T06:07:18+00:00Added an answer on June 5, 2026 at 6:07 am

    In pop, you’re doing: node=NULL; — but this only affects the copy of the pointer that was passed to the function, not the pointer in the original tree. Your tree retains the pointer to the data you’ve now freed. The next time you do much with the tree, you try to dereference that pointer, and things fall down and go boom (at least you hope they do — even worse, sometimes they might seem to work).

    One way to fix this is to pass a double pointer to pop:

    void pop(struct data **node, int num) { 
    
        if ((*node)->num == num)
            // ...
            free(*node);
            *node = NULL;
        }
    }
    

    Now you’re changing the pointer in the tree instead of changing the copy of it your function received.

    This still won’t work quite right though — you’re depending on pop(child, num); to destroy the sub-trees of the current node, but unless their num is set to the same value, they won’t delete anything, just travel down the tree looking for a node with a matching num.

    You probably want one function to walk the tree finding the node you care about, then a second one that walks the tree starting from a designated node, and (unconditionally) destroys that node and its sub-trees.

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

Sidebar

Related Questions

Got a chart control i wanna make from a data table. the table looks
I wanna make a program automatically tagger the text in a directory. Here's my
I wanna make the left hand border of the datagridview object disappear. Any suggestions?
I wanna make a small code block about c #. first think a listbox
I wanna make a function that erases everything inside of a multiple select using
I wanna make option edit product when user login. Here is the link when
I have lots of data in database and I wanna make a statistics from
im new in android programming.. i have a problem here. i wanna make an
i wanna make my own application to reach my mails. i'm using office 365
is there any equivalent to BN_hex2bn in C# , I wanna make BigInteger from

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.