Thread: Can you give me some hints?

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

    Can you give me some hints?

    The program is to build a node tree , then print it out as follow format, can anyone give me some hints or any reference web site about doing this, thanks.

    (the symbol '+' are spaces)
    output :

    0
    ++1
    +++++11
    ++++++++111
    ++++++++++++1112
    +++++++++++++++++11122 Mary
    +++++++++++++++++11123 Jane
    ++++++++112
    ++++++++++++1121
    +++++++++++++++++11211 John
    +++++12
    ++++++++121
    ++++++++++++1213
    +++++++++++++++++12131 Jack


    Sorry, i 've just post it wrongly in C++ Forum
    Last edited by mag_chan; 12-04-2005 at 02:39 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Which part are you having trouble with specifically?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    INPUT : MARY 11122

    this program is to build a tree (eahc node has 3 sons : left called 1, middle called 2, and right called 3. According to the input of id : 11122. First, build a root node, then build a left node(1). Secondly, build a left node again (1). Thirdly, build a left node (1) again. Forthly, build a middle node (2). Finally build a middle node (2) and save the name 'Mary' to this leaf node. So it match the id : 11122.

    But in the below program , it can just build 2 levels node and how can it save 'Mary' to name during the recursion of BuildTree. Thanks for your help!!

    Code:
    //Build a tree
    NodePtr BuildTree(NodePtr tree,char name[], int id)
    {	NodePtr p, T =tree;
    	int RightDigit;
    	 p = T;
    
    	if ( id < 10 )
    	{	if (id == 1)
    		{	if (p->left == NULL)
    			{	p->left = maketree (name);
    				return (T);
    			}
    			else
    				p = p->left;
    		}			
    		else if (id == 2)
    		{	if (p->middle == NULL)
    			{	p->middle = maketree (name);
    				return (T);
    			}
    			else
    				p=p->middle;
    		}
    		else
    		{	if (p->right == NULL)
    			{	p->right = maketree (name);
    				return (T);
    			}
    			else
    				p=p->right;
    		}
    	}
    	else
    	{	int LeftMostDigit;		
    		LeftMostDigit = getLeftDigit(id); //11122->1
    		
    		while ( LeftMostDigit > 0 )
    		{	if (LeftMostDigit == 1)
    			{	if (p->left == NULL)
    				{	p->left = maketree ("---");
    					p = p->left;
    					break;
    				}
    				else
    					p = p->left;
    			}
    			else if (LeftMostDigit == 2)
    			{	if (p->middle == NULL)
    				{	p->middle = maketree ("---");
    					p = p->middle;
    					break;
    				}
    				else
    					p = p->middle;
    			}			
    			else
    			{	if (p->right == NULL)
    				{	p->right = maketree ("---");
    					p = p->right;
    					break;
    				}
    				else
    					p = p->right;		
    			}
    		}
    	}
    	RightDigit = getRightDigit(id); //11122->1122
    	T = BuildTree(T, name, RightDigit);
    	return (T);
    }

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Where's maketree()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    maketree code as below :

    Code:
    NodePtr maketree(char name[]) //create a new node.
    {	NodePtr p;
    	p= (NodePtr)LoggedMalloc(sizeof(struct node));
    	p->name[SIZE] = name[SIZE];
    	p->right = NULL;
                    p->middle = NULL;
    	p->left = NULL;
    	return(p);
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    p->name[SIZE] = name[SIZE];
    1 - SIZE is actually past the end of the array, if the array has SIZE elements.
    2 - You cannot copy entire arrays this way. What this would do is copy the data in the single element 'name[SIZE]' to the element 'p->name[SIZE]', if in fact you had an element SIZE, which you don't, as noted in point 1.
    3 - If you are trying to copy a string, assuming 'name' is a string, you'll want one of the string functions, such as strcpy.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You forgot to mention the casting of the malloc() clone.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please give ur point of view
    By clover in forum C Programming
    Replies: 2
    Last Post: 05-04-2004, 03:56 PM
  2. Give me some opinions on setting up a server
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-19-2004, 10:38 AM
  3. Can you give me your tip plz :)
    By dionys in forum C Programming
    Replies: 6
    Last Post: 04-11-2004, 11:14 PM
  4. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM
  5. Just to give you an idea of what we're going through...
    By rick barclay in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-13-2001, 02:09 PM