Question

Homework Edit the following file and save it as Cxxxxxxxx.java where xxxxxxxx is replaced by your...

Homework Edit the following file and save it as Cxxxxxxxx.java where xxxxxxxx is replaced by your 8 digit ID number. Remove any initial package declaration that might be added to your file in case you edit it in eclipse. The goal of the homework is to add an Euler tour traversal (see page 348 of the text) to the implementation of the class BinaryTree that we have considered.   Your class Cxxxxxxxx should extend the class BinaryTree and implement just one new method. This file C00000000.java contains a shell for your new class that includes a title line for the method you must code and a main program (based on the BTreeApp demo from class) to check that your method works as it should. The only modifications that you should make are to add one or more methods to the class BinaryTree to provide the traversal method called eulerTour. You should also modify the main method to print your name and 8 digit id number. ********************************************************************/ import java.util.ArrayList; import java.util.Scanner; import class13.BinaryTree; import class13.BNode; public class C00000000 extends BinaryTree { public C00000000() { super(); } public ArrayList> eulerOrder() { return new ArrayList>(); // replace this with code that performs the Euler Tour Traversal } // Demo program to test your method --- change this to display your // name and id number. Also change C00000000 to reflect your ID number. public static void main(String args[]) { C00000000 g = new C00000000<>(); BNode cursor = null; Scanner s = new Scanner(System.in); while (true) { try { System.out.println(g.treePrint(cursor) + " commands act at the *cursor*: E l r X . > < ^ H S Q:"); String cmd = s.next(); if (cmd.charAt(0) == 'E') { ArrayList> tour = g.eulerOrder(); String answer = ""; for (BNode node: tour) answer += node.getData(); System.out.println(answer); } if (cmd.charAt(0) == 'Q') break; if (cmd.charAt(0) == 'H') { System.out.println(g.height()); continue; } if (cmd.charAt(0) == 'S') { System.out.println(g.size()); continue; } if (cmd.charAt(0) == 'X' && cursor != null) { g.removeNode(cursor); cursor = (BNode) g.root(); } if (cmd.charAt(0) == 'l') { String entry = s.next(); if (g.size() > 0) g.addLeft(cursor, entry); else g.addRoot(entry); } if (cmd.charAt(0) == 'r') { String entry = s.next(); if (g.size() > 0) g.addRight(cursor, entry); else g.addRoot(entry); } if (cmd.charAt(0) == '.') cursor = (BNode) g.root(); if (cmd.charAt(0) == '>') cursor = cursor.getRight(); if (cmd.charAt(0) == '<') cursor = cursor.getLeft(); if (cmd.charAt(0) == '^') cursor = (BNode) cursor.getParent(); } catch (Exception e) { System.out.println(e); } } s.close(); } }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Euler walk is as follows:

  1. Visit current node
  2. Visit subtree rooted at left child
  3. Visit current node (again)
  4. Visit subtree rooted at right child
  5. Visit current node (again)

based on above theory i am giving implementaion of eulertraversal method.

import java.util.ArrayList;
import java.util.Scanner;

import class13.BinaryTree;
import class13.BNode;

public class C00000000<T> extends BinaryTree<T> {

public C00000000() {
super();
}

public ArrayList<BNode<T>> eulerOrder() {
if(BNode=null)
return ;
else
{
eulerOrder(BNode.left); // traversing left
eulerOrder(BNode); //traversing center node
eulerOrder(BNode.right); //traversing right
return new ArrayList<BNode<T>>();
}
// replace this with code that performs the Euler Tour Traversal


}

// Demo program to test your method --- change this to display your
// name and id number. Also change C00000000 to reflect your ID number.

public static void main(String args[]) {
C00000000<String> g = new C00000000<>();
BNode<String> cursor = null;
Scanner s = new Scanner(System.in);
while (true) {
try {
System.out.println(g.treePrint(cursor)
+ " commands act at the *cursor*: E l r X . > < ^ H S Q:");
String cmd = s.next();
if (cmd.charAt(0) == 'E') {
ArrayList<BNode<String>> tour = g.eulerOrder();
String answer = "";
for (BNode<String> node: tour)
answer += node.getData();
System.out.println(answer);
}
if (cmd.charAt(0) == 'Q')
break;
if (cmd.charAt(0) == 'H') {
System.out.println(g.height());
continue;
}
if (cmd.charAt(0) == 'S') {
System.out.println(g.size());
continue;
}
if (cmd.charAt(0) == 'X' && cursor != null) {
g.removeNode(cursor);
cursor = (BNode<String>) g.root();
}
if (cmd.charAt(0) == 'l') {
String entry = s.next();
if (g.size() > 0)
g.addLeft(cursor, entry);
else
g.addRoot(entry);
}
if (cmd.charAt(0) == 'r') {
String entry = s.next();
if (g.size() > 0)
g.addRight(cursor, entry);
else
g.addRoot(entry);
}
if (cmd.charAt(0) == '.')
cursor = (BNode<String>) g.root();
if (cmd.charAt(0) == '>')
cursor = cursor.getRight();
if (cmd.charAt(0) == '<')
cursor = cursor.getLeft();
if (cmd.charAt(0) == '^')
cursor = (BNode<String>) cursor.getParent();
} catch (Exception e) {
System.out.println(e);
}
}
s.close();
}

}

Add a comment
Know the answer?
Add Answer to:
Homework Edit the following file and save it as Cxxxxxxxx.java where xxxxxxxx is replaced by your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException;...

    how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class checkFruit { private List<String> tests; public List<String> getFruit(String fruits) { tests = new ArrayList<String>(Arrays.asList(fruits.split(","))); for (String test : tests) { System.out.println(test); tests.add()//how would i use add here } return tests; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter file name: "); File file = new File(in.nextLine()); try { checkFruit...

  • Please edit my following JAVA code so that there is no main function. I would like...

    Please edit my following JAVA code so that there is no main function. I would like one function in this class that completes what the current program is trying to do so that I can call this class in my main class which will be separate. import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.Scanner; public class enterwklyincme { private static DecimalFormat df = new DecimalFormat("0.00"); public static float readFloat(Scanner reader) { float num; while (true) { try { num = Float.parseFloat(reader.nextLine()); return...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...

    Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...

  • Please help....the only line that can be modified is /* Your solution goes here */. Everything...

    Please help....the only line that can be modified is /* Your solution goes here */. Everything else should remain the same. I submitted hasDigit = Character.isDigit(passCode.charAt(3)); but it's wrong....thank you import java.util.Scanner; public class CheckingPasscodes { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); boolean hasDigit; String passCode; hasDigit = false; passCode = scnr.next(); /* Your solution goes here */ if (hasDigit) { System.out.println("Has a digit."); } else { System.out.println("Has no digit."); } } }

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • I wrote two java classes. One is main and other one is getter and setter. I...

    I wrote two java classes. One is main and other one is getter and setter. I brought methods from Digit class to Main class to print result. However, for some reason, only welcome print statement got printed and others got ignored. On the output screen, only welcome statement appear. nDigit should be input from user. import java.util.Scanner; public class Main {    public static void main(String[] args)    {        Digit digitGet = new Digit();              ...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT