Hey guys im having trouble traversing a tree. The assignment is to decode morse code. I have the tree built, but its the search im having trouble with.
i can get the search to work if i use recursion, but the problem is when i cout, i of course get all the values that i have searched through. heres the recursion code and what happens.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void Search (char code[], BinaryTree& tree, int count)
{
//BinaryTree temptree;
char node;
if (code[count] == '-')
{
Search (code, tree.getRightSubtree(), count + 1);
}
else if (code[count] == '.')
{
Search (code, tree.getLeftSubtree(), count + 1);
}
node = tree.getRootData();
cout << node << endl;
}
|
if i type in for instance --.-
i will get
Q
G
M
T
'\0' <--- empty space
|
Q is the right output, but im getting all the other values it searched through
i tried a for loop, but the problem then is it doesn't traverse the tree. If i try to go left, it does, but then it resets back to the root, so im basically going in a circle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
for (count = 0; code[count] == '-' || code[count] == '.'; count++)
{
if (code[count] == '-')
{
tree.getRightSubtree();
}
else if (code[count] == '.')
{
tree.getLeftSubtree();
}
}
node = tree.getRootData();
cout << node << endl;
|
so if i entered --- i would simply get 3 empty spaces being '\0' which is my root.
'\0' <---- empty space
'\0' <---- empty space
'\0' <---- empty space
|
So my question is, is there a way to get the last value outputted from a recursive call?
And any way to get it to work using the for loop?