feat: implement backend for import/export

Related to #2

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-05-15 18:15:37 +02:00
parent c7c27300f6
commit 0203318276
Signed by: mfocko
GPG key ID: 7C47D46246790496

34
node.js
View file

@ -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;