|
| 1 | +import Stack from '../../../data-structures/stack/Stack'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @param {Stack} fromPole |
| 5 | + * @param {Stack} toPole |
| 6 | + * @param {function(disc: number, fromPole: number[], toPole: number[])} moveCallback |
| 7 | + */ |
| 8 | +function moveDisc(fromPole, toPole, moveCallback) { |
| 9 | + moveCallback(fromPole.peek(), fromPole.toArray(), toPole.toArray()); |
| 10 | + |
| 11 | + const disc = fromPole.pop(); |
| 12 | + toPole.push(disc); |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {number} numberOfDiscs |
| 17 | + * @param {Stack} fromPole |
| 18 | + * @param {Stack} withPole |
| 19 | + * @param {Stack} toPole |
| 20 | + * @param {function(disc: number, fromPole: number[], toPole: number[])} moveCallback |
| 21 | + */ |
| 22 | +function hanoiTowerRecursive({ |
| 23 | + numberOfDiscs, |
| 24 | + fromPole, |
| 25 | + withPole, |
| 26 | + toPole, |
| 27 | + moveCallback, |
| 28 | +}) { |
| 29 | + if (numberOfDiscs === 1) { |
| 30 | + // Base case with just one disc. |
| 31 | + moveDisc(fromPole, toPole, moveCallback); |
| 32 | + } else { |
| 33 | + // In case if there are more discs then move them recursively. |
| 34 | + |
| 35 | + // Expose the bottom disc on fromPole stack. |
| 36 | + hanoiTowerRecursive({ |
| 37 | + numberOfDiscs: numberOfDiscs - 1, |
| 38 | + fromPole, |
| 39 | + withPole: toPole, |
| 40 | + toPole: withPole, |
| 41 | + moveCallback, |
| 42 | + }); |
| 43 | + |
| 44 | + // Move the disc that was exposed to its final destination. |
| 45 | + hanoiTowerRecursive({ |
| 46 | + numberOfDiscs: 1, |
| 47 | + fromPole, |
| 48 | + withPole, |
| 49 | + toPole, |
| 50 | + moveCallback, |
| 51 | + }); |
| 52 | + |
| 53 | + // Move temporary tower from auxiliary pole to its final destination. |
| 54 | + hanoiTowerRecursive({ |
| 55 | + numberOfDiscs: numberOfDiscs - 1, |
| 56 | + fromPole: withPole, |
| 57 | + withPole: fromPole, |
| 58 | + toPole, |
| 59 | + moveCallback, |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * @param {number} numberOfDiscs |
| 66 | + * @param {function(disc: number, fromPole: number[], toPole: number[])} moveCallback |
| 67 | + */ |
| 68 | +export default function hanoiTower(numberOfDiscs, moveCallback) { |
| 69 | + // Each of three poles of Tower of Hanoi puzzle is represented as a stack |
| 70 | + // that might contain elements (discs). Each disc is represented as a number. |
| 71 | + // Larger discs have bigger number equivalent. |
| 72 | + |
| 73 | + // The pole from where the discs should be moved. |
| 74 | + const fromPole = new Stack(); |
| 75 | + |
| 76 | + // The middle pole that should be used as a helper. |
| 77 | + const withPole = new Stack(); |
| 78 | + |
| 79 | + // The destination pole where all discs need to be moved. |
| 80 | + const toPole = new Stack(); |
| 81 | + |
| 82 | + // Let's create the discs and put them to the fromPole. |
| 83 | + for (let discSize = numberOfDiscs; discSize > 0; discSize -= 1) { |
| 84 | + fromPole.push(discSize); |
| 85 | + } |
| 86 | + |
| 87 | + hanoiTowerRecursive({ |
| 88 | + numberOfDiscs, |
| 89 | + fromPole, |
| 90 | + withPole, |
| 91 | + toPole, |
| 92 | + moveCallback, |
| 93 | + }); |
| 94 | +} |
0 commit comments