feat: implement backend for import/export
Related to #2 Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
c7c27300f6
commit
0203318276
1 changed files with 34 additions and 0 deletions
34
node.js
34
node.js
|
@ -130,6 +130,40 @@ function nodeMaximum(node) {
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function nodeExport(node) {
|
||||||
|
if (!node) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: node.value,
|
||||||
|
rank: node.rank,
|
||||||
|
left: nodeExport(node.left),
|
||||||
|
right: nodeExport(node.right),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function nodeImport(json) {
|
||||||
|
if (!json) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let node = new Node(json.value);
|
||||||
|
node.rank = json.rank;
|
||||||
|
|
||||||
|
if (json.left) {
|
||||||
|
node.left = nodeImport(json.left);
|
||||||
|
node.left.parent = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (json.right) {
|
||||||
|
node.right = nodeImport(json.right);
|
||||||
|
node.right.parent = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
class Node {
|
class Node {
|
||||||
constructor(value, left, right, parent) {
|
constructor(value, left, right, parent) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
|
Loading…
Reference in a new issue