记录B站视频中数据结构哈希表扩容
在封装哈希表的是时候用的是this.count来记录填装因子,但是填装因子记录的是bucket里面的数据,在完成所有的哈希表数据添加之后,this.count记录的是所有的填装因子,概率存在
[ [ [],[],[],[],[],[],[] ] ,[] , [] , [] , [] , [] , [] ]
这样的数据结构,bucket只用了一个,但是总的填装因子大于0.75,这样也会导致扩容,所以可以用bucket的有值数量来决定是否需要扩容
//只有在bucket在大于0.75时才能扩容
let indey = 0;
this.storage.map((item) => {
if ([item]) {
indey++;
}
return indey;
});
const num = indey / this.limt;
if (num > 0.75) {
const newsize = this.limt * 2;
const prime = this.getPrime(newsize);
this.resize(prime);
}
测试
image.png
在填装因子大于0.75的情况下。没有进行扩容,此时打印的数据是已经使用bucket的数量比上总的bucket数量