I am trying to read/write binary trees to a file for a little project I'm working on for fun. I got frustrated and gave up; now, I am trying again! So I am writing the tree to file (or, rather, to string for now) like so:
But now I'm completely lost as to recreating the tree from a string. I thought of perhaps using an array/vector/string of 1's and 0's or trues and falses to keep track of where I was in the tree, but it's really beyond me... Any help?
Read back the individual tokens from the string representation and rebuild the tree in the same way. Generally that would involve some sort of insert function.
The best way I can think off (At The Moment) is to have three "states":
1)Add - Just adds element to left child
2)L-NULL - Just added a null node to left child
3)R-NULL - Just added null node to right child
start in state 1 and remain in state 1 until you add null node. If the null node is the left child go to the null node's parent and go back to state 1. If the null node is the right child go the the null node's grandparent and go back to state 1.
I would ditch the vector representation as it seems to be the one causing you problems and implement a tree class that allows you traverse a tree properly (similar to a linked list). Include functions like left(), right(), parent(), grandparent(). Also maybe have a "current" pointer to the current node with in the tree.
I hope what I am saying makes sense, if not just let me know what you are unclear on and I will re-explain.
@cire aren't the two rather equivalent? Creating a tree and reading a tree from a string? Creating it is probably easier though, I guess.
@Script Coder if I got rid of the vector, how do I stop the nodes from being destroyed when the function ends? Also, do you mean the entire tree is one object?
@JLBorges Could you explain the entire node struct, especially line 8 and 10? Also, is auto different in C++11? Because I thought auto defined the scope that the variable should be accessible in. In addition, could you expound on the meaning of const node::pointer& tree? Is the pointer keyword new as well? Or have I (apparently) just not learned of it yet?
Prior to C++11, auto specified automatic storage duration. Of course, objects have this storage duration by default wherever such duration is desired (and it wasn't legal anywhere else), so nobody ever used it.
autoint num ; was entirely equivalent to int num ; in any block scope.
Since C++11, auto is used to automatically declare the type of a variable based on the static (compile-time) type of its initializer.
1 2 3
std::vector<int> v ;
// ...
auto it = v.begin() ; // it is of type std::vector<int>::iterator
In addition, could you expound on the meaning of const node::pointer& tree? Is the pointer keyword new as well?
using pointer = std::shared_ptr<node> ; is equivalent to typedef std::shared_ptr<node> pointer ; where pointer is an alias for the type std::shared_ptr<node>
> Could you explain the entire node struct, especially line 8 and 10?
> could you expound on the meaning of const node::pointer& tree?
Line 8 defines a C++11 type alias.
With struct node { using pointer = std::shared_ptr<node> ; /* ... */ };
we create a type alias; the node::pointer is an alias (another name) for the type std::shared_ptr<node>
In const node::pointer& tree, the type of tree is a reference to const node::pointer ie. a reference to a const shared pointer to node