Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/com/thealgorithms/recursion/TowerOfHanoi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.thealgorithms.recursion;

/*Tower of Hanoi Problem on Wikipedia - https://en.wikipedia.org/wiki/Tower_of_Hanoi */

public class TowerOfHanoi {

public static void solveHanoi(int n, char src, char dest, char aux) {
if (n == 1) {
System.out.println("Move disk 1 from " + src + " to " + dest);
return;
}
solveHanoi(n - 1, src, aux, dest);
System.out.println("Move disk " + n + " from " + src + " to " + dest);
solveHanoi(n - 1, aux, dest, src);
}
}
11 changes: 11 additions & 0 deletions src/test/java/com/thealgorithms/recursion/TowerOfHanoiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.thealgorithms.recursion;

import org.junit.jupiter.api.Test;

class TowerOfHanoiTest {

@Test
void testSolve() {
TowerOfHanoi.solveHanoi(3, 'A', 'B', 'C');
}
}
Loading