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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:01:22+00:00 2026-06-15T19:01:22+00:00

I’m trying to restore memory allocated in a tree by traversing the tree and

  • 0

I’m trying to restore memory allocated in a tree by traversing the tree and deleting the memory as necessary. For example, suppose I have the following tree structure:

struct tree
{
   int *value;
   tree *left;
   tree *right;
}

tree *root; //always points to the root of this tree

I know that we have to visit each value after every recursive call, delete it, then move to the next node (which can be left or right), but the recursive process seems very counter intuitive (particularly the part where we move to the left or move to the right).

I’m trying to follow the rule of “do something with the root, recursively call the left, then recursively call the right,” but the way the code functions is confusing to me. How can I preserve the invariant of root? If someone can perhaps explain the concept pictorially that would be great.

  • 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-15T19:01:23+00:00Added an answer on June 15, 2026 at 7:01 pm

    as you need the tree for traversal during delete the idea is to delete on return

    void del_tree(tree *t) {
    
      if (t->left) {
        // there is a left subtree existing. delete it
        del_tree(t->left);  // first go deeper on left side
        // left branch now completely empty
        delete t->left;     // nothing left behind t->left
        t->left=0;          // just in case
      } else {
        // there is no left subtree existing
        // we are in a leaf or in an unbalanced node
      }
    
      if (t->right) {
        // there is a right subtree existing. delete it
        del_tree(t->right);  // now go deeper on right side
        // right branch now completely empty
        delete t->right;     // nothing left behind t->right
        t->right=0;          // just in case
      } else {
        // there is no rigt subtree existing
        // we are in a leaf or this was an unbalanced node
        // (before we deleted the left subtree)
      }
    
      // both branches are now completely empty
      // either they were from the beginning (leaf)
      // or we have successfully reduced this node to a leaf
      // now do the node visit
    
      if (t->value) {
        delete t->value;     // tidy up
        t->value=0;          // just in case
      }
      // now we are completely clean and empty
      // after return t will be deleted
    }
    
    void main() {
      tree *my_tree;
    
      // stuff
    
      del_tree(my_tree);   // delete the whole tree
      delete my_tree;      // delete the remaining root node
    }
    

    a very important aspect on recursion is when to stop. i assume that a NULL pointer in your struct indicates that there is no subtree.

    the strategy is now to go as deep as possible if (t->left) del_tree(t->left);
    when we reach a NULL pointer on both, left and right we are stranded in a leaf. we now clean the leaf (deleting value) and return. on return delete t->left; is executed, this node has nothing left on the left subtree and continues on its right subtree.

    here i found a nice image of the traversal


    the problem of deleting a tree is divided into 3 parts. deleting the left subtree, deleting the right subtree and cleaning up self. the deleting of a subtree (left or right) is very much the same procedure as deleting the tree itself. so you use the same function, this is called recursion.

    think of deleting a file system structure. you decide for the strategy to delete the ‘left’ folder structure first, then you delete the subtree ‘right’ and finally you delete the file ‘value’. when during the execution of this strategy you change into a folder (no matter whether left or right) you notice that the problem looks the same. so you apply this strategy again to any folder in the tree.

    what happens is, that you change into the directory left repeatedly unless there is no more directory in the current one. you delete the file ‘value’. then you go back one folder and delete the folder named ‘left’. now you look for a folder named ‘right’, change into it, find no folders, delete file ‘value’ and return to the previous folder. you delete the now empty ‘right’ and finally delete the file ‘values as well. next is to do a further return (backtracking). and so on.

    you cannot delete non empty folders during going deeper. you have to delete when retreating.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And

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.