Skip to content

Commit 49e42ac

Browse files
committed
Add suggested changes
1 parent 27f7760 commit 49e42ac

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/main/java/com/thealgorithms/others/TowerOfHanoi.java

+27-9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33
import java.util.List;
44

55
/**
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.
1224
* Wikipedia: https://en.wikipedia.org/wiki/Tower_of_Hanoi
1325
*/
1426
final class TowerOfHanoi {
@@ -20,9 +32,10 @@ private TowerOfHanoi() {
2032
* Recursively solve the Tower of Hanoi puzzle by moving discs between poles.
2133
*
2234
* @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.
2639
*
2740
* <p>
2841
* This method is called recursively to move n-1 discs
@@ -31,6 +44,11 @@ private TowerOfHanoi() {
3144
* moves the n-1 discs from the
3245
* intermediate pole to the end pole.
3346
* </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>
3452
*/
3553
public static void shift(int n, String startPole, String intermediatePole, String endPole, List<String> result) {
3654
if (n != 0) {

0 commit comments

Comments
 (0)