(search)
| 62 | } |
| 63 | |
| 64 | getPath(search) { |
| 65 | const searchFn = TreeNode.iterFunction(TreeNode.searchFunction(search)); |
| 66 | |
| 67 | const findPath = (currentNode, currentPath) => { |
| 68 | // skip if we already found the result |
| 69 | const attempt = currentPath.concat([currentNode]); |
| 70 | // base case: return the array when there is a match |
| 71 | if (searchFn(currentNode)) { |
| 72 | return attempt; |
| 73 | } |
| 74 | for (let i = 0; i < currentNode.children.length; i++) { |
| 75 | const child = currentNode.children[i]; |
| 76 | const match = findPath(child, attempt); |
| 77 | if (match) { |
| 78 | return match; |
| 79 | } |
| 80 | } |
| 81 | return null; |
| 82 | }; |
| 83 | |
| 84 | return findPath(this, []); |
| 85 | } |
| 86 | |
| 87 | walk(fn, depth = 0) { |
| 88 | const nodeStack = []; |
no test coverage detected