I have an assignment in which I must be able to find all of the ancestors of a node in a general tree and be able to print those ancestors in order from the root down:
the nodes have values that are strings.
I'm having a tough time getting my head around this. All of been able to come up w/ so far is using a multi-dimensional array, finding the values from the node up to the root, storing them in the array and then reversing it.
Not having much luck though. Here's the code I have so far.
// Finds All Of A Node's Ancestors
void tree::FindAncestor(link *findtemp, char key[20])
{
cout << endl;
int foundFlag = 0;
int ctr = 0;
int x = 0;
char temp[500][20];
char reverse[500][20];
Find(findtemp,key,foundFlag ); // Finds value for which ancestors must be found
if (curr == root)
{return;}
cout << curr->value << "'s ancestors are: " << endl;
while (curr->parent->value != NULL)
{
//strcpy(key,curr->parent->value);
strcpy(temp[ctr],curr->parent->value); //input value into array to be reversed later
cout << temp[ctr] << " ";
ctr++;
NextParent();
}
while (x < ctr)
{
strcpy(reverse[(x+ctr)],temp[(ctr-x)]);
x++;
}
cout << endl << endl << temp;
cout << endl << endl << reverse;
}
I have some class specific stuff in there, but you should be able to follow it.
When I print out the stuff at the end I get Hex addresses. Can anyone help? Or maybe tell me a better way of doing this?
Thanks