11package com .thealgorithms .recursion ;
22
33/**
4- * Tower of Hanoi problem using recursion.
4+ * Tower of Hanoi problem solved using recursion.
55 *
66 * <p>Time Complexity: O(2^n)</p>
7- * <p>Space Complexity: O(n) (recursion stack) </p>
7+ * <p>Space Complexity: O(n)</p>
88 */
99public final class TowerOfHanoi {
1010
@@ -16,18 +16,24 @@ private TowerOfHanoi() {
1616 * Solves the Tower of Hanoi problem.
1717 *
1818 * @param n number of disks
19- * @param src source rod
19+ * @param source source rod
2020 * @param helper auxiliary rod
21- * @param dest destination rod
21+ * @param destination destination rod
2222 */
23- public static void towerOfHanoi (int n , char src , char helper , char dest ) {
23+ public static void towerOfHanoi (int n , char source , char helper , char destination ) {
2424 if (n == 1 ) {
25- System .out .println ("Move disk 1 from " + src + " to " + dest );
25+ System .out .println (
26+ "Move disk 1 from " + source + " to " + destination
27+ );
2628 return ;
2729 }
2830
29- towerOfHanoi (n - 1 , src , dest , helper );
30- System .out .println ("Move disk " + n + " from " + src + " to " + dest );
31- towerOfHanoi (n - 1 , helper , src , dest );
31+ towerOfHanoi (n - 1 , source , destination , helper );
32+
33+ System .out .println (
34+ "Move disk " + n + " from " + source + " to " + destination
35+ );
36+
37+ towerOfHanoi (n - 1 , helper , source , destination );
3238 }
3339}
0 commit comments