HW7 Code
/* DO NOT CHANGE: This file is used in evaluation */
public abstract class Base {
public abstract String getName ();
public boolean isLessThan (Base base) { // isLessThan function
return true;
}
public void jettison() {
return;
}
}
/* DO NOT CHANGE: This file is used in evaluation */
public class SymTab<Whatever extends Base> extends Tree<Whatever> {
public SymTab (String name, String caller){
super (name, caller);
}
}
public class Tree<Whatever extends Base> {
// data fields
private TNode root;
private long occupancy;
private String treeName;
private String representation;
private Tracker tracker;
// debug flag
private static boolean debug;
// debug messages
private static final String ALLOCATE = " - Allocating]\n";
private static final String AND = " and ";
private static final String CLOSE = "]\n";
private static final String COMPARE = " - Comparing ";
private static final String INSERT = " - Inserting ";
private static final String TREE = "[Tree ";
public Tree (String name, String caller) {
tracker = new Tracker ("Tree", Size.of (root)
+ Size.of (occupancy)
+ Size.of (treeName)
+ Size.of (representation)
+ Size.of (tracker),
caller + " calling Tree Ctor");
// --------- DO NOT CHANGE ABOVE ---------
//TODO: YOUR CODE GOES HERE
}
public void jettison () {
//TODO: YOUR CODE GOES HERE
}
public void jettisonAllNodes (TNode root) {
//TODO: YOUR CODE GOES HERE
}
public static void debugOff () {
//TODO: YOUR CODE GOES HERE
}
public static void debugOn () {
//TODO: YOUR CODE GOES HERE
}
public boolean insert (Whatever element) {
//TODO: YOUR CODE GOES HERE
}
public boolean isEmpty () {
//TODO: YOUR CODE GOES HERE
}
public Whatever remove (Whatever element) {
//TODO: YOUR CODE GOES HERE
}
public Whatever lookup (Whatever element) {
//TODO: YOUR CODE GOES HERE
}
/**
* Creates a string representation of this tree. This method first
* adds the general information of this tree, then calls the
* recursive TNode function to add all nodes to the return string
*
* @return String representation of this tree
*/
public String toString () {
representation = "Tree " + treeName + ":\noccupancy is ";
representation += occupancy + " elements.";
if (root != null)
root.writeAllTNodes ();
if (debug)
System.err.println (tracker);
return representation;
}
private class TNode {
public Whatever data;
public TNode left, right, parent;
public boolean hasBeenDeleted;
private Tracker tracker;
// left child's height - right child's height
public long balance;
// 1 + height of tallest child, or 0 for leaf
public long height;
public TNode (Whatever element, String caller) {
tracker = new Tracker ("TNode", Size.of (data)
+ Size.of (left) + Size.of (right)
+ Size.of (parent)
+ Size.of (balance) + Size.of (height),
caller + " calling TNode Ctor");
// --------- DO NOT CHANGE ABOVE ---------
//TODO: YOUR CODE GOES HERE
}
public void jettison () {
//TODO: YOUR CODE GOES HERE
}
/**
* Creates a string representation of this node. Information
* to be printed includes this node's height, its balance,
* and the data its storing.
*
* @return String representation of this node
*/
public String toString () {
return "at height: " + height + " with balance: " +
balance + " " + data;
}
/**
* Writes all TNodes to the String representation field.
* This recursive method performs an in-order
* traversal of the entire tree to print all nodes in
* sorted order, as determined by the keys stored in each
* node. To print itself, the current node will append to
* tree's String field.
*/
public void writeAllTNodes () {
if (left != null)
left.writeAllTNodes ();
if (!hasBeenDeleted)
representation += "\n" + this;
if (right != null)
right.writeAllTNodes ();
}
}
}
class Variable extends Base {
public String name; /* name of variable */
public long value; /* value of interest */
private Tracker tracker;
public Variable (String nm, long val, String caller) {
tracker = new Tracker("Variable",
Size.of (name) + Size.of (value) + Size.of (tracker),
caller + " calling Variable ctor");
name = nm;
value = val;
}
public Variable (Variable variable) {
tracker = new Tracker("Variable",
Size.of (name) + Size.of (value) + Size.of (tracker),
"Variable ctor");
name = new String (variable.name);
value = variable.value;
}
public void jettison() {
tracker.jettison();
}
public String getName () {
return name;
}
public boolean equals (Object object) {
if (this == object)
return true;
if (!(object instanceof Variable))
return false;
Variable otherVar = (Variable) object;
return name.equals (otherVar.getName ());
}
public boolean isLessThan (Base base) {
return (name.compareTo (base.getName ()) < 0) ? true : false;
}
public String toString () {
return name + "(" + value + ")";
}
public Variable assignValue (long val) {
Variable retval; // return value
// give variable its value
value = val;
retval = new Variable (this);
return retval;
}
}
import java.io.*;
class UCSDStudent extends Base {
public String name;
public long studentnum;
private Tracker tracker;
//TODO: YOUR CODE GOES HERE
public String toString () {
return "name: " + name + " studentnum: " + studentnum;
}
}
public class Driver {
private static final short NULL = 0;
public static void main (String [] args) {
// initialize debug states
Tree.debugOff ();
// check command line options
for (int index = 0; index < args.length; ++index) {
if (args[index].equals ("-x"))
Tree.debugOn ();
}
// The real start of the code
SymTab<UCSDStudent> symtab =
new SymTab <UCSDStudent>("UCSDStudentTree",
"main");
String buffer = null;
char command;
long number = 0;
UCSDStudent stu = new UCSDStudent (buffer, 0, "main");
System.out.println ("Initial Symbol Table:\n" + symtab);
while (true) {
command = NULL; // reset command each time in loop
System.out.print ("Please enter a command:\n"
+ " (a)llocate, is(e)mpty, (c)heck memory,\n"
+ " (i)nsert, (l)ookup, (r)emove,\n"
+ " (w)rite: ");
try {
command = MyLib.getchar ();
MyLib.clrbuf (command); // get rid of return
switch (command) {
case 'a':
System.out.print ("Please enter name of new"
+ " Tree to allocate: ");
// Delete the old tree to make memeory clean
symtab.jettison ();
buffer = MyLib.getline (); // formatted input
symtab = new SymTab <UCSDStudent> (buffer,
"main");
break;
case 'c':
Tracker.checkMemoryLeaks ();
System.out.println ();
break;
case 'e':
if (symtab.isEmpty ()) {
System.out.println ("Tree is empty.");
} else {
System.out.println ("Tree is"
+ " not empty.");
}
break;
case 'i':
System.out.print ("Please enter UCSD student"
+ " name to insert: ");
buffer = MyLib.getline (); // formatted input
System.out.print ("Please enter UCSD student"
+ " number: ");
number = MyLib.decin ();
MyLib.clrbuf (command); // get rid of return
// create student and place in symbol table
symtab.insert (new UCSDStudent (buffer, number,
"main"));
break;
case 'l':
UCSDStudent found; // whether found or not
System.out.print ("Please enter UCSD student"
+ " name to lookup: ");
stu.name = MyLib.getline ();
found = symtab.lookup (stu);
if (found != null) {
System.out.println ("Student found!");
System.out.println (found);
}
else {
System.out.println ("student "
+ stu.name + " not there!");
}
break;
case 'r':
UCSDStudent removed; // data to be removed
System.out.print ("Please enter UCSD student"
+ " name to remove: ");
stu.name = MyLib.getline ();
removed = symtab.remove (stu);
if (removed != null) {
System.out.println ("Student removed!");
System.out.println (removed);
}
else {
System.out.println ("student "
+ stu.name + " not there!");
}
break;
case 'w':
System.out.println ("The Symbol Table"
+ " contains:\n" + symtab);
}
}
catch (EOFException eof) {
break;
}
}
System.out.println ("\nFinal Symbol Table:\n" + symtab);
stu.jettison ();
symtab.jettison ();
Tracker.checkMemoryLeaks ();
}
}