Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. class Node {
  2. constructor(val) {
  3. this._val = val;
  4. this._parent = null;
  5. this._children = [];
  6. }
  7.  
  8. const isRoot = () => isValid(this._parent); // TODO: implement this
  9. const getchildren = () => this._children;
  10. const hasChildren = () => this._children.length > 0;
  11. const getvalue = () => this._val;
  12. const setvalue = (val) => this._val = val;
  13. const append = (child) => {
  14. child._parent = this;
  15. this._children.push(child);
  16. return this;
  17. }
  18. const toString = () => `Node (val: ${this._val}, children: ${this._children.length})`;
  19. }
  20.  
  21. class Tree {
  22. constructor(root) {
  23. this._root = root;
  24. }
  25.  
  26.  
  27. static map(node, fn, tree = null) {
  28. node.value = fn(node.value);
  29. if(tree === null) {
  30. tree = new Tree(node);
  31. }
  32.  
  33. if(node.hasChildren()) {
  34. _.map(node.children, function (child {
  35. Tree.map(child, fn, tree);
  36. }));
  37. }
  38. return tree;
  39. }
  40.  
  41. const getroot = () => this._root;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement