c++ overloaded operator for >>

I am trying to implement the overloaded operator for output <<
i have a function to print out preorder recursion like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <typename T>
void BST<T>::preorder_recursive(ostream &out, BSTNode *current) const
{
    if (current != NULL)
        return;
    out << "preorder level: " << current->data;
    preorder_recursive(out, current->left);
    preorder_recursive(out, current->right);
}

template <typename T>
ostream &operator << (std::ostream &ostr,const BST<T> &bs)
{
    ostr << * (bs.recursive_preorder(cout));
    return ostr;
}

here is overloaded function. As you can guess, it will try to print out the binary tree but it doesn't do so. please help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <typename T>
// void BST<T>::preorder_recursive(ostream &out, BSTNode *current) const
std::ostream& BST<T>::preorder_recursive( std::ostream &out, BSTNode *current ) const
{
    //if (current != NULL)
        // return;
    if( current == nullptr ) return out ;
       
    // out << "preorder level: " << current->data;
    out << current->data << ' ' ;
    
    preorder_recursive(out, current->left);
    // preorder_recursive(out, current->right);
    return preorder_recursive(out, current->right);
}

template <typename T>
ostream &operator << (std::ostream &ostr,const BST<T> &bs)
{
    // ostr << * (bs.recursive_preorder(cout));
    // return ostr;
    return bs.preorder_recursive(ostr) ;
}
thank you jlborges. it works
Topic archived. No new replies allowed.