Created
May 3, 2017 17:13
-
-
Save thephilip/4d83e0b28ec9f53bbb2c12c6add1b914 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Nodes & Trees Example | |
class Node { | |
constructor(val) { | |
this._val = val; | |
this._parent = null; | |
this._children = []; | |
} | |
isRoot() { | |
return isValid(this._parent); | |
} | |
get children() { | |
return this._children; | |
} | |
hasChildren() { | |
return this._children.length > 0; | |
} | |
get value() { | |
return this._val; | |
} | |
set value(val) { | |
this._val = val; | |
} | |
append(child) { | |
child._paranet = this; | |
this._children.push(child); | |
return this; | |
} | |
toString() { | |
return `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; | |
} | |
get root() { | |
return this._root; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment