Question: Homework C Remove any initial package declaration that might be added to your file in case you edit it in eclipse. The goal of the
Homework C 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(); } } --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package class13;
import java.util.ArrayList;
import class12.Tree;
public class BinaryTree extends Tree {
public BinaryTree() {
super();
}
public void addRoot(T x) throws Exception {
if (root != null) throw new Exception("The tree is not empty");
root = new BNode(x, null, null, null);
size++;
}
public void addLeft(BNode n, T x) throws Exception {
if (n.getLeft() != null) throw new Exception("Already used");
n.setLeft(new BNode(x, n, null, null));
size++;
}
public void addRight(BNode n, T x) throws Exception {
if (n.getRight() != null) throw new Exception("Already used");
n.setRight(new BNode(x, n, null, null));
size++;
}
// returns the parent of the removed node
public BNode removeNode(BNode n) {
if (isLeaf(n)) { // base case
BNode p = (BNode) parent(n);
if (p == null) root = null;
else p.removeChild(n);
size--;
return p;
}
if (n.getLeft() != null) {
BNode m = rightmostLeftDescendant(n);
n.setData(m.getData());
return removeNode(m); // recursively remove rightmost left descendant
}
// otherwise n does have a right child
BNode m = leftmostRightDescendant(n);
n.setData(m.getData());
return removeNode(m);
}
public BNode leftmostRightDescendant(BNode n) {
BNode m = n.getRight();
while (m.getLeft() != null) m = m.getLeft();
return m;
}
public BNode rightmostLeftDescendant(BNode n) {
BNode m = n.getLeft();
while (m.getRight() != null) m = m.getRight();
return m;
}
public ArrayList> inOrder() {
ArrayList> answer = new ArrayList>();
inOrder((BNode) root(), answer);
return answer;
}
public void inOrder(BNode n, ArrayList> v) {
if (n == null) return;
inOrder(n.getLeft(), v);
v.add(n);
inOrder(n.getRight(), v);
}
public ArrayList> flatOrder() {
return inOrder();
}
----------------------------------------------------------------------
please give me the code in the java for Eulertour. thanks
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
