Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node {
- constructor(val) {
- this._val = val;
- this._parent = null;
- this._children = [];
- }
- const isRoot = () => isValid(this._parent); // TODO: implement this
- const getchildren = () => this._children;
- const hasChildren = () => this._children.length > 0;
- const getvalue = () => this._val;
- const setvalue = (val) => this._val = val;
- const append = (child) => {
- child._parent = this;
- this._children.push(child);
- return this;
- }
- const toString = () => `Node (val: ${this._val}, children: ${this._children.length})`;
- }
- class Tree {
- constructor(root) {
- this._root = root;
- }
- static map(node, fn, tree = null) {
- node.value = fn(node.value);
- if(tree === null) {
- tree = new Tree(node);
- }
- if(node.hasChildren()) {
- _.map(node.children, function (child {
- Tree.map(child, fn, tree);
- }));
- }
- return tree;
- }
- const getroot = () => this._root;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement