Thread: Free Binary tree

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    22

    Free Binary tree

    Greetings.

    I seek assistance with the data structure binary tree. Following is a brief description of the issue:

    A binary tree is created with nodes of the tree being a struct containing a pointer each for the left & right branches. I have sucessfully achieved the creation. I am asked to now delete the tree one node at a time by deallocating the memory for each of the nodes using free().

    Following code represents my attempt:

    Code:
    void freeTree(node *root){               //Tree's root note is passed as argument
    	while(1){		
    		if(root == NULL){
    			break;
    		}
    		else if(root != NULL && root->left != NULL){
    			freeTree(root->left);
    		}
    		else if(root != NULL && root->right != NULL){
    			freeTree(root->right);
    		}
    		else{ 
                            free(root);
    			return;
    		}
    	}
    }

    A successful compilation results. However, on attempting execute the code, the following error is generated:

    Code:
             12 [sig] test_st1 7016 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
    Segmentation fault (core dumped)

    Would greatly appreciate assistance with addressing the issue.

    Look forward to a prompt response.

    Best regards,
    wirefree101

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Memloop already answered your problem, in this post:

    Free Binary tree

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read a txt file into an array
    By Hitsugaya_KK in forum C Programming
    Replies: 49
    Last Post: 08-22-2009, 02:22 PM
  2. best STL method to implement a binary tree
    By MatthewDoucette in forum C++ Programming
    Replies: 8
    Last Post: 06-16-2006, 07:08 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. binary tree node structure
    By Kirsten in forum C Programming
    Replies: 2
    Last Post: 04-26-2002, 08:02 PM