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
  • JAVA: Rewrite the following code to where the inputs are from a file. The file name...

    JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; }    public static void main(String[] args) { int n, i, k; System.out.println("Enter...

  • (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...

  • Write a Java method that should take an ArrayList as a parameter, print its element in...

    Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen {   public static void main(String[] args) {     Scanner input = new Scanner(System.in);       System.out.println("How many numbers do you want to input?");       int num = input.nextInt();       ArrayList<Double> d = new ArrayList<Double>(num);       for(int i = 0 ; i < num; i++){           System.out.print("Enter...

  • 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...

  • Modify your program in Lab Assignment 4A to throw an exception if the file does not...

    Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result.   Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox. My code: import java.io.*; import java.util.*; public class Lab4B { public static void main(String[] args) throws IOException { final String input_file = "Lab_4A.txt"; final String output_file = "Lab_4A.txt"; Scanner x = null; FileWriter y = null; try {...

  • 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."); } } }

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