//設計哈希函數
//1>將字符串轉成比較大的數字:hashCode
//2>將大的數字hashCode壓縮到數組范圍
function hashFunc(str,size){
//1.定義hashCode變量
var hashCode=0
//2.霍納算法,來計算 hashCode的值
for(var i=0;i<str.length;i++){
hashCode=37* hashCode + str.charCodeAt(i) //獲取編碼
}
//3.取余狀態
var index=hashCode%size
return index
}
//測試哈希函數
alert(hashFunc('abc',7))
alert(hashFunc('cba',7))
alert(hashFunc('nba',7))
alert(hashFunc('mba',7))
function HashTable(){
//屬性
this.storage=[]
this.count=0
this.limit=7
//方法
//哈希函數
HashTable.prototype.hashFunc= function (str,size){
//1.定義hashCode變量
var hashCode=0
//2.霍納算法,來計算 hashCode的值
for(var i=0;i<str.length;i++){
hashCode=37* hashCode + str.charCodeAt(i) //獲取編碼
}
//3.取余狀態
var index=hashCode%size
return index
}
//插入和修改操作
HashTable.prototype.put= function (key,value){
//1。根據key 獲取對應的 index
var index=this.hashFunc(key,this.limit)
//2.根據 index取出對應的bucket
var bucket=this.storage[index]
//3.判斷bucket 是否為 null
if(bucket==null){
bucket=[]
this.storage[index]=bucket
}
//4.判斷是否是修改數據
for(var i=0;i<bucket.length;i++){
var tuple=bucket[i]
if(tuple[0]==key){
tuple[1]=value
return
}
}
//5.進行添加操作
bucket.push([key,value])
this.count+=1
//6.判斷是否需要擴容操作
if(this.count>this.limit*0.75){
this.resize(this.limit*2)
}
}
//獲取操作
HashTable.prototype.get=function(key){
//1.根據 key獲取對應的 index
var index=this.hashFunc(key,this.limit)
//2.根據 index獲取對應的 bucket
var bucket=this.storage[index]
//3.判斷bucket是否為 null
if(bucket===null){
return null
}
//4.有 bucket,那么進行線性查找
for(var i=0;i<bucket.length;i++){
var tuple=bucket[i]
if(tuple[0]===key){
return tuple[1]
}
}
//依然沒有找到,那么返回 null
return null
}
//刪除操作
HashTable.prototype.remove=function(key){
//1.根據 key 獲取對應的index
var index=this.hashFunc(key,this.limit)
//2.根據index 獲取對應的bucket
var bucket=this.storage[index]
//3.判斷bucket 是否為 null
if(bucket===null) return null
//4.有 bucket,那么進行線性查找,並且刪除
for(var i=0;i<bucket.length;i++){
var tuple=bucket[i]
if(tuple[0]===key){
bucket.splice(i,1)
this.count--
return tuple[1]
// 縮小容量
if(this.limit>7&&this.count<this.length*0.25){
this.resize(Math.floor(this.limit/2))
}
}
}
//5.依然沒有找到,return null
return null
}
//其他方法
//判斷 hash 表是否為null
HashTable.prototype.isEmpty=function(){
return this.count==0
}
//獲取哈希表中的個數
HashTable.prototype.size=function(){
return this.count
}
//哈希表的擴容
HashTable.prototype.resize=function(newLimit){
//1.保存舊的數組內容
var oldStorage=this.storage
//2.重置所有的屬性
this.storage=[]
this.count=0
this.limit=newLimit
//3.遍歷oldStorage中所有的bucket
for(var i=0;i<oldStorage.length;i++){
//3.1取出對應的bucket
var backet=oldStorage[i]
//3.2判斷 bucket是否為 null
if(bucket==null) continue
//3.3bucket 中有數據,取出數據,重新插入
for(var j=0;j<bucket.length;j++){
var tuple=bucket[j]
this.put(tuple[0],tuple[1])
}
}
}
}
//測試 hansh 表
var ht=new HashTable()
//2.插入數據
ht.put('abc','123')
ht.put('cba','321')
ht.put('nba','521')
ht.put('mba','520')
//3.獲取方法
alert(ht.get('abc'))
//4.修改方法
ht.put('abc','111')
alert(ht.get('abc'))
//5.刪除方法
ht.remove('abc')
alert(ht.get('abc'))
//封裝一個函數:判斷傳入的函數是否是質數
//特點:只能被 1和自己整數,不能被 2到n-1之間的數字整除
function isPrime(num){
for(var i=2;i<num;i++){
if(num%i==0) return false
}
return true
}
function isPrime2(){
//1.獲取 num的平方根
var temp=parseInt(Math.sqrt(num))
//2.循環判斷
for(var i=0;i<temp;i++){
if(num%i==0) return false
}
return true
}
//驗證函數
alert(isPrime(3))
alert(isPrime(11))
alert(isPrime(123))
alert(isPrime(41))
function HashTable(){
//屬性
this.storage=[]
this.count=0
this.limit=7
//方法
//哈希函數
HashTable.prototype.hashFunc= function (str,size){
//1.定義hashCode變量
var hashCode=0
//2.霍納算法,來計算 hashCode的值
for(var i=0;i<str.length;i++){
hashCode=37* hashCode + str.charCodeAt(i) //獲取編碼
}
//3.取余狀態
var index=hashCode%size
return index
}
//插入和修改操作
HashTable.prototype.put= function (key,value){
//1。根據key 獲取對應的 index
var index=this.hashFunc(key,this.limit)
//2.根據 index取出對應的bucket
var bucket=this.storage[index]
//3.判斷bucket 是否為 null
if(bucket==null){
bucket=[]
this.storage[index]=bucket
}
//4.判斷是否是修改數據
for(var i=0;i<bucket.length;i++){
var tuple=bucket[i]
if(tuple[0]==key){
tuple[1]=value
return
}
}
//5.進行添加操作
bucket.push([key,value])
this.count+=1
//6.判斷是否需要擴容操作
if(this.count>this.limit*0.75){
var newSize=this.limit*2
var newPrime=this.getPrime(newPrime)
this.resize(this.limit*2)
}
}
//獲取操作
HashTable.prototype.get=function(key){
//1.根據 key獲取對應的 index
var index=this.hashFunc(key,this.limit)
//2.根據 index獲取對應的 bucket
var bucket=this.storage[index]
//3.判斷bucket是否為 null
if(bucket===null){
return null
}
//4.有 bucket,那么進行線性查找
for(var i=0;i<bucket.length;i++){
var tuple=bucket[i]
if(tuple[0]===key){
return tuple[1]
}
}
//依然沒有找到,那么返回 null
return null
}
//刪除操作
HashTable.prototype.remove=function(key){
//1.根據 key 獲取對應的index
var index=this.hashFunc(key,this.limit)
//2.根據index 獲取對應的bucket
var bucket=this.storage[index]
//3.判斷bucket 是否為 null
if(bucket===null) return null
//4.有 bucket,那么進行線性查找,並且刪除
for(var i=0;i<bucket.length;i++){
var tuple=bucket[i]
if(tuple[0]===key){
bucket.splice(i,1)
this.count--
return tuple[1]
// 縮小容量
if(this.limit>7&&this.count<this.length*0.25){
var newSize=Math.floor(this.limit /2)
var newPrime=this.getPrime(newSize)
this.resize(newPrime)
}
}
}
//5.依然沒有找到,return null
return null
}
//其他方法
//判斷 hash 表是否為null
HashTable.prototype.isEmpty=function(){
return this.count==0
}
//獲取哈希表中的個數
HashTable.prototype.size=function(){
return this.count
}
//哈希表的擴容
HashTable.prototype.resize=function(newLimit){
//1.保存舊的數組內容
var oldStorage=this.storage
//2.重置所有的屬性
this.storage=[]
this.count=0
this.limit=newLimit
//3.遍歷oldStorage中所有的bucket
for(var i=0;i<oldStorage.length;i++){
//3.1取出對應的bucket
var backet=oldStorage[i]
//3.2判斷 bucket是否為 null
if(bucket==null) continue
//3.3bucket 中有數據,取出數據,重新插入
for(var j=0;j<bucket.length;j++){
var tuple=bucket[j]
this.put(tuple[0],tuple[1])
}
}
}
//判斷某個數字是否是質數
HashTable.prototype.isPrime=function(num){
var temp=parseInt(Math.sqrt(num))
//2.循環判斷
for(var i=0;i<temp;i++){
if(num%i==0) return false
}
return true
}
//獲取質數的方法
HashTable.prototype.getPrime=function(num){
while(!this.isPrime(num)){
num++
}
return num
}
}