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
Develop a Java application to implement a binary tree data structure. A tree data structure starts...
Develop a Java application to implement a binary tree data structure. A tree data structure starts from the top node, called the root. Each node in the tree has a set of children, which are also nodes of the tree and can have children of their own, and so on. This keeps on going till we get to the bottom of the tree, to the “leaf” nodes. Each node in the tree, except for the root, has a parent. A...
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...
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
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.
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 . •...
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
I have a recursive Tower of Hanoi program and would like to get the index numbers...
I have a recursive Tower of Hanoi program and would like to get the index numbers to be in correct sequence: 1,2,3,4,5,6,7. How can I do this? Main.java import java.io.*; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args){ /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole C without ever putting a...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT