Advertisement
Jameloncio

Tabelas Hash

Oct 22nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. //Classe Hash principal (Não mexi, tá igual a de Luiz)
  2. ---------------------------------------------------------------------------------------------------------------
  3. //TabelaHashLinear
  4. public class TabelaHashLinear<T extends Itemble > extends TabelaHash<Itemble> {
  5.  
  6. TabelaHashLinear(int tam){
  7. super(tam);
  8. }
  9.  
  10. @Override
  11. public void inserir(Itemble elem) {
  12.  
  13. int indice = hash(elem.getKey());
  14. while( (tabela[indice] != null) ) {
  15. indice++;
  16. if (indice >= tabela.length) {
  17. indice = 0;
  18. }
  19. }
  20.  
  21. tabela[indice] = elem;
  22. }
  23.  
  24. @Override
  25. public Itemble buscar(String key) {
  26. int indice = hash(key);
  27.  
  28. while((tabela[indice]!=null) && (!tabela[indice].getKey().equalsIgnoreCase(key))
  29. && (indice < tabela.length)){
  30. indice++;
  31. if (indice >= tabela.length) {
  32. indice = 0;
  33. }
  34. }
  35. if(indice == tabela.length)
  36. return null;
  37. return tabela[indice];
  38. }
  39. }
  40. ---------------------------------------------------------------------------------------------------------------
  41. //TabelaHashQuadrática
  42. public class TabelaHashQuadratico<T extends Itemble > extends TabelaHash{
  43.  
  44. public TabelaHashQuadratico(int tam) {
  45. super(tam);
  46. }
  47.  
  48. @Override
  49. public void inserir(Itemble elem) {
  50. int tentativa = 0;
  51. int indice = hash(elem.getKey());
  52. while( (tabela[indice] != null) ) {
  53. indice+= Math.pow(++tentativa, 2);
  54. indice= indice%tabela.length;
  55.  
  56. //voltando
  57. if (indice >= tabela.length) {
  58. indice = 0;
  59. }
  60. }
  61.  
  62. tabela[indice] = elem;
  63. }
  64.  
  65.  
  66. @Override
  67. public Itemble buscar(String key) {
  68. int indice = hash(key);
  69. int tentativa = 0;
  70.  
  71. while( (tabela[indice]!=null) &&
  72. (!tabela[indice].getKey().equalsIgnoreCase(key))
  73. && (indice < tabela.length) ){
  74. indice += Math.pow(++tentativa, 2);
  75. if (indice >= tabela.length) {
  76. indice = 0;
  77. }
  78. }
  79. if(indice == tabela.length)
  80. return null;
  81.  
  82. return tabela[indice];
  83. }
  84.  
  85. }
  86. ---------------------------------------------------------------------------------------------------------------
  87. //TabelaHashDupla (Não fiz o método de busca ainda, mas verifiquei o inserir)
  88. public class TabelaHashDuplo<T extends Itemble > extends TabelaHash{
  89.  
  90. TabelaHashDuplo(int tam){
  91. super(tam);
  92. }
  93.  
  94. public void inserir(Itemble elem) {
  95.  
  96. int indice = hash(elem.getKey());
  97.  
  98. if(tabela[indice] == null)
  99.  
  100. tabela[indice] = elem;
  101.  
  102. else {
  103.  
  104. int tentativa = 1;
  105. int indice2 = indice + tentativa * hash2(elem.getKey());
  106.  
  107. while( tabela[indice2] != null ) {
  108. tentativa++;
  109. indice2 = indice + tentativa * hash2(elem.getKey());
  110.  
  111. //voltando
  112. if (indice2 >= tabela.length) {
  113. indice2 = 0;
  114. }
  115. }
  116.  
  117. tabela[indice] = elem;
  118. }
  119.  
  120. }
  121.  
  122. public Itemble buscar(String key){
  123. return null;
  124. }
  125.  
  126. public int hash2(String key) {
  127. int indice = 0;
  128.  
  129. for(int i = key.length()-1; i >= 0; i--) {
  130. int c = (int) key.charAt(i);
  131. indice += c * Math.pow(26, key.length() - i);
  132. }
  133.  
  134. return indice % tabela.length;
  135. }
  136.  
  137.  
  138. }
  139. -----------------------------------------------------------------------------------------------------------------
  140. //TabelaHashSeparada (Pelos meus testes, essa classe tá OK, então não mexi nela)
  141.  
  142. Ressalva: O Item que Luis fez tinha 3 atributos, reduzi pra um e organizei o toString, mas foi coisa besta.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement