* @memberof TreeNode# * @function cloneTree * * Clones a tree structure deeply. * * For example, for giving a tree structure: * .--(B1)--. * .-(C1) .-(C2)-.----. * (D1) (D2) (D3) (D4) * * Cloning a tree starting from C2 node creates a mirrored tree struc
(nodeTree: TreeNode<T> = this)
| 160 | * @returns {TreeNode} |
| 161 | */ |
| 162 | cloneTree(nodeTree: TreeNode<T> = this): TreeNode<T> { |
| 163 | // The spread of `T extends object` widens to a plain index type, so re-assert it as `T`. |
| 164 | const clonedNode = new TreeNode<T>({ ...nodeTree.data } as T); |
| 165 | |
| 166 | for (let i = 0; i < nodeTree.childs.length; i++) { |
| 167 | clonedNode.addChild(this.cloneTree(nodeTree.childs[i])); |
| 168 | } |
| 169 | |
| 170 | return clonedNode; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Replaces the current node with a passed tree structure. |
no test coverage detected