0% encontró este documento útil (0 votos)
115 vistas

Codigo Java Tabla Hash

Este documento describe las operaciones y clases principales de una tabla de dispersión (HashTable). Define una interfaz IHashTable con métodos como clear(), contains(), get(), put() y remove(). Luego describe la clase HashTable que implementa esta interfaz, incluyendo métodos para agregar, eliminar y buscar entradas, así como resize de la tabla cuando se llena. También describe la clase interna Entry que almacena cada par clave-valor y enlaza las entradas.

Cargado por

sodaxperia
Derechos de autor
© © All Rights Reserved
Formatos disponibles
Descarga como PPT, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
115 vistas

Codigo Java Tabla Hash

Este documento describe las operaciones y clases principales de una tabla de dispersión (HashTable). Define una interfaz IHashTable con métodos como clear(), contains(), get(), put() y remove(). Luego describe la clase HashTable que implementa esta interfaz, incluyendo métodos para agregar, eliminar y buscar entradas, así como resize de la tabla cuando se llena. También describe la clase interna Entry que almacena cada par clave-valor y enlaza las entradas.

Cargado por

sodaxperia
Derechos de autor
© © All Rights Reserved
Formatos disponibles
Descarga como PPT, PDF, TXT o lee en línea desde Scribd
Está en la página 1/ 14

TABLAS DE DISPERSION

Public Interface IHashTable


{
public void clear();
boolean containeKey( Object Key);
boolean containeValue(Object Value);
Object get(object Key);
boolean isEmpty();
Object put(Object Key, Object Value);
void put(Object Key, Object Value);
void putAll(IHashTable t)
public Object remove ( Object Key);
public int size();
}
OPERACIONES DE TABLAS DE DISPERSION.
Clear() : Lo convierte en vacio
containeKey(Object Key) : Retorna verdadero, si la tabla contiene a Key como clave
containeValue (Object Valor): Retorna verdadero, si la tabla contiene a value
como valor.
Get(Object Key) : Retorna el valor cuya clave es Key.
isEmpty() : Retorna verdad si tabla es vacio.
Put(Object Key, Object Value) : Inserta el par Key en la tabla.
PutAll(IHashTable t) : Inserta todos los pares clave valor en la tabla, de
acuerdo a put.
Remove(Object Key): Elimina el par clave valor cuya clave es Key.
Size(): retorna la cantidad de elementos que hay actualmente
en la tabla.
Public class Entry1
{
Private Object Key;
Private Object value;
Private Entry1 next;
Public Entry1(Object Key, Object Value)
{ This.Key = Key;
This.Value = Value; }
Public Object getKey()
{return HashTable1.unmaskNull(key);}
Public Object getValue()
{return value}
Public Object setValue(object newValue){
object oldValue =value;
Value = newValue;
Return oldValue;}
Public void setNext(Entry1 next)
{ This.next = next;}
Public Entry1 getNext()
{return next}
Public String ToString()
{ return getKey() + “-”+getValue();}
} //termina class Entry1.
Public class HashTable1 implements IHashTable
{ private Entry1 table[ ];
private int size;
private final float loadFactor;
private int ThreeHold;
private final int DEFAULT_INICIAL_CAPACITY = 4;
private final float DEFAULT_LOAD_FACTOR= 0.75;
public HashTable1() {
load Factor = DEFAULT_LOAD_FACTOR;
ThreeHold = DEFAULT_INICIAL_CAPACITY;
Table = new Entry1(DEFAULT_INICIAL_CAPACITY);
}
public HashTable1( int initialCapacity, float loadFactor)
{ if (initalCapacity <0){
throw new Illegal ArgumentException(“Ilegal initial
capacity:”+ initialCapacity ); }
if (loadFactor <= 0 ) {// loadFactor > 1
throw new Illegal ArgumentException(“Ilegal load
factor:”+ loadFacot); }
//encuentre una potencia de 2 > initialCapacity
Int capacity =1;
While (capacity < initialCapacity)
{ capacity = capacity *2; }
This.loadFactor = loadFactor;
Threehold=(int)(cpacity *loadFactor);
Table = new Entry1(capacity);

}
Public HashTable1(int InitialCapacity)
{ this(initialCapacity,0.75F); }

Public void clear()


{ Entry1 tab[ ] = table;
For( int i= 0; i<tab.length; i++) { tab[i]=null; }
Size=0;
}

Public boolean containeKey(Object Key)


{ Object k= maskNull(key);
int i = hCode(k,table.length); // FUNCION DE DISPERSION
Entry1 e=table[i];
While( e != null) { if (eq(k,maskNull(e.getKey())))
{ return true; }
e = e.getNext();
} return false;
}
public boolean containsValue(Object value)
{
if (value == null) {
return containsNullValue();
}
Entry1 tab[] = table;
for(int i=0; i<tab.length; i++) {
for(Entry1 e=tab[i]; e!=null; e=e.getNext()) {
if(value.equals(e.getValue())) {
return true;
}
}
}
return false;
}
Public Object get(Object Key)
{ Object K=maskNull(Key); //codigo
int i = hCode(K,table.length); //Hash
Entry1 e = table[i];
while (true)
{ if ( e == null)
{ return e; // retorna null; }
if ( eq(K,maskNull(e.getKey())))
{ return e.getValue(); }
e=e.getNext();
}
}
Public boolean isEmpty()
{ return size == 0; }

Public Object put(Object Key, Object Value)


{ Object k = maskNull(Key);
int i= hCode(K,Table.length);
for (Entry1 e=table[i]; e != null; e=e.getNext())
{ if (eq(k,maskNull(e.getKey()))
{ Object oldValue = e.getValue();
e.setValue(Value);
Return oldValue;
}
}
addEntry(k,value,i);
Return null; // después de insertar un valor
} // El put si es que existía, reemplaza el antiguo
Public Object remove(Object Key)
{ Entry1 e = removeEntryForKey(Key);
Return(e==null ? e : e.getValue()); // si no existe retorna null

Public int size()


{ return size;}

Public String toString()


{ StringBuffer sb= StringBuffer();
Entry1 tab[ ] = table;
for(int i=0; i < tab.length; i++) {
for( Entry1 e =tab[i]; e != null; e=e.getNext())
{ sb.append(e+” “);
}
} return sb.toString();
}
Public static final Object NULL_KEY = new Object();
Public static Object makNull(Object Key)
{ return (key == null ? NULL_KEY : key);
} // si Key = null, retun NULL_KEY sino return KEY

Public static Object unmaskNull( Object Key)


{ return (Key == NULL_KEY ? Null:Key); }

Private boolean ContaineNullValue()


{ Entry1 tab[ ] = table;
for(int i=0; i < tab.length: i++) {
for( Entry1 e =tab[i]; e != null; e=e.getNext())
{ if ( e.getValue() == null) { return true; }
}
}return false;
}
Private int hCode (Object k, int Capacity)
{ return maskNull(K).hashCode() & (Capacity -1);
// return capacity % k.hashCode()
}

Private static boolean eq(Object x, Object y)


{ return x== y }

Private void addEntry(Object key, object value, int index)


{ Entry1 newEntry = new Entry1(key,value);
newEntry.setNext( table[index]);
Table[index] = newEntry;
if(size++ ==ThreeHold) { resize( 2 * table.length());
}
private Entry1 removeEntryForKey(Object key) {
//Elimina y retorna la entrada asociada con key en esta tabla.
//Retorna null si la tabla no contiene a key como clave de algún valor
Object k = maskNull(key);
int i = hCode(k, table.length);//código hash//<-OjO
Entry1 previous = null;
for(Entry1 e=table[i]; e!=null; e=e.getNext()) {
if(eq(k, maskNull(e.getKey()))) {
if(e == table[i]) {
table[i] = e.getNext();
} else {
previous.setNext(e.getNext());
}
size--;
return e;
}
previous = e;
}
return null;
}
}

También podría gustarte