var Tree = function (value) {
this.value = value;
this.children = [];
};
Tree.prototype.map = function (callback) {
let newTree = new Tree(callback(this.value));
function makeNewChildRecursive(originTree, newTree) {
if (originTree.children.length < 1) {
return;
}
for (let originChild of originTree.children) {
let newChild = new Tree(callback(originChild.value));
newTree.addChild(newChild);
makeNewChildRecursive(originChild, newChild);
}
}
makeNewChildRecursive(this, newTree)
return newTree;
};
Tree.prototype.mapInPlace = function (callback) {
this.value = callback(this.value);
function changeChildRecursive(tree) {
if (tree.children.length < 1) {
return;
}
for (let child of tree.children) {
child.value = callback(child.value);
changeChildRecursive(child);
}
}
changeChildRecursive(this)
return this;
};
Tree.prototype.addChild = function (child) {
child = new Tree(child);
this.children.push(child);
return child;
};