Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. HASH--------------------------------------------------
  2. package hash;
  3. public class Hash {
  4.  
  5. public Celda[] tabla;
  6. private int numElementos;
  7. private double alfaMax;
  8. private int capacidad;
  9.  
  10. public Hash() {
  11. this.alfaMax = 0.8;
  12. this.numElementos = 0;
  13. tabla = new Celda[11];
  14. }
  15.  
  16. public Hash(int capacidad) {
  17. if (esPrimo(capacidad) == true) {
  18. tabla = new Celda[capacidad];
  19. } else {
  20. while (esPrimo(capacidad) == false) {
  21. capacidad = siguientePrimo(capacidad);
  22. }
  23. tabla = new Celda[capacidad];
  24. }
  25. this.numElementos = 0;
  26. this.alfaMax = 0.8;
  27. }
  28.  
  29. public Hash(int capacidad, double alfaMaximo) {
  30. if (esPrimo(capacidad) == true) {
  31. tabla = new Celda[capacidad];
  32. } else {
  33. while (esPrimo(capacidad) == false) {
  34. capacidad = siguientePrimo(capacidad);
  35. }
  36. tabla = new Celda[capacidad];
  37. }
  38. this.numElementos = 0;
  39. this.alfaMax = alfaMaximo;
  40. }
  41.  
  42. public void insertar(int clave, int v) {
  43. this.numElementos++;
  44. if (factorCarga() > alfaMax) {
  45. redimensionar();
  46. }
  47. int posicion;
  48. Celda celda = new Celda(clave); //estado 0 = ocupado
  49. posicion = funcionHash(clave, 0); // k mod n
  50. try {
  51. if (hayColision(posicion) == false) { //No hay colision
  52. tabla[posicion] = celda;
  53. tabla[posicion].setClave(clave);
  54. tabla[posicion].setValor(v);
  55. tabla[posicion].setEstado(1);
  56. } else { // hay colision
  57. int contador = 1;
  58. while (hayColision(posicion) == true) {
  59. posicion = funcionHash(clave, contador); //colision * (7 - clave mod 7)
  60. hayColision(posicion);
  61. contador++;
  62. }
  63. tabla[posicion] = celda;
  64. tabla[posicion].setClave(clave);
  65. tabla[posicion].setValor(v);
  66. tabla[posicion].setEstado(1);
  67. }
  68. } catch (Exception e) {
  69. System.err.print(e);
  70. }
  71.  
  72. }
  73.  
  74. public void borrar(int clave) {
  75. int colision = 0;
  76. int posicion = funcionHash(clave, colision);
  77. try {
  78. while (tabla[posicion].getClave() != clave && colision != tabla.length) {
  79. posicion = funcionHash(clave, colision);
  80. colision++;
  81. }
  82. tabla[posicion].setEstado(-1);
  83. tabla[posicion].setValor(0);
  84. this.numElementos = this.numElementos - 1;
  85.  
  86. } catch (Exception e) {
  87. System.err.println(e);
  88. }
  89. }
  90.  
  91. public void buscar(int clave) {
  92. int colision = 0;
  93. int posicion = funcionHash(clave, colision);
  94. final int posAr = 0;
  95. try {
  96. while (tabla[posicion].getClave() != clave && colision != tabla.length){
  97. posicion = funcionHash(clave,colision);
  98. colision ++;
  99. }
  100.  
  101. } catch (Exception e) {
  102. System.err.println(e);
  103. }
  104. System.out.println("La posicion es " + posicion);
  105. }
  106.  
  107. public int get(int clave) {
  108. int colision = 0;
  109. int posicion = funcionHash(clave, colision);
  110. try {
  111. if (tabla[posicion].getClave() == clave) {
  112. if (tabla[posicion].getEstado() == 1) {
  113. return tabla[posicion].getValor();
  114. } else {
  115. return 0;
  116. }
  117. } else {
  118. while (tabla[posicion].getClave() != clave) {
  119. colision++;
  120. posicion = funcionHash(clave, colision);
  121. }
  122. if (tabla[posicion].getEstado() == 1) {
  123. return tabla[posicion].getValor();
  124. } else {
  125. return 0;
  126. }
  127. }
  128. } catch (Exception e) {
  129. System.err.println(e);
  130. }
  131. return 0;
  132. }
  133.  
  134. public boolean esVacia() {
  135. boolean comprobar = true;
  136. if (this.numElementos == 0) {
  137. return comprobar;
  138. } else {
  139. comprobar = false;
  140. }
  141. return comprobar;
  142. }
  143.  
  144. public double getAlfa() {
  145. return alfaMax;
  146. }
  147.  
  148. public void setAlfa(double alfa) {
  149. this.alfaMax = alfa;
  150. }
  151.  
  152. public int getNumElementos() {
  153. return this.numElementos;
  154. }
  155.  
  156. public double factorCarga() {
  157. return (double) this.numElementos / tabla.length;
  158. }
  159.  
  160. private boolean hayColision(int posicion) {
  161. boolean colision = true;
  162. if (this.tabla[posicion] == null) {
  163. colision = false;
  164. }
  165. return colision;
  166. }
  167.  
  168. private int funcionHash(int clave, int colisiones) {
  169. int numero = hash1(clave) + hash2(clave, colisiones);
  170. return numero;
  171. }
  172.  
  173. private int hash1(int clave) {
  174. int numero = clave % tabla.length;
  175. return numero;
  176. }
  177.  
  178. private int hash2(int clave, int colisiones) {
  179. int numero = colisiones * (7 - (clave % 7));
  180. return numero;
  181. }
  182.  
  183. private void redimensionar() {
  184. Celda[] copia = this.tabla;
  185. int tamano = tabla.length * 2;
  186. int tamanoAumentado = 0;
  187. this.numElementos = 0;
  188. try {
  189. if (esPrimo(tamano) == false) {
  190. tamanoAumentado = siguientePrimo(tamano);
  191. }
  192. this.tabla = new Celda[tamanoAumentado];
  193. int clave;
  194. int valor;
  195. for (Celda copia1 : copia) {
  196. if (copia1 != null) {
  197. clave = copia1.getClave();
  198. valor = (int) copia1.getValor();
  199. insertar(clave, valor);
  200. }
  201. }
  202. } catch (Exception e) {
  203. System.err.println(e);
  204. }
  205. }
  206.  
  207. private boolean esPrimo(int numero) {
  208. boolean primo = true;
  209. int contador = 2;
  210. while (contador != numero && primo) {
  211. if (numero % contador == 0) {
  212. primo = false;
  213. }
  214. contador++;
  215. }
  216. return primo;
  217. }
  218.  
  219. private int siguientePrimo(int numero) {
  220. if (esPrimo(numero) == true) {
  221. return numero;
  222. } else {
  223. while (esPrimo(numero) == false) {
  224. numero++;
  225. }
  226. }
  227. return numero;
  228. }
  229. }
  230. CELDA---------------------------------------------------------------------------------------------------------------------------------
  231. package hash;
  232. public class Celda{
  233.  
  234. private int clave;
  235. private int valor;
  236. private int estado;
  237.  
  238. /**
  239. * Constructor por defecto
  240. */
  241. public Celda() {
  242.  
  243. }
  244.  
  245. /**
  246. * Constructor al que se le pasa un entero clave
  247. *
  248. * @param clave
  249. */
  250. public Celda(int clave) {
  251. this.clave = clave;
  252. }
  253.  
  254. /**
  255. *
  256. * @param estado
  257. */
  258. public void setEstado(int estado) {
  259. this.estado = estado;
  260. }
  261.  
  262. /**
  263. *
  264. * @return
  265. */
  266. public int getClave() {
  267. return clave;
  268. }
  269.  
  270. public void setClave(int clave) {
  271. this.clave = clave;
  272. }
  273.  
  274. public int getEstado() {
  275. return this.estado;
  276. }
  277.  
  278. /**
  279. *
  280. * @param valor
  281. */
  282. public void setValor(int valor) {
  283. this.valor = valor;
  284. }
  285.  
  286. /**
  287. *
  288. * @return
  289. */
  290. public int getValor() {
  291. return valor;
  292. }
  293.  
  294. }
  295. MAIN---------------------------------------------------------------------------------------------------------------------------------
  296. package hash;
  297. public class Tester {
  298.  
  299. public static void main(String[] args){
  300.  
  301. Hash miTabla = new Hash (25,0.8);
  302. for (int i = 0; i < 100; i ++){
  303. int dni = (int)Math.round(Math.random()*100000000);
  304.  
  305. miTabla.insertar(dni, dni);
  306.  
  307. }
  308.  
  309. for(int i = 0;i<miTabla.tabla.length;i++){
  310. System.out.println(miTabla.tabla[i]);
  311. }
  312. //miTabla.buscar(); NOTA* Al ser creados con la funcion random es imposible buscar uno y coincidir pero
  313. // introduciendolo a mano si te devuelve la posicion en el array.
  314.  
  315.  
  316. }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement