js数据结构和算法 --- 散列表

(一)哈希表(散列表)

在这个例子中,将名称转化为 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())

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值