Printing Binary Search Tree To File
I am not sure if I am doing this right. I need to traverse through the BST recursively and not sure why it is creating an empty file?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void BinarySearchTree::save_file(string file)
{
ofstream outFile;
outFile.open(file);
save_file_helper(root, file);
}
void BinarySearchTree::save_file_helper(Node* current, string file)
{
ofstream outFile;
while (outFile) {
if (current == NULL)
return;
else {
save_file_helper(current->left, file);
outFile << current->title << endl;
outFile << current->price << endl;
outFile << current->amount << endl;
outFile << current->isbn << endl;
save_file_helper(current->right, file);
}
}
outFile.close();
}
|
You're not passing the opened file to the helper, just the original filename.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void BinarySearchTree::save_file(string file)
{
ofstream outFile;
outFile.open(file);
save_file_helper(root, outFile);
outFile.close();
}
void BinarySearchTree::save_file_helper(Node* current, ofstream &outFile)
{
if (current == NULL)
return;
else {
save_file_helper(current->left, file);
outFile << current->title << endl;
outFile << current->price << endl;
outFile << current->amount << endl;
outFile << current->isbn << endl;
save_file_helper(current->right, file);
}
}
|
Topic archived. No new replies allowed.