Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Classe Hash principal (Não mexi, tá igual a de Luiz)
- ---------------------------------------------------------------------------------------------------------------
- //TabelaHashLinear
- public class TabelaHashLinear<T extends Itemble > extends TabelaHash<Itemble> {
- TabelaHashLinear(int tam){
- super(tam);
- }
- @Override
- public void inserir(Itemble elem) {
- int indice = hash(elem.getKey());
- while( (tabela[indice] != null) ) {
- indice++;
- if (indice >= tabela.length) {
- indice = 0;
- }
- }
- tabela[indice] = elem;
- }
- @Override
- public Itemble buscar(String key) {
- int indice = hash(key);
- while((tabela[indice]!=null) && (!tabela[indice].getKey().equalsIgnoreCase(key))
- && (indice < tabela.length)){
- indice++;
- if (indice >= tabela.length) {
- indice = 0;
- }
- }
- if(indice == tabela.length)
- return null;
- return tabela[indice];
- }
- }
- ---------------------------------------------------------------------------------------------------------------
- //TabelaHashQuadrática
- public class TabelaHashQuadratico<T extends Itemble > extends TabelaHash{
- public TabelaHashQuadratico(int tam) {
- super(tam);
- }
- @Override
- public void inserir(Itemble elem) {
- int tentativa = 0;
- int indice = hash(elem.getKey());
- while( (tabela[indice] != null) ) {
- indice+= Math.pow(++tentativa, 2);
- indice= indice%tabela.length;
- //voltando
- if (indice >= tabela.length) {
- indice = 0;
- }
- }
- tabela[indice] = elem;
- }
- @Override
- public Itemble buscar(String key) {
- int indice = hash(key);
- int tentativa = 0;
- while( (tabela[indice]!=null) &&
- (!tabela[indice].getKey().equalsIgnoreCase(key))
- && (indice < tabela.length) ){
- indice += Math.pow(++tentativa, 2);
- if (indice >= tabela.length) {
- indice = 0;
- }
- }
- if(indice == tabela.length)
- return null;
- return tabela[indice];
- }
- }
- ---------------------------------------------------------------------------------------------------------------
- //TabelaHashDupla (Não fiz o método de busca ainda, mas verifiquei o inserir)
- 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());
- while( tabela[indice2] != null ) {
- tentativa++;
- indice2 = indice + tentativa * hash2(elem.getKey());
- //voltando
- if (indice2 >= tabela.length) {
- indice2 = 0;
- }
- }
- tabela[indice] = elem;
- }
- }
- public Itemble buscar(String key){
- return null;
- }
- 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;
- }
- }
- -----------------------------------------------------------------------------------------------------------------
- //TabelaHashSeparada (Pelos meus testes, essa classe tá OK, então não mexi nela)
- Ressalva: O Item que Luis fez tinha 3 atributos, reduzi pra um e organizei o toString, mas foi coisa besta.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement