-
Notifications
You must be signed in to change notification settings - Fork 20k
Add function documentation, enhance comments in TowerOfHanoi.java #5533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
siriak
merged 24 commits into
TheAlgorithms:master
from
Hardvan:tower_of_hanoi_improvement
Oct 13, 2024
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
887ee0f
Add function documentation, enhance comments in TowerOfHanoi.java
Hardvan f87e70d
Merge branch 'master' into tower_of_hanoi_improvement
Hardvan 3bedefd
Update directory
Hardvan a04c2c3
Add wiki link
Hardvan 4b9bea5
Merge branch 'tower_of_hanoi_improvement' of https://github.com/Hardv…
Hardvan a941a1d
Merge branch 'master' into tower_of_hanoi_improvement
Hardvan c2f6872
Update directory
Hardvan 10102c7
Fix comments
Hardvan ca10371
Merge branch 'tower_of_hanoi_improvement' of https://github.com/Hardv…
Hardvan c0dc63b
Add unit tests for ToH
Hardvan 65b9306
Update directory
Hardvan 69e69b5
Fix clang errors
Hardvan 95f4a90
Merge branch 'tower_of_hanoi_improvement' of https://github.com/Hardv…
Hardvan a4ec1ae
Merge branch 'master' into tower_of_hanoi_improvement
Hardvan 5d17a20
Update directory
Hardvan 146f3e9
Fix comments
Hardvan 27f7760
Merge branch 'tower_of_hanoi_improvement' of https://github.com/Hardv…
Hardvan 49e42ac
Add suggested changes
Hardvan ecbc8d1
Merge branch 'master' into tower_of_hanoi_improvement
alxkm 903c69a
Merge branch 'master' into tower_of_hanoi_improvement
Hardvan 496d9e4
Merge branch 'master' into tower_of_hanoi_improvement
alxkm ba1b548
Update directory
alxkm cf99ba3
Merge branch 'master' into tower_of_hanoi_improvement
alxkm e557086
Update directory
alxkm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,65 @@ | ||
package com.thealgorithms.others; | ||
|
||
import java.util.Scanner; | ||
import java.util.List; | ||
|
||
/** | ||
* The {@code TowerOfHanoi} class provides a recursive solution to the Tower of Hanoi puzzle. | ||
* This puzzle involves moving a set of discs from one pole to another, following specific rules: | ||
* 1. Only one disc can be moved at a time. | ||
* 2. A disc can only be placed on top of a larger disc. | ||
* 3. All discs must start on one pole and end on another. | ||
* | ||
* This implementation recursively calculates the steps required to solve the puzzle and stores them | ||
* in a provided list. | ||
* | ||
* <p> | ||
* For more information about the Tower of Hanoi, see | ||
* <a href="https://en.wikipedia.org/wiki/Tower_of_Hanoi">Tower of Hanoi on Uncyclopedia</a>. | ||
* </p> | ||
* | ||
* The {@code shift} method takes the number of discs and the names of the poles, | ||
* and appends the steps required to solve the puzzle to the provided list. | ||
* Time Complexity: O(2^n) - Exponential time complexity due to the recursive nature of the problem. | ||
* Space Complexity: O(n) - Linear space complexity due to the recursion stack. | ||
* Uncyclopedia: https://en.wikipedia.org/wiki/Tower_of_Hanoi | ||
*/ | ||
final class TowerOfHanoi { | ||
|
||
private TowerOfHanoi() { | ||
} | ||
|
||
public static void shift(int n, String startPole, String intermediatePole, String endPole) { | ||
// if n becomes zero the program returns thus ending the loop. | ||
/** | ||
* Recursively solve the Tower of Hanoi puzzle by moving discs between poles. | ||
* | ||
* @param n The number of discs to move. | ||
* @param startPole The name of the start pole from which discs are moved. | ||
* @param intermediatePole The name of the intermediate pole used as a temporary holding area. | ||
* @param endPole The name of the end pole to which discs are moved. | ||
* @param result A list to store the steps required to solve the puzzle. | ||
* | ||
* <p> | ||
* This method is called recursively to move n-1 discs | ||
* to the intermediate pole, | ||
* then moves the nth disc to the end pole, and finally | ||
* moves the n-1 discs from the | ||
* intermediate pole to the end pole. | ||
* </p> | ||
* | ||
* <p> | ||
* Time Complexity: O(2^n) - Exponential time complexity due to the recursive nature of the problem. | ||
* Space Complexity: O(n) - Linear space complexity due to the recursion stack. | ||
* </p> | ||
*/ | ||
public static void shift(int n, String startPole, String intermediatePole, String endPole, List<String> result) { | ||
Hardvan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (n != 0) { | ||
// Shift function is called in recursion for swapping the n-1 disc from the startPole to | ||
// the intermediatePole | ||
shift(n - 1, startPole, endPole, intermediatePole); | ||
System.out.format("Move %d from %s to %s%n", n, startPole, endPole); // Result Printing | ||
// Shift function is called in recursion for swapping the n-1 disc from the | ||
// intermediatePole to the endPole | ||
shift(n - 1, intermediatePole, startPole, endPole); | ||
} | ||
} | ||
// Move n-1 discs from startPole to intermediatePole | ||
shift(n - 1, startPole, endPole, intermediatePole, result); | ||
|
||
public static void main(String[] args) { | ||
System.out.print("Enter number of discs on Pole 1: "); | ||
Scanner scanner = new Scanner(System.in); | ||
int numberOfDiscs = scanner.nextInt(); // input of number of discs on pole 1 | ||
shift(numberOfDiscs, "Pole1", "Pole2", "Pole3"); // Shift function called | ||
scanner.close(); | ||
// Add the move of the nth disc from startPole to endPole | ||
result.add(String.format("Move %d from %s to %s", n, startPole, endPole)); | ||
|
||
// Move the n-1 discs from intermediatePole to endPole | ||
shift(n - 1, intermediatePole, startPole, endPole, result); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/com/thealgorithms/others/TowerOfHanoiTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.thealgorithms.others; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TowerOfHanoiTest { | ||
|
||
@Test | ||
public void testHanoiWithOneDisc() { | ||
List<String> result = new ArrayList<>(); | ||
TowerOfHanoi.shift(1, "Pole1", "Pole2", "Pole3", result); | ||
|
||
// Expected output for 1 disc | ||
List<String> expected = List.of("Move 1 from Pole1 to Pole3"); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testHanoiWithTwoDiscs() { | ||
List<String> result = new ArrayList<>(); | ||
TowerOfHanoi.shift(2, "Pole1", "Pole2", "Pole3", result); | ||
|
||
// Expected output for 2 discs | ||
List<String> expected = List.of("Move 1 from Pole1 to Pole2", "Move 2 from Pole1 to Pole3", "Move 1 from Pole2 to Pole3"); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testHanoiWithThreeDiscs() { | ||
List<String> result = new ArrayList<>(); | ||
TowerOfHanoi.shift(3, "Pole1", "Pole2", "Pole3", result); | ||
|
||
// Expected output for 3 discs | ||
List<String> expected = List.of("Move 1 from Pole1 to Pole3", "Move 2 from Pole1 to Pole2", "Move 1 from Pole3 to Pole2", "Move 3 from Pole1 to Pole3", "Move 1 from Pole2 to Pole1", "Move 2 from Pole2 to Pole3", "Move 1 from Pole1 to Pole3"); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
public void testHanoiWithZeroDiscs() { | ||
List<String> result = new ArrayList<>(); | ||
TowerOfHanoi.shift(0, "Pole1", "Pole2", "Pole3", result); | ||
|
||
// There should be no moves if there are 0 discs | ||
assertTrue(result.isEmpty()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.