Question: ` ` ` public static void main ( String [ ] args ) throws AVLTreeException, IOException { String usage = Harvester % n ;

```
public static void main(String[] args) throws AVLTreeException, IOException
{
String usage = "Harvester %n";
usage += "The command file name is entered as a command line argumet.%n";
usage += "The command file consists of instructions in one of these formats:.%n";
usage += "insert %n";
usage += "remove %n";
usage += "traverse%n";
usage += "props";
if (args.length !=1)
{
System.out.printf(usage);
}
System.exit(1);
// Read the command file
String fileName = args[0];
FileReader fileReader = new FileReader(fileName);
Scanner scanner = new Scanner(fileReader);
//1. Instantiate the tree using the default constructor
// AVLTree avlTree = new AVLTree>();
//2. Instantiate the tree using a comparator that compares strings first by length,
// and if lengths are the same, then lexicographically
Comparator comparator = new Comparators>(){
@Override
public int compare(String s1, String s2){
int lenDiff = Integer.compare(s1.length(), s2.length());
if (lenDiff !=0){
}
return lenDiff;
return s1.compareTo(s2); // Lexicographical comparison if lengths are equal
}
};
AVLTree avlTree = new AVLTree>(comparator);
while (scanner.hasNextLine()){
String line = scanner.nextLine().trim();
if (line.isEmpty()) continue; // Skip empty lines
String[] parts = line.split("\\s+");
String command = parts[0];
switch (command.toLowerCase()){
case "insert":
if (parts.length !=2){
System.out.printf("Error: Invalid insert command format.%n");
continue;
}
String wordToInsert = parts[1];
System.out.printf("Inserting %s...%n", wordToInsert);
avlTree.insert(wordToInsert);
break;
case "remove":
if (parts.length !=2){
System.out.printf("Error: Invalid remove command format.%n");
continue;
}
String wordToRemove = parts[1];
System.out.printf("Removing %s...%n", wordToRemove);
avlTree.remove(wordToRemove);
break;
case "traverse":
System.out.println("In-order traversal of the AVL Tree:");
// implement this method for
break;
case "props":
System.out.printf("Number of elements in AVL tree: %d%n", avlTree.size());
System.out.printf("Height of the AVL tree: %d%n", avlTree.height());
break;
default:
System.out.printf("Error: Unknown command %s%n", command);
}
```
` ` ` public static void main ( String [ ] args )

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!