0
votes

I am new here and I would like a small help. I am doing my homework in C++ (using classes), which includes serialization binary search tree into file and deserialization from file. I got stuck.

I´ve programmed the binary search tree and some useful methods for later. But I have problem with serialization the binary search tree into a file. Thanks for all help, advices and ideas !!!

My code:

Tree.h:

#include <fstream> 
using namespace std;

class BinaryTree
{
public:
    BinaryTree();
    ~BinaryTree();

    void Insert(int x);
    void InOrder();
    bool Search(int x);
    void treeToFile(ofstream& file);

private:
    struct node
    {   
        int key;
        node* left;
        node* right;
    };
    node* root;
    void Clear(node* nod);
    void Insert(int x, node*& nod);
    void InOrder(node* nod);
    bool Search(int x, node* nod);
    void treeToFile(node* nod, ofstream& file);
};

Tree.cpp:

#include "Tree.h"
#include<iostream>
#include<fstream>

using namespace std;

BinaryTree::BinaryTree()
{
    root = nullptr;
}

BinaryTree:: ~BinaryTree()
{
    cout << "Clear Binary Tree" << endl;
    Clear(root);
}

void BinaryTree::Clear(node* nod)
{
    if (nod != nullptr)
    {
        Clear(nod->left); 
        Clear(nod->right);
        cout <<"Deleting node: " <<nod->key << endl;
        delete nod;
    }
}

void BinaryTree::Insert(int x)
{
    Insert(x, root);
}

void BinaryTree::Insert(int x, node*& nod)
{
    if (nod == nullptr) 
    {
        nod = new node();
        nod->left = nullptr;
        nod->right = nullptr;
        nod->key = x;
    }
    else
        if (x<nod->key) 
            Insert(x, nod->left);
        else
            if (x>nod->key) 
                Insert(x, nod->right);
}

bool BinaryTree::Search(int x)
{
    return Search(x, root);
}

void BinaryTree::treeToFile(ofstream & file)
{
    return treeToFile(root, file);
}

bool BinaryTree::Search(int x, node* nod)
{
    if (nod == nullptr)
        return false;

    if (nod->key == x)
        return true;

    if (x<nod->key)
        Search(x, nod->left);
    else
        if (x>nod->key)
            Search(x, nod->right);
}

void BinaryTree::InOrder()
{
    cout << "Inorder traversal:" << endl;
    InOrder(root);
}

void BinaryTree::InOrder(node* nod)
{
    if (nod != nullptr)
    {
        InOrder(nod->left);
        cout <<"Node with key: "<<nod->key <<endl;
        InOrder(nod->right);
    }
}
void BinaryTree::treeToFile(node* root, ofstream& file)
{   
    if (!file.is_open()){
        cout << "File cannot be opened" << endl;
    }
    if (root == NULL) {
        file << "#" << ";";
        return;
    }
    file << root->key << ';';
    treeToFile(root->left, file);
    treeToFile(root->right, file);
    file.close();

}

Main. cpp

#include "Tree.h"
#include<iostream>
#include<string>

using namespace std;

int main()
{
    BinaryTree* tree = new BinaryTree();
    tree->Insert(4);
    tree->Insert(42);
    tree->Insert(21);
    tree->Insert(31);
    tree->Insert(14);
    tree->Insert(12);
    tree->Insert(3);
    ofstream output("tree.txt");
    tree->treeToFile(output);


    delete tree;
    return 0;
}

Content of serialized file, when I tried to compiled it: 4;3;#;#;

For serialization I am using preorder traversal as my teacher instructed. But when I tried to store nods of tree in file - it stores only left subtree, and in compiler it says that the file cannot be opened.

Compiler shows this:

Compiler_result

1
Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: How to debug small programs - NathanOliver

1 Answers

0
votes

You are calling treeToFile with a node argument recurisvely but once you end traversing left and right for the first section of the tree you close the file. From there you hit the if branch saying file cannot be opened. You should close the file at the very end of saving, possibly move the close to treeToFile taking ostream not the node and ostream overload.

Also you return on a function call that returns void that is just unnecessary.