/** * Evaluacion 3 Catedra * @author alvarez.jostin@gmail.com * */ import java.util.Scanner; public class EvaluacionCatedra { /** * Variable to read data from the input */ private Scanner input = null; /** * Reference to the RandomTree class */ private RandomTree randomTree = null; /** * Main method of the class * @param args */ public static void main(String[] args) { EvaluacionCatedra evaluacion = new EvaluacionCatedra(); evaluacion.run(); } public void run() { this.input = new Scanner(System.in); randomTree = new RandomTree(); char option = ' '; while ( option != 's' && option != 'S') { option = ' '; printMenu(); while ( isInvalid(option)) { option = input.next().charAt(0); } doOption(option); } // End of the program System.out.println(""); } /** * Method to do the action chosen by the user * @param option the option that the user chose */ public void doOption( char option) { switch (option) { case 'a': case 'A': { randomTree.resetTree(); break; } case 'b': case 'B': { addElement(); break; } case 'c': case 'C': { printFarthestLeaf(); break; } default: break; } } /** * Method to print the menu of the program */ public void printMenu() { System.out.println("\nBienvenido al programa de manejo de árboles de la tercera evaluación de cátedra."); System.out.println("Por favor digite la letra de la opción que desea: "); System.out.print("[a) Reiniciar el arbol, b) Insertar elemento, c) Imprimir nivel, s) Salir]: "); } /** * Method to determine if the option inserted is valid * @param option the option inserted by the user * @return true if option == a, b, c or s */ public boolean isInvalid( char option) { option = Character.toUpperCase(option); return ! (option == 'A' || option == 'B' || option == 'C' || option == 'S'); } /** * Method to add a new element to the three */ public void addElement( ) { System.out.print("\nValor (entero) a insertar: "); int value = input.nextInt(); randomTree.insert("", value); randomTree.print(System.out); System.out.println(""); } /** * Method to print the leaf that is farthest away from the root. * If the tree is empty, it prints "-1". */ public void printFarthestLeaf() { long farthestLevel = 0; farthestLevel = ( randomTree.isEmpty()) ? -1 : randomTree.findFarthestLeaf(); System.out.printf("\nEl nivel de la hoja más alejada a la raíz es: %d\n", farthestLevel); } }