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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
| /**
* 使用开放寻址法解决冲突的散列表
* 支持线性探测、二次探测和双重散列
*/
public class OpenAddressingHashTable<K, V> {
/**
* 探测方法枚举
*/
public enum ProbeType {
LINEAR, // 线性探测
QUADRATIC, // 二次探测
DOUBLE_HASH // 双重散列
}
/**
* 键值对条目
*/
private static class Entry<K, V> {
K key;
V value;
boolean isDeleted; // 懒删除标记
Entry(K key, V value) {
this.key = key;
this.value = value;
this.isDeleted = false;
}
@Override
public String toString() {
return isDeleted ? "[已删除]" : key + "=" + value;
}
}
private Entry<K, V>[] table;
private int size;
private int capacity;
private ProbeType probeType;
private static final double LOAD_FACTOR_THRESHOLD = 0.5;
@SuppressWarnings("unchecked")
public OpenAddressingHashTable(int initialCapacity, ProbeType probeType) {
this.capacity = initialCapacity;
this.table = new Entry[capacity];
this.size = 0;
this.probeType = probeType;
}
public OpenAddressingHashTable(ProbeType probeType) {
this(16, probeType);
}
/**
* 主哈希函数
*/
private int hash1(K key) {
if (key == null) return 0;
return Math.abs(key.hashCode()) % capacity;
}
/**
* 辅助哈希函数(用于双重散列)
*/
private int hash2(K key) {
if (key == null) return 1;
return 7 - (Math.abs(key.hashCode()) % 7);
}
/**
* 探测函数
*/
private int probe(K key, int attempt) {
int h1 = hash1(key);
switch (probeType) {
case LINEAR:
return (h1 + attempt) % capacity;
case QUADRATIC:
return (h1 + attempt * attempt) % capacity;
case DOUBLE_HASH:
int h2 = hash2(key);
return (h1 + attempt * h2) % capacity;
default:
return h1;
}
}
/**
* 插入或更新键值对
*/
public V put(K key, V value) {
if ((double) size / capacity >= LOAD_FACTOR_THRESHOLD) {
resize();
}
for (int attempt = 0; attempt < capacity; attempt++) {
int index = probe(key, attempt);
Entry<K, V> entry = table[index];
// 位置为空或已删除,可以插入
if (entry == null || entry.isDeleted) {
table[index] = new Entry<>(key, value);
if (entry == null || entry.isDeleted) {
size++;
}
System.out.println("插入: " + key + " -> " + value +
" (位置: " + index + ", 探测次数: " + (attempt + 1) + ")");
return null;
}
// 键已存在,更新值
if (key.equals(entry.key)) {
V oldValue = entry.value;
entry.value = value;
System.out.println("更新: " + key + " -> " + value +
" (位置: " + index + ", 探测次数: " + (attempt + 1) + ")");
return oldValue;
}
}
throw new IllegalStateException("散列表已满,无法插入");
}
/**
* 获取值
*/
public V get(K key) {
for (int attempt = 0; attempt < capacity; attempt++) {
int index = probe(key, attempt);
Entry<K, V> entry = table[index];
if (entry == null) {
System.out.println("未找到: " + key + " (探测次数: " + (attempt + 1) + ")");
return null;
}
if (!entry.isDeleted && key.equals(entry.key)) {
System.out.println("找到: " + key + " -> " + entry.value +
" (位置: " + index + ", 探测次数: " + (attempt + 1) + ")");
return entry.value;
}
}
System.out.println("未找到: " + key + " (已探测整个表)");
return null;
}
/**
* 删除键值对(懒删除)
*/
public V remove(K key) {
for (int attempt = 0; attempt < capacity; attempt++) {
int index = probe(key, attempt);
Entry<K, V> entry = table[index];
if (entry == null) {
System.out.println("删除失败,未找到: " + key);
return null;
}
if (!entry.isDeleted && key.equals(entry.key)) {
entry.isDeleted = true;
size--;
System.out.println("删除: " + key + " -> " + entry.value +
" (位置: " + index + ", 探测次数: " + (attempt + 1) + ")");
return entry.value;
}
}
System.out.println("删除失败,未找到: " + key);
return null;
}
/**
* 扩容并重新散列
*/
@SuppressWarnings("unchecked")
private void resize() {
System.out.println("开始扩容: " + capacity + " -> " + (capacity * 2));
Entry<K, V>[] oldTable = table;
int oldCapacity = capacity;
capacity *= 2;
table = new Entry[capacity];
size = 0;
// 重新插入所有有效元素
for (int i = 0; i < oldCapacity; i++) {
Entry<K, V> entry = oldTable[i];
if (entry != null && !entry.isDeleted) {
put(entry.key, entry.value);
}
}
System.out.println("扩容完成,新容量: " + capacity);
}
/**
* 显示散列表状态
*/
public void display() {
System.out.println("\n散列表状态 (" + probeType + " 探测, 容量: " + capacity +
", 大小: " + size + ", 负载因子: " +
String.format("%.2f", (double) size / capacity) + "):");
for (int i = 0; i < capacity; i++) {
Entry<K, V> entry = table[i];
if (entry == null) {
System.out.println("位置 " + i + ": 空");
} else {
System.out.println("位置 " + i + ": " + entry);
}
}
}
/**
* 计算平均探测次数
*/
public double getAverageProbeCount() {
int totalProbes = 0;
int validEntries = 0;
for (int i = 0; i < capacity; i++) {
Entry<K, V> entry = table[i];
if (entry != null && !entry.isDeleted) {
// 重新计算这个元素的探测次数
for (int attempt = 0; attempt < capacity; attempt++) {
int index = probe(entry.key, attempt);
if (index == i) {
totalProbes += attempt + 1;
validEntries++;
break;
}
}
}
}
return validEntries == 0 ? 0 : (double) totalProbes / validEntries;
}
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
}
|