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
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
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.
In java, create a binary search tree (without using additional libraries) of stacks where each node...
In java, create a binary search tree (without using additional libraries) of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key,...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use serializable interface implantation /** Class to encapsulate a tree node. */ protected static class Node implements Serializable {
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT