(一)哈希表(散列表)
在这个例子中,将名称转化为 ASCII 码 求和 将 value 存储到 和的位置 找 Jobs 的邮箱找数组的398项就行
(二)代码实现
// 散列函数 key -> number -> items[number] = value
class HashTable {
constructor () {
this.items = []
}
// 通过 AscII 转化方式 字母.charCodeAt() 转化成数字
loseloseHashCode (key) {
var hash = 0
for (var i = 0; i < key.length; i++) {
hash += key[i].charCodeAt()
}
// hash 是每个字母的 AscII 码总和
return hash % 37
}
// 添加
set (key, value) {
var position = this.loseloseHashCode(key)
this.items[position] = value
}
// 取值
get (key) {
return this.items[this.loseloseHashCode(key)]
}
// 获取哈希表
getItems () {
return this.items
}
remove (key) {
this.items[this.loseloseHashCode(key)] = undefined
}
}
var hashTable = new HashTable()
hashTable.set('Jobs', 'jobs@qq.com')
hashTable.set('Lucy', 'Lucy@163.com')
hashTable.set('Mary', 'Mary@qq.com')
console.log(hashTable.get('Jobs'))
console.log(hashTable.getItems())
hashTable.remove('Mary')
console.log(hashTable.getItems())
(三)loseloseHashCode 存在问题
(四)散列冲突解决办法
(1)分离链接法(散列表和链表相结合)
// 辅助类 保存key value
class Node {
constructor (key, value) {
this.key = key
this.value = value
}
}
// 散列函数 key -> number -> items[number] = value
class HashTable_L {
constructor () {
this.items = []
}
// 通过 AscII 转化方式 字母.charCodeAt() 转化成数字
loseloseHashCode (key) {
var hash = 0
for (var i = 0; i < key.length; i++) {
hash += key[i].charCodeAt()
}
// hash 是每个字母的 AscII 码总和
return hash % 37
}
// 添加
set (key, value) {
var position = this.loseloseHashCode(key)
if (this.items[position]) {
this.items[position].append(new Node(key, value))
} else {
var l = new LinkedList() // 创建链表
this.items[position] = l // 数组的 第position 项是链表
this.items[position].append(new Node(key, value)) // 链表添加方法 key也得存储在链表中 不然get的时候仅通过loseloseHashCode拿到的是整个链表
}
}
// 取值
get (key) {
var position = this.loseloseHashCode(key)
if (!this.items[position]) {
return
}
var linkedList = this.items[this.loseloseHashCode(key)] // 使用散列表定位到某一链表
var head = linkedList.getHead() // 获得整个链表
var current = head
// 开始循环查找
while (current) {
if (current.element.key === key) { // 找到
return current.element.value
} else {
current = current.next
}
return undefined
}
}
// 获取哈希表
getItems () {
return this.items
}
remove (key) {
var position = this.loseloseHashCode(key)
// 不存在直接return
if (!this.items[position]) {
return false
}
var linkedList = this.items[this.loseloseHashCode(key)] // 使用散列表定位到某一链表
var head = linkedList.getHead() // 获得整个链表
var current = head
// 开始循环查找
while (current) {
if (current.element.key === key) { // 找到
this.items[position].remove(current.element) // 链表删除 传入key
if (this.items[position].isEmpty()) {
this.items[position] = undefined
}
return true
} else {
current = current.next
}
}
return false
}
}
var hashTable = new HashTable_L()
hashTable.set('Jobs', 'jobs@qq.com')
hashTable.set('Lucy', 'Lucy@163.com')
hashTable.set('Mary', 'Mary@qq.com')
console.log(hashTable.get('Jobs'))
console.log(hashTable.getItems())
hashTable.remove('Mary')
console.log(hashTable.getItems())
(2)线性探查法
如果某一值定位到13,但是13被占用了,就往下找,看14有没有被占,如果14被占,继续向下,15.。。16.。。直到找到第一个没被占的,放进去
// 辅助类 保存key value
class Node {
constructor (key, value) {
this.key = key
this.value = value
}
}
// 散列函数 key -> number -> items[number] = value
class HashTable_L {
constructor () {
this.items = []
}
// 通过 AscII 转化方式 字母.charCodeAt() 转化成数字
loseloseHashCode (key) {
var hash = 0
for (var i = 0; i < key.length; i++) {
hash += key[i].charCodeAt()
}
// hash 是每个字母的 AscII 码总和
return hash % 37
}
// 添加
set (key, value) {
var position = this.loseloseHashCode(key)
if (!this.items[position]) { // 如果没值
this.items[position] = new Node(key, value)
} else {
// 位置被占 继续向下找
index = position + 1
// 直到找到没有值的一项
while (this.items[index]) {
index = index + 1
}
this.position[index] = new Node(key, value)
}
}
// 取值
get (key) {
var position = this.loseloseHashCode(key)
// key不想等 继续向下找
while (this.items[position].key !== key) {
position++
}
// 找到返回value
return this.items[position].value
}
// 获取哈希表
getItems () {
return this.items
}
remove (key) {
var position = this.loseloseHashCode(key)
// key不想等 继续向下找
while (this.items[position].key !== key) {
position++
}
console.log(position)
// 找到删除
this.items[position] = undefined
}
}
var hashTable = new HashTable_L()
hashTable.set('Jobs', 'jobs@qq.com')
hashTable.set('Lucy', 'Lucy@163.com')
hashTable.set('Mary', 'Mary@qq.com')
console.log(hashTable.get('Jobs'))
hashTable.remove('Mary')
console.log(hashTable.getItems())