Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HASH--------------------------------------------------
- package hash;
- public class Hash {
- public Celda[] tabla;
- private int numElementos;
- private double alfaMax;
- private int capacidad;
- public Hash() {
- this.alfaMax = 0.8;
- this.numElementos = 0;
- tabla = new Celda[11];
- }
- public Hash(int capacidad) {
- if (esPrimo(capacidad) == true) {
- tabla = new Celda[capacidad];
- } else {
- while (esPrimo(capacidad) == false) {
- capacidad = siguientePrimo(capacidad);
- }
- tabla = new Celda[capacidad];
- }
- this.numElementos = 0;
- this.alfaMax = 0.8;
- }
- public Hash(int capacidad, double alfaMaximo) {
- if (esPrimo(capacidad) == true) {
- tabla = new Celda[capacidad];
- } else {
- while (esPrimo(capacidad) == false) {
- capacidad = siguientePrimo(capacidad);
- }
- tabla = new Celda[capacidad];
- }
- this.numElementos = 0;
- this.alfaMax = alfaMaximo;
- }
- public void insertar(int clave, int v) {
- this.numElementos++;
- if (factorCarga() > alfaMax) {
- redimensionar();
- }
- int posicion;
- Celda celda = new Celda(clave); //estado 0 = ocupado
- posicion = funcionHash(clave, 0); // k mod n
- try {
- if (hayColision(posicion) == false) { //No hay colision
- tabla[posicion] = celda;
- tabla[posicion].setClave(clave);
- tabla[posicion].setValor(v);
- tabla[posicion].setEstado(1);
- } else { // hay colision
- int contador = 1;
- while (hayColision(posicion) == true) {
- posicion = funcionHash(clave, contador); //colision * (7 - clave mod 7)
- hayColision(posicion);
- contador++;
- }
- tabla[posicion] = celda;
- tabla[posicion].setClave(clave);
- tabla[posicion].setValor(v);
- tabla[posicion].setEstado(1);
- }
- } catch (Exception e) {
- System.err.print(e);
- }
- }
- public void borrar(int clave) {
- int colision = 0;
- int posicion = funcionHash(clave, colision);
- try {
- while (tabla[posicion].getClave() != clave && colision != tabla.length) {
- posicion = funcionHash(clave, colision);
- colision++;
- }
- tabla[posicion].setEstado(-1);
- tabla[posicion].setValor(0);
- this.numElementos = this.numElementos - 1;
- } catch (Exception e) {
- System.err.println(e);
- }
- }
- public void buscar(int clave) {
- int colision = 0;
- int posicion = funcionHash(clave, colision);
- final int posAr = 0;
- try {
- while (tabla[posicion].getClave() != clave && colision != tabla.length){
- posicion = funcionHash(clave,colision);
- colision ++;
- }
- } catch (Exception e) {
- System.err.println(e);
- }
- System.out.println("La posicion es " + posicion);
- }
- public int get(int clave) {
- int colision = 0;
- int posicion = funcionHash(clave, colision);
- try {
- if (tabla[posicion].getClave() == clave) {
- if (tabla[posicion].getEstado() == 1) {
- return tabla[posicion].getValor();
- } else {
- return 0;
- }
- } else {
- while (tabla[posicion].getClave() != clave) {
- colision++;
- posicion = funcionHash(clave, colision);
- }
- if (tabla[posicion].getEstado() == 1) {
- return tabla[posicion].getValor();
- } else {
- return 0;
- }
- }
- } catch (Exception e) {
- System.err.println(e);
- }
- return 0;
- }
- public boolean esVacia() {
- boolean comprobar = true;
- if (this.numElementos == 0) {
- return comprobar;
- } else {
- comprobar = false;
- }
- return comprobar;
- }
- public double getAlfa() {
- return alfaMax;
- }
- public void setAlfa(double alfa) {
- this.alfaMax = alfa;
- }
- public int getNumElementos() {
- return this.numElementos;
- }
- public double factorCarga() {
- return (double) this.numElementos / tabla.length;
- }
- private boolean hayColision(int posicion) {
- boolean colision = true;
- if (this.tabla[posicion] == null) {
- colision = false;
- }
- return colision;
- }
- private int funcionHash(int clave, int colisiones) {
- int numero = hash1(clave) + hash2(clave, colisiones);
- return numero;
- }
- private int hash1(int clave) {
- int numero = clave % tabla.length;
- return numero;
- }
- private int hash2(int clave, int colisiones) {
- int numero = colisiones * (7 - (clave % 7));
- return numero;
- }
- private void redimensionar() {
- Celda[] copia = this.tabla;
- int tamano = tabla.length * 2;
- int tamanoAumentado = 0;
- this.numElementos = 0;
- try {
- if (esPrimo(tamano) == false) {
- tamanoAumentado = siguientePrimo(tamano);
- }
- this.tabla = new Celda[tamanoAumentado];
- int clave;
- int valor;
- for (Celda copia1 : copia) {
- if (copia1 != null) {
- clave = copia1.getClave();
- valor = (int) copia1.getValor();
- insertar(clave, valor);
- }
- }
- } catch (Exception e) {
- System.err.println(e);
- }
- }
- private boolean esPrimo(int numero) {
- boolean primo = true;
- int contador = 2;
- while (contador != numero && primo) {
- if (numero % contador == 0) {
- primo = false;
- }
- contador++;
- }
- return primo;
- }
- private int siguientePrimo(int numero) {
- if (esPrimo(numero) == true) {
- return numero;
- } else {
- while (esPrimo(numero) == false) {
- numero++;
- }
- }
- return numero;
- }
- }
- CELDA---------------------------------------------------------------------------------------------------------------------------------
- package hash;
- public class Celda{
- private int clave;
- private int valor;
- private int estado;
- /**
- * Constructor por defecto
- */
- public Celda() {
- }
- /**
- * Constructor al que se le pasa un entero clave
- *
- * @param clave
- */
- public Celda(int clave) {
- this.clave = clave;
- }
- /**
- *
- * @param estado
- */
- public void setEstado(int estado) {
- this.estado = estado;
- }
- /**
- *
- * @return
- */
- public int getClave() {
- return clave;
- }
- public void setClave(int clave) {
- this.clave = clave;
- }
- public int getEstado() {
- return this.estado;
- }
- /**
- *
- * @param valor
- */
- public void setValor(int valor) {
- this.valor = valor;
- }
- /**
- *
- * @return
- */
- public int getValor() {
- return valor;
- }
- }
- MAIN---------------------------------------------------------------------------------------------------------------------------------
- package hash;
- public class Tester {
- public static void main(String[] args){
- Hash miTabla = new Hash (25,0.8);
- for (int i = 0; i < 100; i ++){
- int dni = (int)Math.round(Math.random()*100000000);
- miTabla.insertar(dni, dni);
- }
- for(int i = 0;i<miTabla.tabla.length;i++){
- System.out.println(miTabla.tabla[i]);
- }
- //miTabla.buscar(); NOTA* Al ser creados con la funcion random es imposible buscar uno y coincidir pero
- // introduciendolo a mano si te devuelve la posicion en el array.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement