[英]Saving a tree data structure to a text file JavaScript
我想將樹形結構保存到文本文件,然后通過讀取文本文件來重建文件。 它將用於應如下所示的文件樹:
rootnode
|-dir 1
| |-file 1
| |-file 2
|
|-dir 2
|
|-dir 3
|-file 3.1
|
|-dir 3.1
|-fileName
這是我的遍歷:
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);
};
這就是所謂的:
tree.traversalDF(function(node){
console.log(node.parent);
fs.appendFile(path.join(__dirname, '/testTree.txt'), node+ '\n', 'utf8');
})
這樣只會將此“ [對象,對象]”保存相同的次數。 但是我想保存數據。
這是節點和樹的屬性:
//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;
};
這是保存的數據,如何重建它:
{"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":[]}]}]}]}
使用JSON.stringify(node)
而不是traversalDF
回調中的僅node
。 實際上,您根本不需要遍歷它。 您應該可以只調用JSON.stringify(obj)
進行序列化。
要反序列化,只需在從文件中讀取它之后使用JSON.parse(/* string */)
。
創建的樹與所需的樹相同,但是它存儲所需的所有數據,這是代碼:
const fs = require('fs');
const path = require('path');
//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;
};
//--------------------------graph traversal code--------------------------------//
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);
};
Tree.prototype.traversalBF = function(node, pathPart) {
//determines number of children of the given node
var length = node.children.length;
var i = 0;
var found = false;
//cycles through until there is a match
while( found != true && i <= length){
if(node.children[i] != null){
if(node.children[i].data == pathPart){
found = true;
//when there is a match it returns the node
return node.children[i];
}
} else if( i == length) {
var nodeFile = new Node(pathPart);
//adds the file name onto the the node
node.children.push(nodeFile);
//sets the node parent to the currentNode
// nodeFile.parent = node;
return nodeFile;
}
i++;
}
}
Tree.prototype.add = function(path){
var pathSplit = path.split('/');
//gets the length of the path
var pathLength = pathSplit.length;
//this compares the path to the nodes/directories
let compare = (currentNode, n) => {
if(n == pathLength -1){
//create a new node with file name as data
var nodeFile = new Node(pathSplit[n]);
//adds the file name onto the the node
currentNode.children.push(nodeFile);
}else{
var newNode = () => this.traversalBF(currentNode, pathSplit[n]);
compare(newNode(), n+1);
};
};
compare(this._root, 1);
};
var tree = new Tree('one');
tree.add('one/2/2.1');
tree.add('one/2/2.2');
tree.add('one/hi');
tree.add('one/4/4.1');
tree.add('one/4/4.1/lemons');
tree.add('one/4/5.11/lemons2');
tree.traversalDF(function(node){
console.log(node.data);
});
//writeFileSyn used instead on appendFile so it overwrites the data in the .txt
//necessary to use 'writeFileSync' otherwise next line attempts to read an empty file
fs.writeFileSync(path.join(__dirname, '/testTree.txt'), JSON.stringify(tree) + '\n', 'utf8');
//reads data from txt file
var treeRecon = JSON.parse(fs.readFileSync(path.join(__dirname, '/testTree.txt')));
//creates a tree
var Reconstructed = new Tree(treeRecon._root.data);
console.log(Reconstructed);
for (i = 0; i < treeRecon._root.children.length; i++) {
//for each child in the root in the children of the root.
//the children are pushed onto the roots children hence recreating it.
Reconstructed._root.children.push(treeRecon._root.children[i]);
}
console.log(Reconstructed);
Reconstructed.traversalDF(function(node){
console.log(node.data);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.