recursive traversals
Sep 25, 2011 at 7:03pm UTC
Hi everyone, I'd like to use a recursive traversal to output a binary search tree with a space between each value - but no space at the end, which is the part I can't figure out.
I have
1 2 3 4 5 6 7 8 9 10
bool BSTree::inorder()
{
if (m_root)
{
recursive_inorder(m_root);
return true ;
}
else
return false ;
}
and
1 2 3 4 5 6 7 8 9
void BSTree::recursive_inorder(BSNode *subroot)
{
if (sub_root != NULL)
{
recursive_inorder(subroot->m_left, output);
cout << subroot->m_data << " " ;
recursive_inorder(subroot->m_right, output);
}
}
Any tips/help would be great
Sep 25, 2011 at 7:35pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void BSTree::recursive_inorder(BSNode *subroot)
{
if (sub_root != NULL)
{
recursive_inorder(subroot->m_left, output);
cout << subroot->m_data;
if (subroot->m_right != NULL)
{
cout << " " ;
}
recursive_inorder(subroot->m_right, output);
}
}
Let see if this helps...
Topic archived. No new replies allowed.