La presentazione è in caricamento. Aspetta per favore

La presentazione è in caricamento. Aspetta per favore

Public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA //tabella hash private int[] table; //dimensione della tabella (meglio se numero primo)

Presentazioni simili


Presentazione sul tema: "Public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA //tabella hash private int[] table; //dimensione della tabella (meglio se numero primo)"— Transcript della presentazione:

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){} //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){} //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 SimpleHash hash = new SimpleHash(5) public SimpleHash(int dim) { table = new int[dim]; this.dim = dim; } 5 dim table 01234 00000 hash.initialize(); public void initialize() { for (int i = 0; i < dim; i++) table[i] = -1; } 0000 table 01234 i 0 000 table 01234 i 1 00 table 01234 i 2 table 01234 i 5

7 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"); } 1561 table 0123 4 key 0 j 0 v 5 dim 5 1561 table 0123 4 key 0 j 1 v 5 1561 table 0123 4 key 0 j 2 v 5 table[j] 15615 table 0123 4 key 0 j 2 v 5 table[j]

8 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; } 15615244 table 0123 4 key 1 j 1 v 1 table[j] 15615244 table 0123 4 key 1 j 2 v 1 table[j] 15615244 table 0123 4 key 1 j 3 v 1 table[j] 15615244 table 0123 4 key 1 j 4 v 1 table[j] 15615244 table 0123 4 key 1 j 0 v 1 table[j]

9 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; } 15615244 table 0123 4 i out [ 15615244 table 0123 4 i out [15, 0 15615244 table 0123 4 i out [15,61, 1 15615244 table 0123 4 i out [15,61,5, 2 15615244 table 0123 4 i out [15,61,5,24, 3 15615244 table 0123 4 i out [15,61,5,24, 4 out [15,61,5,24,4]

10

11 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; } 155244 table 0123 4 key 4 j 4 v 24 table[j] 155244 table 0123 4 key 4 j 0 v 24 table[j] 155244 table 0123 4 key 4 j 1 v 24 table[j]

12 Esempio di Soluzione

13 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; } Nuova classe che specifica il tipo degli elementi della tabella hash 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

14 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; } 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); }

15 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; } 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; }

16 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()); }

17 hash.initialize(); table 01234 hash.insert(61); table 01234 61 hash.insert(15); table 01234 1561 hash.insert(5); table 01234 152615

18 hash.insert(4); table 01234 152615 4 hash.insert(24); table 01234 152615 24 43 4 3 hash.cancel(4); table 01234 152615 24 hash.cancel(61); table 01234 152 5 24


Scaricare ppt "Public class SimpleHash { //DICHIARAZIONE DELLE VARIABILI DI ISTANZA //tabella hash private int[] table; //dimensione della tabella (meglio se numero primo)"

Presentazioni simili


Annunci Google