Beruflich Dokumente
Kultur Dokumente
q Sumário
l Definição. Características.
l Resolução de colisões: dispersão aberta e dispersão fechada. Teste quadrático.
l Desempenho de tabelas de dispersão.
l Interface de tabela de dispersão.
l Implementação de tabela de dispersão fechada com teste quadrático.
HASH (v2) - 1
Tabela de Dispersão
q Árvores binárias (de pesquisa) simples
l pior caso nas operações de manipulação é O(N)
l pior caso surge sistematicamente em usos correntes (ex: elementos previamente
ordenados)
q Tabela de dispersão
l pesquisa baseada na geração de um inteiro a partir da chave
l tempo médio constante para inserção, remoção e pesquisa
l não requer gestão de memória especial nem comparação de elementos
l ocorrência do pior caso tem probabilidade muito baixa
q Aplicações
l tabelas de símbolos dos compiladores
HASH (v2) - 2
0 0
1 1 81
2
{0, 1, 4, 81, 64} 3
1º último 4 4 64
5
6
7
8
9
HASH (v2) - 3
q Teste quadrático
l procurar próxima posição livre a seguir a H com passo quadrático
- tenta-se H0+12, H 0 + 22, H0 + 32, ...
l é preciso garantir que se pode percorrer a tabela toda - ver adiante
HASH (v2) - 4
l f(i) = i2
HASH (v2) - 5
HASH (v2) - 6
HASH (v2) - 7
HASH (v2) - 8
HASH (v2) - 9
1 1 1 1 como a
posição
2 2 2 2
add(13) add(23) remove(13) contains(23) está
3 13 3 13 3 13 3 13 marcada
4 4 4 4 como
23 23 23
marca apagada,
5 5 5 5 procura na
posição
como posição
6 6 6 6
apagada seguinte
7 7 7 7
8 8 8 8
9 9 9 9
HASH (v2) - 10
HASH (v2) - 11
/**
* This method is not part of standard Java 1.2.
* Like contains, it checks if x is in the set.
* If it is, it returns the reference to the matching
* object; otherwise it returns null.
*/
public Object getMatch( Object x )
{
int currentPos = findPos( x );
if( array[ currentPos ] == null )
return null;
return array[ currentPos ].element;
}
HASH (v2) - 12
/**
* Tests if item in pos is active.
* @param pos a position in the hash table.
* @param arr the HashEntry array (can be oldArray during rehash).
* @return true if this position is active.
*/
private static boolean isActive( HashEntry [ ] arr, int pos )
{
return arr[ pos ] != null && arr[ pos ].isActive;
}
HASH (v2) - 13
/**
* Adds an item to this collection.
*/
public boolean add( Object x )
{
int currentPos = findPos( x );
if( isActive( array, currentPos ) )
return false;
return true;
}
HASH (v2) - 14
HASH (v2) - 15
return true;
}
HASH (v2) - 16
HASH (v2) - 17
HASH (v2) - 18
do
{
currentPos++;
} while( currentPos < array.length &&
!isActive( array, currentPos ) );
visited++;
return array[ currentPos ].element;
}
HASH (v2) - 19
HASH (v2) - 20
HASH (v2) - 21
return currentPos;
}
HASH (v2) - 22
for( ; !isPrime( n ); n += 2 )
;
return n;
}
HASH (v2) - 23
if( n == 1 || n % 2 == 0 )
return false;
return true;
}
}
HASH (v2) - 24
HASH (v2) - 25
HASH (v2) - 26
package weiss.util;
/**
* Hash table implementation of the Map.
*/
public class HashMap extends MapImpl
{
public HashMap( )
{
super( new HashSet( ) );
}
HASH (v2) - 27
HASH (v2) - 28
HASH (v2) - 29
HASH (v2) - 30