Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tabelahash;
- public class TabelaHashDuplo<T extends Itemble > extends TabelaHash{
- TabelaHashDuplo(int tam){
- super(tam);
- }
- public void inserir(Itemble elem) {
- int indice = hash(elem.getKey());
- if(tabela[indice] == null)
- tabela[indice] = elem;
- else {
- int tentativa = 1;
- int indice2 = indice + tentativa * hash2(elem.getKey());
- indice2 = indice2 % tabela.length;
- if(indice2<0){
- indice *= (-1);
- }
- while( tabela[indice2] != null ) {
- tentativa++;
- indice2 = indice + tentativa * hash2(elem.getKey());
- indice2 = indice2 % tabela.length;
- if(indice2<0){
- indice *= (-1);
- }
- //voltando
- if (indice2 >= tabela.length) {
- indice2 = 0;
- }
- }
- tabela[indice2] = elem;
- }
- }
- public Itemble buscar(String key){
- int indice = hash(key);
- if(tabela[indice].getKey().equalsIgnoreCase(key)){
- System.out.println("Não colidiu");
- return tabela[indice];
- } else {
- int tentativa = 1;
- int indice2 = indice + tentativa * hash2(key);
- indice2 = indice2 % tabela.length;
- while(tabela[indice2]==null){
- tentativa++;
- indice2 = indice + tentativa * hash2(key);
- indice2 = indice2 % tabela.length;
- if(indice2<0){
- indice2 *=(-1);
- }
- }
- while((!tabela[indice2].getKey().equalsIgnoreCase(key))
- && (indice2 < tabela.length) ) {
- tentativa++;
- if(tentativa>=tabela.length){
- return null;
- }
- indice2 = indice + tentativa * hash2(key);
- indice2 = indice2 % tabela.length;
- if(indice2<0){
- indice2 *=(-1);
- }
- while(tabela[indice2]==null){
- tentativa++;
- indice2 = indice + tentativa * hash2(key);
- indice2 = indice2 % tabela.length;
- if(indice2<0){
- indice2 *=(-1);
- }
- }
- //voltando
- if (indice2 >= tabela.length) {
- indice2 = 0;
- }
- }
- if(indice == tabela.length)
- return null;
- return tabela[indice2];
- }
- }
- public int hash2(String key) {
- int indice = 0;
- for(int i = key.length()-1; i >= 0; i--) {
- int c = (int) key.charAt(i);
- indice += c * Math.pow(26, key.length() - i);
- }
- return indice % tabela.length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement