Question

In: Computer Science

Java program that uses binary tree and recursions to implement Tower of Hanoi game where there...

Java program that uses binary tree and recursions to implement Tower of Hanoi game where there can be any amount of disks and there are either 3,4, or 5 pegs to store the disks(# of pegs and disks is manually inputted by user), in Tower of Hanoi game, the object of the game is move all the pieces from the 1st peg to the right most peg or the opposite side. You can place disks on top of each other only if disk under is a bigger number. Game ends when all pegs have been moved from left side to right side.

output example:

Disk 1 Moved from peg A to C

Disk 2 Moved from peg A to B

Disk 1 Moved from peg C to B

Disk 3 Moved from peg A to C

Disk 1 Moved from peg B to A

Disk 2 Moved from peg B to C

Disk 1 Moved from peg A to C

Solutions

Expert Solution

Here is the solution with recursion. Followed by outputs. Please do upvote thank you.

import java.util.*;
class Main
{
static void towerOfHanoi(int n, char from_peg, char to_peg, char aux_rod)
{
if (n == 1)
{
System.out.println("Disk 1 moved from peg " + from_peg + " to " + to_peg);
return;
}
towerOfHanoi(n-1, from_peg, aux_rod, to_peg);
System.out.println("Disk " + n + " moved from rod " + from_peg + " to " + to_peg);
towerOfHanoi(n-1, aux_rod, to_peg, from_peg);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n = sc.nextInt(); // Number of pegs
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of pegs
}
}


Related Solutions

C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
Implement a recursive program to draw a binary tree so that the root appears at the...
Implement a recursive program to draw a binary tree so that the root appears at the center of the page, the root of the left subtree is at the center of the left half of the page, etc. Alternative formulation: implement a recursive preorder binary tree traversal so that, when each node is displayed on the page, if the node is a left child it is shown to the left of the node displayed above it, and if it is...
java. Consider a binary tree with integer values in its nodes, implement a method that returns...
java. Consider a binary tree with integer values in its nodes, implement a method that returns the sum of the values contained in all of the nodes of the binary tree with root n.Hint: use recursion.
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template (include screenshots please of output)
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
Tower of Hanoi - Java Code Use three rods labeled A, B, and C Use three...
Tower of Hanoi - Java Code Use three rods labeled A, B, and C Use three discs labels 1 (smallest), 2 (medium size), and 3 (largest disc) The program prompts you to enter the starting rod (A, B, or C) The program prompts you to enter the ending rod (A, B, or C, but cannot be same rod as entered as starting rod) The starting rod has discs 1, 2, 3, where 1 is on top of 2 is on...
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
Write a java program to solve Towers of Hanoi with the condition that there are "m"...
Write a java program to solve Towers of Hanoi with the condition that there are "m" number of rods and "n" number of disks. Where m >= 3 and n >=1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT