import java.util.Scanner; /** * Read words and definitions from a file and load them to a * binary search tree using a map as interface. Finally print * all the dictionary words and definitions in alphabetical order. */ public class Solution { /** * Gets data from the dictionary file */ private Scanner input = null; private RandomBinaryTree randomBinaryTree = new RandomBinaryTree(); /** * Start the execution of the solution * @param args Command line arguments */ public static void main(String args[]) { Solution solution = new Solution(); solution.run(); } /** * Run the solution. This method is called from main() */ public void run() { try { int decision = 0; // Create object to read data from standard input this.input = new Scanner(System.in); decision = readOption(); switch (decision) { case 1: clean(); break; case 2: insert();break; case 3: level(); break; case 0: break; } } catch ( java.util.InputMismatchException exception ) { System.out.print("Invalid data"); } } /** * leer la opcion deseada por el usuario 1 (Restart), 2 (insert), 3 (level) 0 (exit) * @return la opcion deseada */ public int readOption() { System.out.print("\n1 (Restart), 2 (insert), 3 (level) 0 (exit)"); return input.nextInt(); } /** * por comodidad solo se aceptan numeros positivos */ public void insert() { int number = 0; System.out.print("Insert the number"); number = input.nextInt(); if (number != 0) { if (number < 0 ) { System.out.print("Invalid data"); } else { randomBinaryTree.insert(number); } run(); } } /** * limpia la matriz */ public void clean() { randomBinaryTree.clean(); run(); } /** * determina cuantos niveles tiene el arbol */ public void level() { int level = -1; if ( randomBinaryTree.isEmpty() != true ) { level = randomBinaryTree.level(); } System.out.printf("%d", level); run(); } }