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);
}