Question: 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
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 C00000000extends 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
public BinaryTree() {
super();
}
public void addRoot(T x) throws Exception {
if (root != null) throw new Exception("The tree is not empty");
root = new BNode
size++;
}
public void addLeft(BNode
if (n.getLeft() != null) throw new Exception("Already used");
n.setLeft(new BNode
size++;
}
public void addRight(BNode
if (n.getRight() != null) throw new Exception("Already used");
n.setRight(new BNode
size++;
}
// returns the parent of the removed node
public BNode
if (isLeaf(n)) { // base case
BNode
if (p == null) root = null;
else p.removeChild(n);
size--;
return p;
}
if (n.getLeft() != null) {
BNode
n.setData(m.getData());
return removeNode(m); // recursively remove rightmost left descendant
}
// otherwise n does have a right child
BNode
n.setData(m.getData());
return removeNode(m);
}
public BNode
BNode
while (m.getLeft() != null) m = m.getLeft();
return m;
}
public BNode
BNode
while (m.getRight() != null) m = m.getRight();
return m;
}
public ArrayList
ArrayList
inOrder((BNode
return answer;
}
public void inOrder(BNode
if (n == null) return;
inOrder(n.getLeft(), v);
v.add(n);
inOrder(n.getRight(), v);
}
public ArrayList
return inOrder();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
