Question
Hi, Can anyone modify this codes. This code is from chegg. Please make sure the main method remain same and it works. The main method
Hi, Can anyone modify this codes. This code is from chegg. Please make sure the main method remain same and it works. The main method is already given below. Please answer as soon as possible. Thanks
Again, start with the tree.java program and make a tree from characters typed by the user. This time, make a complete treeone that is completely full except possibly on the right end of the bottom row. The characters should be ordered from the top down and from left to right along each row, as if writing a letter on a pyramid. (This arrangement does not correspond to any of the three traversals we discussed in this chapter.) Thus, the string ABCDEFGHIJ would be arranged as A B C D E F G H I J One way to create this tree is from the top down, rather than the bottom up as in the previous two Programming Projects. Start by creating a node which will be the root of the final tree. If you think of the nodes as being numbered in the same order the letters are arranged, with 1 at the root, then any node numbered n has a left child numbered 2*n and a right child numbered 2*n+1. You might use a recursive routine that makes two children and then calls itself for each child. The nodes dont need to be created in the same order they are arranged on the tree. As in the previous Programming Projects, you can jettison the search-tree routines from the Tree class.
This is the main method:
public static void main(String[] args) throws IOException
{
int value;
System.out.print("Enter character string for tree: ");
String str = getString();
Tree theTree = new Tree(str);
while(true)
{
System.out.print("Enter first letter of ");
System.out.print("show or traverse: ");
int choice = getChar();
switch(choice)
{
case 's':
theTree.displayTree();
break;
case 't':
System.out.print("Enter type 1, 2 or 3: ");
value = getInt();
theTree.traverse(value);
break;
default:
System.out.print("Invalid entry ");
} // end switch
} // end while
} // end main()
output:
Enter character string for tree: ABCDEFGHIJ
Enter first letter of show or traverse: s
......................................................
(A)
(B) (C)
(D) (E) (F) (G)
(H) (I) (J) --- --- --- --- ---
......................................................
Enter first letter of show or traverse: t
Enter type 1, 2 or 3: 1
Preorder traversal: (A)(B)(D)(H)(I)(E)(J)(C)(F)(G)
Enter first letter of show or traverse: t
Enter type 1, 2 or 3: 2
Inorder traversal: (H)(D)(I)(B)(J)(E)(A)(F)(C)(G)
Enter first letter of show or traverse: t
Enter type 1, 2 or 3: 3
Postorder traversal: (H)(I)(D)(J)(E)(B)(F)(G)(C)(A)
Enter first letter of show or traverse:
Code is here: modify or cheange codes
//TreeApp.java
import java.io.*;
import java.util.*; // for Stack class
////////////////////////////////////////////////////////////////
class Node {
public char keyChar; // key
public Node leftChild; // this node's left child
public Node rightChild; // this node's right child
// -------------------------------------------------------------
Node(char kch) // constructor
{
keyChar = kch;
}
// -------------------------------------------------------------
public void display() // display ourself
{
System.out.print("(" + keyChar + ")");
}
// -------------------------------------------------------------
} // end class Node
// //////////////////////////////////////////////////////////////
class Tree {
private Node root; // first node of tree
private String strBuild; // string used to build tree
private int maxNodes; // total number of nodes
// -------------------------------------------------------------
public Tree(String s) // constructor
{
strBuild = s;
maxNodes = s.length();
char ch = strBuild.charAt(0); // get first character
root = new Node(ch); // make the root
recBuild(root, 1); // create root's children
}
// -------------------------------------------------------------
public void recBuild(Node thisNode, int ourNumber) {
int leftNumber = ourNumber * 2; // left child
if (leftNumber > maxNodes)
return;
else { // get character
char ch = strBuild.charAt(leftNumber - 1); // make node
Node leftNode = new Node(ch);
thisNode.leftChild = leftNode; // connect to it
recBuild(leftNode, leftNumber); // build its children
}
int rightNumber = ourNumber * 2 + 1; // right child
if (rightNumber > maxNodes)
return;
else { // get character
char ch = strBuild.charAt(rightNumber - 1); // make node
Node rightNode = new Node(ch);
thisNode.rightChild = rightNode; // connect to it
recBuild(rightNode, rightNumber); // build its children
}
}
// -------------------------------------------------------------
public void traverse(int traverseType) {
switch (traverseType) {
case 1:
System.out.print(" Preorder traversal: ");
preOrder(root);
break;
case 2:
System.out.print(" Inorder traversal: ");
inOrder(root);
break;
case 3:
System.out.print(" Postorder traversal: ");
postOrder(root);
break;
}
System.out.println();
}
// -------------------------------------------------------------
private void preOrder(Node localRoot) {
if (localRoot != null) {
localRoot.display();
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}
// -------------------------------------------------------------
private void inOrder(Node localRoot) {
if (localRoot != null) {
inOrder(localRoot.leftChild);
localRoot.display();
inOrder(localRoot.rightChild);
}
}
// -------------------------------------------------------------
private void postOrder(Node localRoot) {
if (localRoot != null) {
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.display();
}
}
// Function to display the tree
public void displayTree() {
Stack globalStack = new Stack();
globalStack.push(root);
int nBlanks = 32;
boolean isRowEmpty = false;
System.out
.println("......................................................");
while (isRowEmpty == false) {
Stack localStack = new Stack();
isRowEmpty = true;
for (int j = 0; j < nBlanks; j++)
System.out.print(' ');
while (globalStack.isEmpty() == false) {
Node temp = (Node) globalStack.pop();
if (temp != null) {
temp.display();
localStack.push(temp.leftChild);
localStack.push(temp.rightChild);
if (temp.leftChild != null || temp.rightChild != null)
isRowEmpty = false;
} else {
System.out.print("---");
localStack.push(null);
localStack.push(null);
}
for (int j = 0; j < nBlanks * 2 - 3; j++)
System.out.print(' ');
} // end while globalStack not empty
System.out.println();
nBlanks /= 2;
while (localStack.isEmpty() == false)
globalStack.push(localStack.pop());
} // end while isRowEmpty is false
System.out
.println("......................................................");
} // end displayTree()
// -------------------------------------------------------------
} // end class Tree
// //////////////////////////////////////////////////////////////
class TreeApp {
public static void main(String[] args) throws IOException {
int value;
System.out.print("Enter character string for tree: ");
String str = getString();
Tree theTree = new Tree(str);
while (true) {
System.out.print("Enter first letter of ");
System.out.print("show or traverse: ");
int choice = getChar();
switch (choice) {
case 's':
theTree.displayTree();
break;
case 't':
System.out.print("Enter type 1, 2 or 3: ");
value = getInt();
theTree.traverse(value);
break;
default:
System.out.print("Invalid entry ");
} // end switch
} // end while
} // end main()
// -------------------------------------------------------------
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
// -------------------------------------------------------------
public static char getChar() throws IOException {
String s = getString();
return s.charAt(0);
}
// -------------------------------------------------------------
public static int getInt() throws IOException {
String s = getString();
return Integer.parseInt(s);
}
// -------------------------------------------------------------
} // end class TreeApp
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started