-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTablaHash.java
More file actions
148 lines (119 loc) · 3.21 KB
/
TablaHash.java
File metadata and controls
148 lines (119 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.util.NoSuchElementException;
class NodoHT <K,V>{
private K llave;
private V valor;
public NodoHT(K key,V value){
super();
this.llave=key;
this.valor=value;
}
public K getLlave() {
return this.llave;
}
public V getValor() {
return this.valor;
}
public void setValor(V valor) {
this.valor = valor;
}
}
public class TablaHash<K, V>{
private ListaEnlazada<NodoHT<K, V>>[] tabla;
private final static double LOAD_FACTOR = 0.8;
private int size;
public TablaHash() {
this(17);
}
public TablaHash(int size) {
this.tabla = (ListaEnlazada<NodoHT<K, V>>[]) new ListaEnlazada[size];
for (int i = 0; i < this.tabla.length; i++) {
this.tabla[i] = new ListaEnlazada<NodoHT<K, V>>();
}
this.size = 0;
}
public void put(K llave, V valor) {
if ((double) this.size / this.tabla.length >= TablaHash.LOAD_FACTOR) {
rehashing();
}
int hash = Math.abs(llave.hashCode()) % tabla.length;
ListaEnlazada<NodoHT<K,V>> bucket = this.tabla[hash];
bucket.insertaFin(new NodoHT<K,V>(llave, valor));
this.size++;
}
public void rehashing() {
TablaHash<K,V> tablaTmp = new TablaHash<K,V>((this.tabla.length<< 1) + 1); //(this.tabla.length()*2) + 1 || (this.tabla.length()<< 1) + 1
ListaEnlazada<NodoHT<K, V>> bucket;
NodoHT<K, V> current;
for (int i = 0; i < this.tabla.length; i++) {
bucket = this.tabla[i];
for (int j = 0; j < bucket.size(); j++) {
current = (NodoHT<K, V>)bucket.getAt(j);
tablaTmp.put(current.getLlave(),current.getValor());
}
}
this.tabla=tablaTmp.tabla;
this.size=tablaTmp.size;
}
public V get(K llave) throws NoSuchElementException {
int hash = Math.abs(llave.hashCode()) % tabla.length;
ListaEnlazada<NodoHT<K, V>> bucket = this.tabla[hash];
NodoHT<K, V> current;
for (int i = 0; i < bucket.size(); i++) {
current =bucket.getAt(i);
if (current.getLlave().equals(llave)) {
return current.getValor();
}
}
throw new NoSuchElementException("No se encontro el valor");
}
public V delete(K llave) throws NoSuchElementException{
int hash = Math.abs(llave.hashCode()) % tabla.length;
ListaEnlazada<NodoHT<K, V>> bucket = this.tabla[hash];
NodoHT<K, V> current;
for (int i = 0; i < bucket.size(); i++) {
current =bucket.getAt(i);
if (current.getLlave().equals(llave)) {
return (V)bucket.borrarEn(i).getValor();
}
}
throw new NoSuchElementException("No se encontro el valor");
}
public void clear() {
this.tabla = (ListaEnlazada<NodoHT<K, V>>[]) new ListaEnlazada[11];
for (int i = 0; i < this.tabla.length; i++) {
this.tabla[i] = new ListaEnlazada<NodoHT<K, V>>();
}
this.size = 0;
System.gc();
}
public boolean containKey(K llave) {
try {
this.get(llave);
return true;
}catch(NoSuchElementException e) {
return false;
}
}
public static void main(String[] args) {
TablaHash<Integer,String> a= new TablaHash<>();
a.put(1,"one");
a.put(2, "valor");
a.put(3,"ff");
a.put(4,"ff");
a.put(5,"ff");
a.put(6,"ff");
a.put(7,"ff");
a.put(8,"ff");
a.put(9,"ff");
a.put(10,"ff");
a.put(11,"ff");
a.put(12,"ff");
a.put(13,"ff");
a.put(14,"ff");
a.put(15,"ff");
a.put(16,"ff");
a.put(17,"ff");
a.delete(2);
a.delete(1);
}
}