Thread: Binary trees question

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Binary trees question

    Code:
     void destroy_tree(struct node *leaf)
    {
      if( leaf != 0 )
      {
          destroy_tree(leaf->left);
          destroy_tree(leaf->right);
          free( leaf );
      }
    }
    Why wouldnt we just free the root. Doesnt it free all the memories that are bound to it ? I may be wrong. I even know I am wrong but , so even we free the root , other memorial places will still be filled?

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    No AFAIK it won't. It will only free the root node.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    No, it doesn't work like that. C has no concept of a "tree". You asked to allocate some memory, C doesn't know/care how you're going to use that memory. If you were to free the root, you'd simply free that one element, leaving all the other elements still allocated and no way to get to them. That's called a memory leak

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Yes. If you do not explicitly free the nodes, they will continue to fill memory. For newer languages, like Java, this is generally not the case.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It looks good to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Salem View Post
    It looks good to me.
    I think the OP was asking *why* you need to go through each node and free it, I'm sure the code is out of a book somewhere.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Thumbs up

    Yes that is true . Besides I dont have any problem with the code. I have seen it somewhere. You have already answered my question.Thanks to all.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    Quote Originally Posted by ozumsafa View Post
    Code:
     void destroy_tree(struct node *leaf)
    {
      if( leaf != 0 )
      {
          destroy_tree(leaf->left);
          destroy_tree(leaf->right);
          free( leaf );
      }
    }
    Why wouldnt we just free the root. Doesnt it free all the memories that are bound to it ? I may be wrong. I even know I am wrong but , so even we free the root , other memorial places will still be filled?
    memory 4 the each node will be allocated separately and if u are freeing only root node,then only node is getting freed and memory 4 the others node is still allocated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary trees
    By studentc in forum C Programming
    Replies: 1
    Last Post: 05-25-2004, 03:48 AM
  2. FunctionType visit and binary search trees
    By MRAB54 in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2004, 05:20 AM
  3. Merging binary search trees
    By ibdutta in forum C Programming
    Replies: 2
    Last Post: 04-16-2004, 07:31 AM
  4. Binary trees
    By JaWiB in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 09:51 AM