| 20 | 26 | * @return {Zipper} |
| 21 | 27 | */ |
| 22 | 28 | static fromTree(tree) { |
| 23 | | throw new Error('Remove this line and implement the function'); |
| 29 | // Edge Case: Empty tree |
| 30 | if (tree === null) { |
| 31 | return new Zipper(null, []); |
| 32 | } |
| 33 | return new Zipper(tree, []); |
| 24 | 34 | } |
| 25 | 35 | |
| 26 | 36 | /** |
| 27 | 37 | * @return {BinaryTreeNode} |
| 28 | 38 | */ |
| 29 | 39 | toTree() { |
| 30 | | throw new Error('Remove this line and implement the function'); |
| 40 | // Edge Case: Empty tree |
| 41 | if (this._node === null && this._path.length === 0) { |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | let current = this; |
| 46 | while (current._path.length > 0) { |
| 47 | current = current.up(); |
| 48 | } |
| 49 | return current._node; |
| 31 | 50 | } |
| 32 | 51 | |
| 33 | 52 | /** |
| 34 | 53 | * @return {number} |
| 35 | 54 | */ |
| 36 | 55 | value() { |
| 37 | | throw new Error('Remove this line and implement the function'); |
| 56 | // Edge Case: Focus node is null |
| 57 | if (this._node === null) { |
| 58 | return null; |
| 59 | } |
| 60 | return this._node.value; |
| 38 | 61 | } |
| 39 | 62 | |
| 40 | 63 | /** |
| 41 | 64 | * @return {Zipper | null} |
| 42 | 65 | */ |
| 43 | 66 | left() { |
| 44 | | throw new Error('Remove this line and implement the function'); |
| 67 | // Edge Case: Focus node is null |
| 68 | if (this._node === null) { |
| 69 | return null; |
| 70 | } |
| 71 | // Edge Case: No left child |
| 72 | if (this._node.left === null) { |
| 73 | return null; |
| 74 | } |
| 75 | const newPath = [...this._path, { node: this._node, side: 'left' }]; |
| 76 | return new Zipper(this._node.left, newPath); |
| 45 | 77 | } |
| 46 | 78 | |
| 47 | 79 | /** |
| 48 | 80 | * @return {Zipper | null} |
| 49 | 81 | */ |
| 50 | 82 | right() { |
| 51 | | throw new Error('Remove this line and implement the function'); |
| 83 | // Edge Case: Focus node is null |
| 84 | if (this._node === null) { |
| 85 | return null; |
| 86 | } |
| 87 | // Edge Case: No right child |
| 88 | if (this._node.right === null) { |
| 89 | return null; |
| 90 | } |
| 91 | const newPath = [...this._path, { node: this._node, side: 'right' }]; |
| 92 | return new Zipper(this._node.right, newPath); |
| 52 | 93 | } |
| 53 | 94 | |
| 54 | 95 | /** |
| 55 | 96 | * @return {Zipper | null} |
| 56 | 97 | */ |
| 57 | 98 | up() { |
| 58 | | throw new Error('Remove this line and implement the function'); |
| 99 | // Edge Case: Already at root (no parent) |
| 100 | if (this._path.length === 0) { |
| 101 | return null; |
| 102 | } |
| 103 | const newPath = [...this._path]; |
| 104 | const parentEntry = newPath.pop(); |
| 105 | const parent = parentEntry.node; |
| 106 | return new Zipper(parent, newPath); |
| 59 | 107 | } |
| 60 | 108 | |
| 61 | 109 | /** |
| 62 | 110 | * @param {number} value |
| 63 | 111 | * @return {Zipper} |
| 64 | 112 | */ |
| 65 | | setValue(value) { |
| 66 | | throw new Error('Remove this line and implement the function'); |
| 113 | set_value(value) { |
| 114 | // Edge Case: Focus node is null |
| 115 | if (this._node === null) { |
| 116 | throw new Error('Cannot set value of null node'); |
| 117 | } |
| 118 | const newNode = { ...this._node, value }; |
| 119 | return this._rebuildWithNewNode(newNode); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Helper method to rebuild the tree with a new node at the current focus |
| 124 | * @param {BinaryTreeNode} newNode |
| 125 | * @return {Zipper} |
| 126 | * @private |
| 127 | */ |
| 128 | _rebuildWithNewNode(newNode) { |
| 129 | if (this._path.length === 0) { |
| 130 | return new Zipper(newNode, []); |
| 131 | } |
| 132 | |
| 133 | const currentPath = [...this._path]; |
| 134 | let rebuiltTree = newNode; |
| 135 | |
| 136 | // Rebuild the tree from bottom up |
| 137 | for (let i = currentPath.length - 1; i >= 0; i--) { |
| 138 | const entry = currentPath[i]; |
| 139 | const parent = { ...entry.node }; |
| 140 | |
| 141 | if (entry.side === 'left') { |
| 142 | parent.left = rebuiltTree; |
| 143 | } else { |
| 144 | parent.right = rebuiltTree; |
| 145 | } |
| 146 | |
| 147 | rebuiltTree = parent; |
| 148 | } |
| 149 | |
| 150 | // Return a zipper focused on the new node, but we need to navigate to it |
| 151 | // from the root of the rebuilt tree |
| 152 | const rootZipper = new Zipper(rebuiltTree, []); |
| 153 | return this._navigateToNode(rootZipper, newNode); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Navigate to a specific node in the tree |
| 158 | * @param {Zipper} rootZipper |
| 159 | * @param {BinaryTreeNode} targetNode |
| 160 | * @return {Zipper} |
| 161 | * @private |
| 162 | */ |
| 163 | _navigateToNode(rootZipper, targetNode) { |
| 164 | // Simple navigation by value (for now) |
| 165 | // This is a simplified approach - in a real implementation we'd need better tracking |
| 166 | if (rootZipper._node === targetNode) { |
| 167 | return rootZipper; |
| 168 | } |
| 169 | |
| 170 | // Try left subtree |
| 171 | if (rootZipper._node.left) { |
| 172 | const leftZipper = rootZipper.left(); |
| 173 | if (leftZipper && leftZipper._node === targetNode) { |
| 174 | return leftZipper; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Try right subtree |
| 179 | if (rootZipper._node.right) { |
| 180 | const rightZipper = rootZipper.right(); |
| 181 | if (rightZipper && rightZipper._node === targetNode) { |
| 182 | return rightZipper; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // If not found directly, return the root zipper |
| 187 | return rootZipper; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Find a node with a specific value in the tree |
| 192 | * @param {Zipper} rootZipper |
| 193 | * @param {number} targetValue |
| 194 | * @return {Zipper} |
| 195 | * @private |
| 196 | */ |
| 197 | _findNodeInTree(rootZipper, targetValue) { |
| 198 | if (rootZipper._node === null) { |
| 199 | return rootZipper; |
| 200 | } |
| 201 | |
| 202 | if (rootZipper._node.value === targetValue) { |
| 203 | return rootZipper; |
| 204 | } |
| 205 | |
| 206 | // Search left subtree |
| 207 | if (rootZipper._node.left) { |
| 208 | const leftResult = this._findNodeInTree(rootZipper.left(), targetValue); |
| 209 | if (leftResult._node && leftResult._node.value === targetValue) { |
| 210 | return leftResult; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Search right subtree |
| 215 | if (rootZipper._node.right) { |
| 216 | const rightResult = this._findNodeInTree(rootZipper.right(), targetValue); |
| 217 | if (rightResult._node && rightResult._node.value === targetValue) { |
| 218 | return rightResult; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Return root if not found |
| 223 | return rootZipper; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Move to the previous node in in-order traversal |
| 228 | * @return {Zipper | null} |
| 229 | */ |
| 230 | prev() { |
| 231 | // Edge Case: Focus node is null |
| 232 | if (this._node === null) { |
| 233 | return null; |
| 234 | } |
| 235 | |
| 236 | // If there's a left subtree, find the rightmost node in it |
| 237 | if (this._node.left !== null) { |
| 238 | let current = this._node.left; |
| 239 | const newPath = [...this._path, { node: this._node, side: 'left' }]; |
| 240 | |
| 241 | while (current.right !== null) { |
| 242 | newPath.push({ node: current, side: 'right' }); |
| 243 | current = current.right; |
| 244 | } |
| 245 | |
| 246 | return new Zipper(current, newPath); |
| 247 | } |
| 248 | |
| 249 | // Otherwise, go up until we come from a right subtree |
| 250 | let currentPath = [...this._path]; |
| 251 | while (currentPath.length > 0) { |
| 252 | const entry = currentPath[currentPath.length - 1]; |
| 253 | if (entry.side === 'right') { |
| 254 | currentPath.pop(); |
| 255 | return new Zipper(entry.node, currentPath); |
| 256 | } |
| 257 | currentPath.pop(); |
| 258 | } |
| 259 | |
| 260 | // No previous node (we're at the leftmost node) |
| 261 | return null; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Move to the next node in in-order traversal |
| 266 | * @return {Zipper | null} |
| 267 | */ |
| 268 | next() { |
| 269 | // Edge Case: Focus node is null |
| 270 | if (this._node === null) { |
| 271 | return null; |
| 272 | } |
| 273 | |
| 274 | // If there's a right subtree, find the leftmost node in it |
| 275 | if (this._node.right !== null) { |
| 276 | let current = this._node.right; |
| 277 | const newPath = [...this._path, { node: this._node, side: 'right' }]; |
| 278 | |
| 279 | while (current.left !== null) { |
| 280 | newPath.push({ node: current, side: 'left' }); |
| 281 | current = current.left; |
| 282 | } |
| 283 | |
| 284 | return new Zipper(current, newPath); |
| 285 | } |
| 286 | |
| 287 | // Otherwise, go up until we come from a left subtree |
| 288 | let currentPath = [...this._path]; |
| 289 | while (currentPath.length > 0) { |
| 290 | const entry = currentPath[currentPath.length - 1]; |
| 291 | if (entry.side === 'left') { |
| 292 | currentPath.pop(); |
| 293 | return new Zipper(entry.node, currentPath); |
| 294 | } |
| 295 | currentPath.pop(); |
| 296 | } |
| 297 | |
| 298 | // No next node (we're at the rightmost node) |
| 299 | return null; |
| 67 | 300 | } |
| 68 | 301 | |
| 69 | 302 | /** |
| 79 | 317 | * @return {Zipper} |
| 80 | 318 | */ |
| 81 | 319 | setRight(right) { |
| 82 | | throw new Error('Remove this line and implement the function'); |
| 320 | // Edge Case: Focus node is null |
| 321 | if (this._node === null) { |
| 322 | throw new Error('Cannot set right of null node'); |
| 323 | } |
| 324 | const newNode = { ...this._node, right }; |
| 325 | return this._rebuildWithNewNode(newNode); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Insert a new subtree before the focus node (in in-order traversal) |
| 330 | * @param {BinaryTreeNode} subtree |
| 331 | * @return {Zipper} |
| 332 | */ |
| 333 | insert_before(subtree) { |
| 334 | // Edge Case: Focus node is null |
| 335 | if (this._node === null) { |
| 336 | throw new Error('Cannot insert before null node'); |
| 337 | } |
| 338 | |
| 339 | // If current node has no left child, insert as left child |
| 340 | if (this._node.left === null) { |
| 341 | return this.setLeft(subtree); |
| 342 | } |
| 343 | |
| 344 | // Otherwise, find the rightmost node in the left subtree |
| 345 | let current = this._node.left; |
| 346 | const insertPath = []; |
| 347 | |
| 348 | while (current.right !== null) { |
| 349 | insertPath.push({ node: current, side: 'right' }); |
| 350 | current = current.right; |
| 351 | } |
| 352 | |
| 353 | // Insert as right child of the rightmost node in left subtree |
| 354 | const updatedCurrent = { ...current, right: subtree }; |
| 355 | |
| 356 | // Rebuild the path from the insertion point up to the current node |
| 357 | let rebuiltSubtree = updatedCurrent; |
| 358 | for (let i = insertPath.length - 1; i >= 0; i--) { |
| 359 | const entry = insertPath[i]; |
| 360 | const parent = { ...entry.node }; |
| 361 | if (entry.side === 'right') { |
| 362 | parent.right = rebuiltSubtree; |
| 363 | } else { |
| 364 | parent.left = rebuiltSubtree; |
| 365 | } |
| 366 | rebuiltSubtree = parent; |
| 367 | } |
| 368 | |
| 369 | // Now rebuild the current node with the updated left subtree |
| 370 | const updatedCurrentNode = { ...this._node, left: rebuiltSubtree }; |
| 371 | return this._rebuildWithNewNode(updatedCurrentNode); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Insert a new subtree after the focus node (in in-order traversal) |
| 376 | * @param {BinaryTreeNode} subtree |
| 377 | * @return {Zipper} |
| 378 | */ |
| 379 | insert_after(subtree) { |
| 380 | // Edge Case: Focus node is null |
| 381 | if (this._node === null) { |
| 382 | throw new Error('Cannot insert after null node'); |
| 383 | } |
| 384 | |
| 385 | // If current node has no right child, insert as right child |
| 386 | if (this._node.right === null) { |
| 387 | return this.setRight(subtree); |
| 388 | } |
| 389 | |
| 390 | // Otherwise, find the leftmost node in the right subtree |
| 391 | let current = this._node.right; |
| 392 | const insertPath = []; |
| 393 | |
| 394 | while (current.left !== null) { |
| 395 | insertPath.push({ node: current, side: 'left' }); |
| 396 | current = current.left; |
| 397 | } |
| 398 | |
| 399 | // Insert as left child of the leftmost node in right subtree |
| 400 | const updatedCurrent = { ...current, left: subtree }; |
| 401 | |
| 402 | // Rebuild the path from the insertion point up to the current node |
| 403 | let rebuiltSubtree = updatedCurrent; |
| 404 | for (let i = insertPath.length - 1; i >= 0; i--) { |
| 405 | const entry = insertPath[i]; |
| 406 | const parent = { ...entry.node }; |
| 407 | if (entry.side === 'left') { |
| 408 | parent.left = rebuiltSubtree; |
| 409 | } else { |
| 410 | parent.right = rebuiltSubtree; |
| 411 | } |
| 412 | rebuiltSubtree = parent; |
| 413 | } |
| 414 | |
| 415 | // Now rebuild the current node with the updated right subtree |
| 416 | const updatedCurrentNode = { ...this._node, right: rebuiltSubtree }; |
| 417 | return this._rebuildWithNewNode(updatedCurrentNode); |
| 83 | 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Delete the focus node and move focus to next/prev/parent |
| 422 | * @return {Zipper} |
| 423 | */ |
| 424 | delete() { |
| 425 | // Edge Case: Focus node is null |
| 426 | if (this._node === null) { |
| 427 | throw new Error('Cannot delete null node'); |
| 428 | } |
| 429 | |
| 430 | // Case 1: Node has no children (leaf node) |
| 431 | if (this._node.left === null && this._node.right === null) { |
| 432 | if (this._path.length === 0) { |
| 433 | // Deleting the root node |
| 434 | return new Zipper(null, []); |
| 435 | } |
| 436 | |
| 437 | // Remove leaf from parent by rebuilding the tree |
| 438 | const currentPath = [...this._path]; |
| 439 | let rebuiltTree = null; // The leaf node becomes null |
| 440 | |
| 441 | // Rebuild the tree from bottom up |
| 442 | for (let i = currentPath.length - 1; i >= 0; i--) { |
| 443 | const entry = currentPath[i]; |
| 444 | const parent = { ...entry.node }; |
| 445 | |
| 446 | if (entry.side === 'left') { |
| 447 | parent.left = rebuiltTree; |
| 448 | } else { |
| 449 | parent.right = rebuiltTree; |
| 450 | } |
| 451 | |
| 452 | rebuiltTree = parent; |
| 453 | } |
| 454 | |
| 455 | // Return a zipper focused on the parent node in the rebuilt tree |
| 456 | const parentPath = [...this._path]; |
| 457 | parentPath.pop(); |
| 458 | const parentNode = this._path[this._path.length - 1].node; |
| 459 | |
| 460 | // Find the parent node in the rebuilt tree |
| 461 | const rootZipper = new Zipper(rebuiltTree, []); |
| 462 | return this._findNodeInTree(rootZipper, parentNode.value); |
| 463 | } |
| 464 | |
| 465 | // Case 2: Node has only left child |
| 466 | if (this._node.right === null) { |
| 467 | const replacementNode = this._node.left; |
| 468 | return this._rebuildWithNewNode(replacementNode); |
| 469 | } |
| 470 | |
| 471 | // Case 3: Node has only right child |
| 472 | if (this._node.left === null) { |
| 473 | const replacementNode = this._node.right; |
| 474 | return this._rebuildWithNewNode(replacementNode); |
| 475 | } |
| 476 | |
| 477 | // Case 4: Node has two children |
| 478 | // Find the leftmost node in the right subtree (successor) |
| 479 | let successor = this._node.right; |
| 480 | let successorPath = []; |
| 481 | |
| 482 | while (successor.left !== null) { |
| 483 | successorPath.push({ node: successor, side: 'left' }); |
| 484 | successor = successor.left; |
| 485 | } |
| 486 | |
| 487 | // Create new node with successor's value |
| 488 | const newNode = { |
| 489 | value: successor.value, |
| 490 | left: this._node.left, |
| 491 | right: this._node.right |
| 492 | }; |
| 493 | |
| 494 | // Remove the successor from its original position by rebuilding the right subtree |
| 495 | let rebuiltRightSubtree = this._node.right; |
| 496 | |
| 497 | if (successorPath.length === 0) { |
| 498 | // Successor is the direct right child |
| 499 | rebuiltRightSubtree = successor.right; |
| 500 | } else { |
| 501 | // Successor is deeper in the right subtree |
| 502 | // Rebuild the path from successor's parent up to the right child |
| 503 | let current = successor.right; |
| 504 | |
| 505 | for (let i = successorPath.length - 1; i >= 0; i--) { |
| 506 | const entry = successorPath[i]; |
| 507 | const parent = { ...entry.node }; |
| 508 | if (entry.side === 'left') { |
| 509 | parent.left = current; |
| 510 | } else { |
| 511 | parent.right = current; |
| 512 | } |
| 513 | current = parent; |
| 514 | } |
| 515 | |
| 516 | rebuiltRightSubtree = current; |
| 517 | } |
| 518 | |
| 519 | // Create the final new node with the rebuilt right subtree |
| 520 | const finalNewNode = { |
| 521 | value: successor.value, |
| 522 | left: this._node.left, |
| 523 | right: rebuiltRightSubtree |
| 524 | }; |
| 525 | |
| 526 | return this._rebuildWithNewNode(finalNewNode); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | module.exports = { Zipper }; |
| 531 | |
| 532 | // Handled Edge Cases: Empty tree, Focus node is null, No left child, No right child, Already at root (no parent) |
| 84 | 533 | } |