Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Author: Uriel C.H.
- * Instituto Tecnológico Superior de Ciudad Hidalgo
- * Sysadmin and Advanced Researcher
- */
- import java.util.Scanner;
- public class ArbolBinario {
- HojaBinaria raiz = new HojaBinaria();
- public static void main(String[] args) {
- Scanner x = new Scanner(System.in);
- ArbolBinario p1 = new ArbolBinario();
- p1.raiz = null;
- int wordleng;
- String palabra;
- String arbol = "qgfxrdmvpwoahcjtbyzeuisnlk";
- for (int i = 0; i < arbol.length(); i++) {
- p1.Insertar(arbol.charAt(i));
- }
- while (true) {
- try {
- palabra = x.nextLine();
- palabra = palabra.toLowerCase();
- wordleng = palabra.length();
- if (wordleng == 1 && palabra.equals("q")) {
- System.out.println("*");
- } else {
- for (int i = 0; i < wordleng; i++) {
- if (palabra.charAt(i) != ' ') {
- if (i < (wordleng - 1)) {
- p1.Searcher(palabra.charAt(i));
- System.out.print("-");
- } else {
- p1.Searcher(palabra.charAt(i));
- }
- } else {
- System.out.print("*-");
- }
- }
- }
- System.out.println();
- } catch (java.lang.NullPointerException ex) {
- String aray[] = new String [5];
- System.out.println("\nERROR DATO INVALIDO");
- main(aray);
- }
- }
- }
- public void Insertar(char nom) {
- HojaBinaria hoja = new HojaBinaria();
- hoja.data = nom;
- hoja.der = null;
- hoja.izq = null;
- if (raiz == null) {
- hoja.code = '*';
- raiz = hoja;
- } else {
- HojaBinaria chalan = new HojaBinaria();
- HojaBinaria chalanAnt = new HojaBinaria();
- chalan = raiz;
- while (chalan != null) {
- chalanAnt = chalan;
- if (hoja.data > chalan.data) {
- chalan = chalan.der;
- } else {
- chalan = chalan.izq;
- }
- }
- if (hoja.data >= chalanAnt.data) {
- hoja.code = '1';
- chalanAnt.der = hoja;
- } else {
- hoja.code = '0';
- chalanAnt.izq = hoja;
- }
- }
- }
- public void Searcher(char nom) {
- if (raiz.data == nom) {
- System.out.print(raiz.code);
- } else {
- HojaBinaria chalan = new HojaBinaria();
- HojaBinaria chalan2 = new HojaBinaria();
- chalan = raiz;
- while (chalan.data != nom) {
- chalan2 = chalan;
- if (nom > chalan.data) {
- if (chalan.code != '*') {
- System.out.print(chalan.code);
- }
- chalan = chalan.der;
- } else {
- if (chalan.code != '*') {
- System.out.print(chalan.code);
- }
- chalan = chalan.izq;
- }
- }
- if (nom >= chalan2.data) {
- System.out.print(chalan2.der.code);
- } else {
- System.out.print(chalan2.izq.code);
- }
- }
- }
- }
- class HojaBinaria {
- char data;
- char code;
- HojaBinaria izq, der;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement