3
3
import java .util .List ;
4
4
5
5
/**
6
- * The {@code TowerOfHanoi} class provides functionality to solve the Tower of
7
- * Hanoi puzzle.
8
- * It uses recursion to move discs between poles and prints the steps to solve
9
- * the puzzle.
10
- * The main function interacts with the user to get the number of discs and
11
- * calls the recursive {@code shift} function to perform the moves.
6
+ * The {@code TowerOfHanoi} class provides a recursive solution to the Tower of Hanoi puzzle.
7
+ * This puzzle involves moving a set of discs from one pole to another, following specific rules:
8
+ * 1. Only one disc can be moved at a time.
9
+ * 2. A disc can only be placed on top of a larger disc.
10
+ * 3. All discs must start on one pole and end on another.
11
+ *
12
+ * This implementation recursively calculates the steps required to solve the puzzle and stores them
13
+ * in a provided list.
14
+ *
15
+ * <p>
16
+ * For more information about the Tower of Hanoi, see
17
+ * <a href="https://en.wikipedia.org/wiki/Tower_of_Hanoi">Tower of Hanoi on Wikipedia</a>.
18
+ * </p>
19
+ *
20
+ * The {@code shift} method takes the number of discs and the names of the poles,
21
+ * and appends the steps required to solve the puzzle to the provided list.
22
+ * Time Complexity: O(2^n) - Exponential time complexity due to the recursive nature of the problem.
23
+ * Space Complexity: O(n) - Linear space complexity due to the recursion stack.
12
24
* Wikipedia: https://en.wikipedia.org/wiki/Tower_of_Hanoi
13
25
*/
14
26
final class TowerOfHanoi {
@@ -20,9 +32,10 @@ private TowerOfHanoi() {
20
32
* Recursively solve the Tower of Hanoi puzzle by moving discs between poles.
21
33
*
22
34
* @param n The number of discs to move.
23
- * @param startPole The name of the start pole.
24
- * @param intermediatePole The name of the intermediate pole.
25
- * @param endPole The name of the end pole.
35
+ * @param startPole The name of the start pole from which discs are moved.
36
+ * @param intermediatePole The name of the intermediate pole used as a temporary holding area.
37
+ * @param endPole The name of the end pole to which discs are moved.
38
+ * @param result A list to store the steps required to solve the puzzle.
26
39
*
27
40
* <p>
28
41
* This method is called recursively to move n-1 discs
@@ -31,6 +44,11 @@ private TowerOfHanoi() {
31
44
* moves the n-1 discs from the
32
45
* intermediate pole to the end pole.
33
46
* </p>
47
+ *
48
+ * <p>
49
+ * Time Complexity: O(2^n) - Exponential time complexity due to the recursive nature of the problem.
50
+ * Space Complexity: O(n) - Linear space complexity due to the recursion stack.
51
+ * </p>
34
52
*/
35
53
public static void shift (int n , String startPole , String intermediatePole , String endPole , List <String > result ) {
36
54
if (n != 0 ) {
0 commit comments