I want to save a tree structure to a text file, I then want to reconstruct the file by reading the text file. It will be used for a file tree that should look something like this:
rootnode
|-dir 1
| |-file 1
| |-file 2
|
|-dir 2
|
|-dir 3
|-file 3.1
|
|-dir 3.1
|-fileName
This is a traversal that I have:
Tree.prototype.traversalDF = function(callBack) {
(function depth(currentNode) {
for (var i=0, length = currentNode.children.length; i < length; i++) {
depth(currentNode.children[i]);
}
callBack(currentNode);
})(this._root);
};
this is how it is called:
tree.traversalDF(function(node){
console.log(node.parent);
fs.appendFile(path.join(__dirname, '/testTree.txt'), node+ '\n', 'utf8');
})
this only saves this '[object, object]' the same number of times there are nodes. But I want the data to be saved.
This is the node and the tree properties:
//Every Node will have these properties
function Node(data) {
this.data = data;
this.children = [];
};
function Tree(data) {
//this creates an instance of a node with data passed
var node = new Node(data);
//allows access to the properties of node
this._root = node;
};
This is the saved data How could it be reconstructed:
{"data":"2.2","children":[]}
{"data":"2.1","children":[]}
{"data":"2","children":[{"data":"2.1","children":[]},{"data":"2.2","children":[]}]}
{"data":"lemons","children":[]}
{"data":"4.1","children":[{"data":"lemons","children":[]}]}
{"data":"lemons2","children":[]}
{"data":"5.11","children":[{"data":"lemons2","children":[]}]}
{"data":"4","children":[{"data":"4.1","children":[{"data":"lemons","children":[]}]},{"data":"5.11","children":[{"data":"lemons2","children":[]}]}]}
{"data":"one","children":[{"data":"2","children":[{"data":"2.1","children":[]},{"data":"2.2","children":[]}]},{"data":"4","children":[{"data":"4.1","children":[{"data":"lemons","children":[]}]},{"data":"5.11","children":[{"data":"lemons2","children":[]}]}]}]}