/src/tp1/jalupe/ArvorePatricia.java

https://gitlab.com/lucalvos/TP1LAEDSJaLuPe · Java · 208 lines · 148 code · 31 blank · 29 comment · 41 complexity · 3f7625c2920505a163944e85575b16f8 MD5 · raw file

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package tp1.jalupe;
  7. import java.util.ArrayList;
  8. /**
  9. *
  10. * @author decom
  11. */
  12. public class ArvorePatricia {
  13. private static abstract class PatNo {
  14. }
  15. private static class PatNoInt extends PatNo {
  16. int index;
  17. PatNo esq, dir;
  18. }
  19. private static class PatNoExt extends PatNo {
  20. String chave; // @{\it O tipo da chave depende da aplica\c{c}\~ao}@
  21. ArrayList<Integer> linha,coluna;
  22. public PatNoExt(){
  23. linha = new ArrayList<Integer>();
  24. coluna = new ArrayList<Integer>();
  25. }
  26. }
  27. private PatNo raiz;
  28. private int nbitsChave; // Chave
  29. public int comparacoes;
  30. // @{\it Retorna o i-\'esimo bit da chave k a partir da esquerda}@
  31. private int bit(int i, String k) {
  32. if(k.charAt(i-1) == '1') return 1;
  33. return 0;
  34. }
  35. // private int bit(int i, char k) {
  36. // if (i == 0) {
  37. // return 0;
  38. // }
  39. // int c = (int) k;
  40. // for (int j = 1; j <= this.nbitsChave - i; j++) {
  41. // c = c / 2;
  42. // }
  43. // return c % 2;
  44. // }
  45. // @{\it Verifica se p \'e n\'o externo}@
  46. private boolean eExterno(PatNo p) {
  47. Class classe = p.getClass();
  48. return classe.getName().equals(PatNoExt.class.getName());
  49. }
  50. private PatNo criaNoInt(int i, PatNo esq, PatNo dir) {
  51. PatNoInt p = new PatNoInt();
  52. p.index = i;
  53. p.esq = esq;
  54. p.dir = dir;
  55. return p;
  56. }
  57. private PatNo criaNoExt(String k) {
  58. PatNoExt p = new PatNoExt();
  59. p.chave = k;
  60. return p;
  61. }
  62. // private PatNo criaNoExt(char k) {
  63. // PatNoExt p = new PatNoExt();
  64. // p.chave = k;
  65. //
  66. // return p;
  67. // }
  68. private void pesquisa(String k, PatNo t, String nome) {
  69. if (this.eExterno(t)) { // Se o no for externo fazer abaixo, se nao, ir para no interno
  70. PatNoExt aux = (PatNoExt) t; // Pega um no externo auxiliar
  71. //System.out.println("Chave : " + aux.chave + "\nString : " + k);
  72. if (aux.chave.equals(k)) { //compara o elemento com o o que dese, se for, retorna o elemento
  73. //System.out.println("Elemento encontrado");
  74. System.out.println("Palavra : " + nome
  75. +"\nChave : " + aux.chave
  76. +"\nLinha(s) : " + aux.linha
  77. +"\nColuna(s) : " + aux.coluna);
  78. System.out.println();
  79. } else {
  80. System.out.println("Elemento nao encontrado");
  81. }
  82. } else { // se for interno,pega o bit (aux.index) do elemento k
  83. PatNoInt aux = (PatNoInt) t; // pego o no como no interno
  84. if (this.bit(aux.index, k) == 0) { // pega o bit, se ele for 0, vai para a parte esquerda da arvore
  85. pesquisa(k, aux.esq, nome);
  86. } else { //se nao, vai para a parte direita
  87. pesquisa(k, aux.dir, nome);
  88. }
  89. }
  90. }
  91. private PatNo insereEntre(String k, PatNo t, int i) {
  92. PatNoInt aux = null;
  93. if (!this.eExterno(t)) {
  94. aux = (PatNoInt) t;
  95. }
  96. if (this.eExterno(t) || (i < aux.index)) { // @{\it Cria um novo n\'o externo}@
  97. PatNo p = this.criaNoExt(k);
  98. PatNoExt no = (PatNoExt) p;
  99. no.linha.add(ExtrairPalavra.countLinha);
  100. no.coluna.add(ExtrairPalavra.countColuna);
  101. if (this.bit(i, k) == 1) {
  102. return this.criaNoInt(i, t, p);
  103. } else {
  104. return this.criaNoInt(i, p, t);
  105. }
  106. } else {
  107. if (this.bit(aux.index, k) == 1) {
  108. aux.dir = this.insereEntre(k, aux.dir, i);
  109. } else {
  110. aux.esq = this.insereEntre(k, aux.esq, i);
  111. }
  112. return aux;
  113. }
  114. }
  115. private PatNo insere(String k, PatNo t) {
  116. if (t == null) {
  117. return this.criaNoExt(k);
  118. } else {
  119. PatNo p = t;
  120. while (!this.eExterno(p)) {
  121. PatNoInt aux = (PatNoInt) p;
  122. if (this.bit(aux.index, k) == 1) {
  123. p = aux.dir;
  124. } else {
  125. p = aux.esq;
  126. }
  127. }
  128. PatNoExt aux = (PatNoExt) p;
  129. int i = 1; // @{\it acha o primeiro bit diferente}@
  130. while ((i <= this.nbitsChave)
  131. && (this.bit(i, k) == this.bit(i, aux.chave))) {
  132. i++;
  133. }
  134. if (i > this.nbitsChave) { //Se numero de i passar da variavel, o valor eh igual
  135. aux.linha.add(ExtrairPalavra.countLinha);
  136. aux.coluna.add(ExtrairPalavra.countColuna);
  137. return t;
  138. } else {
  139. return this.insereEntre(k, t, i);
  140. }
  141. }
  142. }
  143. private void central(PatNo pai, PatNo filho, String msg) {
  144. if (filho != null) {
  145. if (!this.eExterno(filho)) {
  146. PatNoInt aux = (PatNoInt) filho;
  147. central(filho, aux.esq, "ESQ");
  148. if (pai != null) {
  149. System.out.println("Pai: " + ((PatNoInt) pai).index + " " + msg + " Int: " + aux.index);
  150. } else {
  151. System.out.println("Pai: " + pai + " " + msg + " Int: " + aux.index);
  152. }
  153. central(filho, aux.dir, "DIR");
  154. } else {
  155. PatNoExt aux = (PatNoExt) filho;
  156. if (pai != null) {
  157. System.out.println("Pai: " + ((PatNoInt) pai).index + " " + msg + " Ext: " + aux.chave);
  158. } else {
  159. System.out.println("Pai: " + pai + " " + msg + " Ext: " + aux.chave);
  160. }
  161. }
  162. }
  163. }
  164. public void imprime() {
  165. this.central(null, this.raiz, "RAIZ");
  166. }
  167. public ArvorePatricia(int nbitsChave) {
  168. this.raiz = null;
  169. this.nbitsChave = nbitsChave;
  170. }
  171. public void pesquisa(String k, String nome) {
  172. this.pesquisa(k, this.raiz, nome);
  173. }
  174. public void insere(String k) {
  175. this.raiz = this.insere(k, this.raiz);
  176. }
  177. }