| Total Complexity | 9 |
| Complexity/F | 1.29 |
| Lines of Code | 62 |
| Function Count | 7 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | class Node { |
||
| 2 | // put your code here to address problems |
||
| 3 | constructor(data = null) { |
||
| 4 | this.data = data; |
||
| 5 | this.children = []; |
||
| 6 | } |
||
| 7 | |||
| 8 | /** |
||
| 9 | * add data as a new child node |
||
| 10 | * @param data |
||
| 11 | */ |
||
| 12 | add(data) { |
||
| 13 | this.children.push(new Node(data)); |
||
| 14 | } |
||
| 15 | |||
| 16 | /** |
||
| 17 | * remove data from children list |
||
| 18 | * @param data |
||
| 19 | */ |
||
| 20 | remove(data) { |
||
| 21 | this.children = this.children.filter(e => { |
||
| 22 | return JSON.stringify(e.data) !== JSON.stringify(data) |
||
| 23 | }); |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | class Tree { |
||
| 28 | // put your code here to address problems |
||
| 29 | constructor() { |
||
| 30 | this.root = null; |
||
| 31 | } |
||
| 32 | /** |
||
| 33 | * breadth first traveral |
||
| 34 | * @param fn : a function |
||
| 35 | */ |
||
| 36 | BFS(fn) { |
||
| 37 | let queue = [this.root]; |
||
| 38 | while (queue.length) { |
||
| 39 | let node = queue.shift(); |
||
| 40 | queue.push(...node.children); |
||
| 41 | fn(node); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * depth first traversal |
||
| 47 | * @param fn : a function |
||
| 48 | */ |
||
| 49 | DFS(fn) { |
||
| 50 | let stack = [this.root]; |
||
| 51 | while (stack.length) { |
||
| 52 | let node = stack.shift(); |
||
| 53 | stack.unshift(...node.children); |
||
| 54 | fn(node); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | module.exports = { |
||
| 60 | Node, |
||
| 61 | Tree |
||
| 62 | }; |