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)
cout << endl;
int foundFlag = 0;
int ctr = 0;
int x = 0;
Find(findtemp,key,foundFlag ); // Finds value for which ancestors must be found
if (curr == root)
cout << curr->value << "'s ancestors are: " << endl;
while (curr->parent->value != NULL)
strcpy(temp[ctr],curr->parent->value); //input value into array to be reversed later
cout << temp[ctr] << " ";
while (x < ctr)
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?