1 / 18

public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA //tabella hash

public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA //tabella hash private int[] table; //dimensione della tabella (meglio se numero primo) private int dim; //METODI // Costruttore. Inizializza le variabili di istanza public SimpleHash(int dim) {}

Télécharger la présentation

public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA //tabella hash

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA //tabella hash private int[] table; //dimensione della tabella (meglio se numero primo) private int dim; //METODI // Costruttore. Inizializza le variabili di istanza public SimpleHash(int dim) {} // Inizializza ogni elemento della tabella a -1 (nessun valore inserito) public void initialize() {} //Cerca l'elemento v all'interno della tabella restituisce la posizione in cui si trova l’elemento, -1 se questo non e’ presente. public int search(int v){} //Inseriscel'elemento v nella tabella se la tabella non e' piena e se l'elemento non e' gia' presente public void insert(int v){} //Cancella l'elemento v dalla tabella se presente. Resituisce false se l'elemento non viene cancellato, true altrimenti public boolean cancel(int v){} //Stampa la tabella come una stringa public String toString(){} }

  2. /**************************************************************************************************************************/************************************************************************************************************************** *costruttore. Istanzia l'array con un numero di elementi pari a dim (l'array va da 0 a n-1) * * Inizializza la dimensione della tabella (non sarebbe necessario avere una variabile esplicita * * per la dimensione - metodo length) * **************************************************************************************************************************/ public SimpleHash(int dim) { table = new int[dim]; this.dim = dim; } /******************************************************************************************************************* *Inizializza ogni elemento della tabella a -1 (nessun valore inserito) - potrei voler inserire il valore 0, * *quindi l'inizializzazione di default (0) non va bene * *******************************************************************************************************************/ public void initialize() { for (int i = 0; i < dim; i++) table[i] = -1; }

  3. /*******************************************************************************************************************/******************************************************************************************************************* * Cerca l'elemento v all'interno della tabella. Restituisce l'indice della posizione in cui e' inserito * * l'elemento se presente in tabella, -1 altrimenti * ******************************************************************************************************************/ public int search(int v) { int key = v % dim; int j = key; while((table[j] != v) && (table[j] != -1) && ((j+1)%dim != key)) j = (j+1)%dim; if(table[j] == v) return j; else return -1; } /******************************************************************************************************************* *Inserisce l'elemento v nella tabella se la tabella non e' piena e se l'elemento non e' gia' presente * *******************************************************************************************************************/ public void insert(int v) { int key = v % dim; int j = key; while((table[j] != v) && (table[j] != -1) && ((j+1) % dim != key)) j = (j+1) % dim; if(table[j] == -1) { table[j] = v; System.out.println("Elemento "+v+" inserito in posizione "+j); } else if(table[j] == v) System.out.println("Elemeno già inserito"); else System.out.println("Impossibile inserire l'elemento: tabella piena"); }

  4. /**************************************************************************************************************************/************************************************************************************************************************** *Cancella l'elemento v dalla tabella se presente. Resituisce false se l'elemento non viene cancellato, true * *altrimenti * ***************************************************************************************************************************/ public boolean cancel(int v) { int pos = search(v); if(pos == -1) return false; table[pos] = -1; return true; } /****************************************************************************************************************** *Stampa la tabella come una stringa ******************************************************************************************************************/ public String toString() { String out = new String("["); int i; for(i = 0; i < dim-1; i++) out = out + table[i] +", "; out = out + table[i]+"]"; return out; }

  5. public class SimpleHashTest { public static void main(String[] argv) { SimpleHash hash = new SimpleHash(5); System.out.println(hash.toString()); hash.initialize(); System.out.println(hash.toString()); hash.insert(61); System.out.println(hash.toString()); hash.insert(15); System.out.println(hash.toString()); hash.insert(5); System.out.println(hash.toString()); hash.insert(4); System.out.println(hash.toString()); hash.insert(24); System.out.println(hash.toString()); hash.insert(0); System.out.println("Elemento 1 in posizione: "+hash.search(1)); System.out.println("Elemento 33 cancellato :" +hash.cancel(33)); System.out.println("Elemento 61 cancellato: "+ hash.cancel(61)); System.out.println(hash.toString()); System.out.println("Elemento 24 in posizione" + hash.search(24)); hash.insert(24); System.out.println(hash.toString()); } }

  6. dim 5 table 0 0 0 0 0 0 1 2 3 4 i i i i 5 2 1 0 table table table table -1 -1 -1 -1 -1 -1 0 -1 -1 -1 0 0 0 0 -1 0 0 -1 0 0 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 SimpleHash hash = new SimpleHash(5) public SimpleHash(int dim) { table = new int[dim]; this.dim = dim; } hash.initialize(); public void initialize() { for (int i = 0; i < dim; i++) table[i] = -1; }

  7. key key v v key dim j key j j j v v 2 0 0 5 5 1 0 0 0 5 2 5 5 table table table table 15 15 15 15 61 61 61 61 -1 -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 hash.insert(5) public void insert(int v) { int key = v % dim; int j = key; while((table[j] != v) && (table[j] != -1) && ((j+1) % dim != key)) j = (j+1) % dim; if(table[j] == -1) { table[j] = v; System.out.println("Elemento "+v+" inserito in posizione "+j); return; } if(table[j] == v) { System.out.println("Elemeno già inserito"); return; } System.out.println("Impossibile inserire l'elemento: tabella piena"); } table[j] table[j] table[j]

  8. v key j j v j j v key key key j key v v 0 1 3 1 4 1 1 1 1 1 1 2 1 1 1 table table table table table 15 15 15 15 15 61 61 61 61 61 5 5 5 5 5 24 24 24 24 24 4 4 4 4 4 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 table[j] hash.search(1) public int search(int v) { int key = v % dim; int j = key; while((table[j] != v) && (table[j] != -1) && ((j+1)%dim != key)) j = (j+1)%key; if(table[j] == v) return j; return -1; } table[j] table[j] table[j] table[j]

  9. i out out out i i i out out i i out out [15,61,5,24,4] [15,61, [ [15,61,5,24, [15,61,5,24, [15,61,5, [15, table table table table table table 15 15 15 15 15 15 61 61 61 61 61 61 5 5 5 5 5 5 24 24 24 24 24 24 4 4 4 4 4 4 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 0 hash.toString() public String toString() { String out = new String("["); int i; for(i = 0; i < dim-1; i++) out = out + table[i] +", "; out = out + table[i]+"]"; return out; } 1 2 3 4

  10. j v j key v j key v key 0 4 24 4 4 24 4 24 1 table table table 15 15 15 -1 -1 -1 5 5 5 24 24 24 4 4 4 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 hash.search(24) public int search(int v) { int key = v % dim; int j = key; while((table[j] != v) && (table[j] != -1) && ((j+1)%dim != key)) j = (j+1)%key; if(table[j] == v) return j; return -1; } table[j] table[j] table[j]

  11. Esempio di Soluzione

  12. Nuova classe che specifica il tipo degli elementi della tabella hash public class Elem { private int value; private int link; public Elem() { value = -1; link = -1; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public int getLink() { return link; } public void setLink(int link) { this.link = link; } } Ogni elemento è composto da due interi: il valore dell’elemento ed il link – posizione dell’elemento in cui è memorizzato il successivo elemento associato alla stessa chiave. Costruttore: inizializza entrambe le variabili d’istanza a -1. All’inizio ogni elemento avrà il valore -1 e non ci saranno associazioni posizioni - chiavi Metodo getValue(): Restituisce il valore della variabile value Metodo setValue(int value): Assegna alla variabile value (di questo Elem) il valore del parametro formale value Metodo getLink(): Restituisce il valore della variabile link Metodo setLink(int link): Assegna alla variabile link (di questo Elem) il valore del parametro formale link

  13. public void insert(int v) { int j = v%dim; while((table[j].getValue() != v) && (table[j].getLink() != -1)) j = table[j].getLink(); if(table[j].getValue() == v) { System.out.println("Elemento "+v+" gia' inserito"); return; } int firstFree = getFirstFree(v%dim); if(firstFree == -1) { System.out.println("Impossibile inserire: tabella piena"); return; } if(firstFree != v%dim) table[j].setLink(firstFree); table[firstFree].setValue(v); System.out.println("Elemento "+v+" inserito in posizione"+firstFree); } public SimpleHash(int dim) { table = new Elem[dim]; this.dim = dim; } public void initialize() { for (int i = 0; i < dim; i++) table[i] = new Elem(); } public int search(int v) { int j = v % dim; while ((table[j].getValue() != v) && (table[j].getLink() != -1)) j = table[j].getLink(); if(table[j].getValue() == v) return j; return -1; }

  14. public boolean cancel(int v) { int j = v % dim; int prev = -1; while ((table[j].getValue() != v) && (table[j].getLink() != -1)) { prev = j; j = table[j].getLink(); } if(table[j].getValue() == v) { if(prev != -1) { table[prev].setLink(table[j].getLink()); table[j].setValue(-1); table[j].setLink(-1); return true; } if(table[j].getLink() == -1) { table[j].setValue(-1); return true; } int value = table[table[j].getLink()].getValue(); int link = table[table[j].getLink()].getLink(); table[j].setValue(value); table[table[j].getLink()].setValue(-1); table[table[j].getLink()].setLink(-1); table[j].setLink(link); return true; } return false; } public String toString() { String out = new String("["); int i; for(i = 0; i < dim-1; i++) out = out + "(" +table[i].getValue()+","+table[i].getLink()+")" +", "; out = out + "(" +table[i].getValue()+","+table[i].getLink()+")"+"]"; return out; } public int getFirstFree(int from) { int j = from; while(((j+1) % dim != from) && (table[j].getValue() != -1)) j = (j+1)%dim; if (table[j].getValue() == -1) return j; return -1; }

  15. public class SimpleHashTest1 { public static void main(String[] argv) { SimpleHash3 hash = new SimpleHash3(5); hash.initialize(); System.out.println(hash.toString()); hash.insert(61); System.out.println(hash.toString()); hash.insert(15); System.out.println(hash.toString()); hash.insert(5); System.out.println(hash.toString()); hash.insert(4); System.out.println(hash.toString()); hash.insert(24); System.out.println(hash.toString()); hash.insert(0); System.out.println("Elemento 1 in posizione: "+hash.search(1)); System.out.println("Elemento 4 cancellato :" +hash.cancel(4)); System.out.println("Elemento 61 cancellato: "+ hash.cancel(61)); System.out.println(hash.toString()); System.out.println("Elemento 24 in posizione" + hash.search(24)); hash.insert(24); System.out.println(hash.toString()); } }

  16. table table table table 15 -1 -1 15 -1 -1 2 -1 61 61 61 -1 -1 -1 -1 -1 -1 -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 hash.initialize(); hash.insert(61); hash.insert(15); hash.insert(5);

  17. table table table table 15 15 15 15 2 2 2 2 -1 61 61 61 -1 -1 -1 -1 5 5 5 5 24 24 4 4 -1 3 -1 -1 -1 -1 -1 -1 -1 24 -1 -1 -1 -1 -1 -1 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 hash.insert(4); 4 3 hash.insert(24); hash.cancel(4); hash.cancel(61);

More Related