Update 算法 - 符号表.md

二分查找符号表在数组已满时扩容。
This commit is contained in:
CyC2018
2026-10-31 14:03:54 +08:00
parent b931e0783d
commit 8b057da93e
+8 -1
View File
@@ -216,7 +216,9 @@ public class BinarySearchOrderedST<Key extends Comparable<Key>, Value> implement
values[index] = value;
return;
}
// 否则在数组中插入新的节点,需要先将插入位置之后的元素都向后移动一个位置
// 否则在数组中插入新的节点:数组已满时先扩容为原来的两倍,再将插入位置之后的元素都向后移动一个位置
if (N == keys.length)
resize(Math.max(1, 2 * keys.length));
for (int j = N; j > index; j--) {
keys[j] = keys[j - 1];
values[j] = values[j - 1];
@@ -226,6 +228,11 @@ public class BinarySearchOrderedST<Key extends Comparable<Key>, Value> implement
N++;
}
private void resize(int capacity) {
keys = Arrays.copyOf(keys, capacity);
values = Arrays.copyOf(values, capacity);
}
@Override
public Value get(Key key) {
int index = rank(key);