write to file from binary tree

I have created a binary tree, and as part of the assignment we are supposed to write the tree nodes to a file, I thought modifying the inorder display function would do the trick, but it doesn't. Can anyone help with this. Here's what I have for the function right now:

template <typename T>
void BSearch <T>::write (Node <T> *& root)
{
New.open("New.txt"); //New is a ofstream object private member of the BinaryTree header

if ( root!=0)
{
inorder (root -> left);
New << root -> data;
inorder (root -> right);
}
}
What's 'inorder' ?

The code should be
1
2
3
4
5
6
7
8
9
10
11
12
void write(Node* n){
   fstream New("blah blah blah");
   write_node(n, New);
   New.close();
}

void write_node(Node* n, fstream& file){
   if(!n) return;
   write_node(n->left, file);
   file << n*>data;
   write_node(n->right, file);
}
Topic archived. No new replies allowed.