* Find a node, returning the path to it * With the last element being the node itself
(id: T)
| 533 | * With the last element being the node itself |
| 534 | */ |
| 535 | find(id: T): T[] { |
| 536 | // We need to recursively find the node |
| 537 | function findNode(nodes: TreeNode<T>[], path: T[]): T[] { |
| 538 | for (const node of nodes) { |
| 539 | if (node.value === id) { |
| 540 | return [...path, id]; |
| 541 | } |
| 542 | const result = findNode(node.children, [...path, node.value]); |
| 543 | if (result.length > 0) { |
| 544 | return result; |
| 545 | } |
| 546 | } |
| 547 | return []; |
| 548 | } |
| 549 | |
| 550 | return findNode(this.nodes, []); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Split the tree into two trees |
no outgoing calls