Skip to content

Conversation

@vizahat36
Copy link
Contributor

Add Tower of Hanoi recursive algorithm

Description

Fixes #7223 (requested by @justanothercoder-hub).

This PR adds a professional, well-documented implementation of the Tower of Hanoi problem to the recursion module.
The solution demonstrates the classic recursive 3-step approach and adheres to all core problem constraints.

Features

  • Recursive implementation following standard Tower of Hanoi rules
  • Clear and educational JavaDoc documentation
  • Time complexity: O(2^n − 1), Space complexity: O(n)
  • JUnit test class added to validate the implementation

Testing

  • JUnit tests included
  • 25 test cases covering:
    • Disk count validation
    • Correctness of generated moves
    • Edge cases and exception handling
    • Formula verification (2^n − 1 moves)
  • All tests pass successfully
  • Checkstyle compliant

TowerOfHonai-output

@codecov-commenter
Copy link

codecov-commenter commented Jan 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.31%. Comparing base (8403b8f) to head (a38a464).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7235      +/-   ##
============================================
- Coverage     79.32%   79.31%   -0.01%     
+ Complexity     6992     6990       -2     
============================================
  Files           781      781              
  Lines         22955    22958       +3     
  Branches       4509     4510       +1     
============================================
+ Hits          18209    18210       +1     
  Misses         4020     4020              
- Partials        726      728       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vizahat36
Copy link
Contributor Author

vizahat36 commented Jan 23, 2026

Hi @DenizAltunkapan,@yanglbme, @alxkm
This PR is ready for review and all CI checks are passing.
I’ve addressed the requested enhancement by adding the Tower of Hanoi implementation with documentation and JUnit tests.
Please check this once.

@DenizAltunkapan
Copy link
Member

@vizahat36
Copy link
Contributor Author

vizahat36 commented Feb 9, 2026

Hi @DenizAltunkapan, thanks for the review.

You’re right — there’s already a TowerOfHanoi implementation under com.thealgorithms.puzzlesandgames.
That said, I believe a recursion-focused version under com.thealgorithms.recursion can still be useful, since this problem is commonly taught as a core recursion example rather than a puzzle.

Both implementations use the standard Tower of Hanoi recursion, so the core algorithm is the same.
The difference is mainly in API design and usability:

  • My version provides a solve() method that returns the list of moves, instead of requiring a list to be passed in
  • It includes input validation with clear exceptions
  • Adds a small utility to calculate total moves (2^n - 1)
  • Separates the public API and recursive helper for better readability

To keep it non-duplicative, I can:

  • Keep the implementation minimal and recursion-focused
  • Add concise docs explaining the recursion flow
  • Include unit tests
  • Reference the existing implementation in Javadoc

Happy to update the PR this way, or close it if you feel it’s redundant. Please let me know what you prefer.

@DenizAltunkapan
Copy link
Member

@vizahat36 please make sure the repository only has one towerofhanoi class. You can update the existing version with more unit tests and concise docs, but keeping two classes is redundant in my opinion...

@vizahat36
Copy link
Contributor Author

Thanks for the clarification, @DenizAltunkapan — that makes sense.

I’ll update the PR to remove the duplicate class and instead enhance the existing
TowerOfHanoi implementation under puzzlesandgames with improved documentation
and the additional unit tests.

I’ll push the changes shortly.

@vizahat36
Copy link
Contributor Author

Thanks for the guidance, @DenizAltunkapan.
I’ve removed the duplicate recursion implementation and enhanced the existing
TowerOfHanoi under puzzlesandgames with improved documentation and unit tests.

@vizahat36
Copy link
Contributor Author

@DenizAltunkapan
Thank you for the earlier guidance.
I have fixed the clang-format issue and all checks are now passing.
Kindly review the changes when you have time.

@DenizAltunkapan DenizAltunkapan merged commit 0c79d33 into TheAlgorithms:master Feb 11, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Adding "TOWER OF HANOI" to Recursion File

3 participants