diff --git a/notes/算法 - 符号表.md b/notes/算法 - 符号表.md index 426e94ef..68e2c9d2 100644 --- a/notes/算法 - 符号表.md +++ b/notes/算法 - 符号表.md @@ -216,7 +216,9 @@ public class BinarySearchOrderedST, 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, 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);