Consider the following code:
class Node {
constructor(data = null, parent = null) {
this.data = data;
this.parent = parent;
this.children = [];
}
appendChild(data) {
this.children.push(new Node(data, this.root));
return this.root;
}
toString() {
return this.data;
}
}
class NTree {
constructor(data) {
this.root = null;
}
addRoot(data) {
this.root = new Node(data);
}
find(data) {
if (this.root.data === data) {
return this.root;
}
}
appendChild(data) {
this.root.appendChild(data);
}
toString() {
console.log(this.root);
}
}
const t = new NTree();
t.addRoot(1);
t.appendChild(2);
t.appendChild(3);
t.appendChild(4);
console.log(t);
The outputs looks as follows:
NTree {
root: Node { data: 1, parent: null, children: [ [Node], [Node], [Node] ] }
}
How can I convert the above output to this:
NTree {
root: Node { data: 1, parent: null, children: [ 2, 3, 4 ] }
}