Thread: HELP! :linked list from tree leaf nodes

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Question HELP! :linked list from tree leaf nodes

    hii , I'll appreciate it if someone please help me with this problem
    I want to create a linked-list from the leaves of a tree ordered from right to left .
    without using a Static or global variable

    typedef struct t_node {
    int data;
    struct t_node *left;
    struct t_node *right;
    } T_NODE;


    typedef struct list_node
    {
    int data;
    struct list_node *next;
    } L_NODE;

    the function header : L_NODE* leavesRightToLeft (T_NODE* root, 1 more variable )
    for example :
    Code:
                                      3
                                    /    \
                                   2      5
                                   \    /  \
                                    4   7   9
                                            /
                                           1
    and the function will return a pointer to linked list that will be as following :
    1 -> 7 -> 4

    I've tried to solve it but i have a problem with the pointers, the idea works, but can you help me with the pointers please
    or you can just give me another solution ?
    Code:
    Lnode *leavesRightToLeft(Tree *root,Lnode *last)
    {
    	Lnode *p,*p2,*lastleft,*lastright;
    	if ( ( root->left==NULL) && (root->right==NULL) )
    	{
    		p= (Lnode*) malloc (sizeof(Lnode));
    		p->next=NULL;
    		p->data=root->value;
    		last=p;
    		return p;
    	}
    	else if ( !root->left )
    	{
    		return leavesRightToLeft(root->right,last);
    	}
    	else if ( !root->right )
    	{
    		return leavesRightToLeft(root->left,last);
    	}    
    	else
    	{
    		p=leavesRightToLeft(root->right,lastright);
    		p2=leavesRightToLeft(root->left,lastleft);
    		lastright->next=p2;
    		last=lastleft;
    		return p;
    	}
    }
    Thanks in advanced.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    I've added some comments and hi-lighting that may help (this is just first glance)

    Code:
    Lnode *leavesRightToLeft(Tree *root,Lnode *last)
    {
    	Lnode *p,*p2,*lastleft,*lastright;
            /* as function is recusive, should definitely check for root == NULL also */
    	if ( ( root->left==NULL) && (root->right==NULL) )
    	{
    		p= (Lnode*) malloc (sizeof(Lnode));
    		p->next=NULL;
    		p->data=root->value;
    		last=p;
    		return p;
    	}
            /* !root->left doesn't necessarily mean root->right != NULL */
            /* this should probably be: else if (!root->left && root->right) */
    	else if ( !root->left ) 
    	{
    		return leavesRightToLeft(root->right,last);
    	}
            /* same goes with this one */
    	else if ( !root->right )
    	{
    		return leavesRightToLeft(root->left,last);
    	}    
    	else
    	{
    		p=leavesRightToLeft(root->right,lastright);
    		p2=leavesRightToLeft(root->left,lastleft);
    		lastright->next=p2;
    		last=lastleft;
    		return p;
    	}
    }

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    See the code in the post right at the very bottom here:
    binary tree to linked list - TDWTF Forums
    It efficiently turns a tree into a list in-place, i.e. it reuses the left pointers as previous node pointers, and the right pointers as next node pointers.
    Too cool huh!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. building a linked list from tree leaf nodes
    By ayman88 in forum C Programming
    Replies: 0
    Last Post: 02-27-2010, 04:24 PM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM