# Can you give me some hints?

Printable View

• 12-03-2005
mag_chan
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
• 12-04-2005
quzah
Which part are you having trouble with specifically?

Quzah.
• 12-04-2005
mag_chan
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); }```
• 12-04-2005
dwks
Where's maketree()?
• 12-04-2005
mag_chan
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); }```
• 12-05-2005
quzah
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.
• 12-05-2005
dwks
You forgot to mention the casting of the malloc() clone.