javascript数据结构与算法(笔记)---散列表(未完)

本文深入探讨了散列表的基本操作,包括添加、获取和删除元素,并详细解释了散列函数的工作原理。针对数据增多导致的冲突问题,介绍了两种解决策略:分离链接法和线性探查法,为读者提供了理解和实现高效散列表的基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 散列表

 

/*

* put(key,value): 向散列表添加一个新的项

* remove(key): 根据键值从散列表中移除值

* get(key): 返回根据键值检索到的特定的值

*/

 

function HashTable() {

var table = [];

 

var loseloseHashCode = function(key) { // javascript 数据结构和算法 P80 这个散列函数不是很明白干什么用的

var hash = 0;

for(var i = 0; i < key.length; i++) {

hash += key.charCodeAt(i);

}

return hash % 37;

}

 

this.put = function(key, value) {

var position = loseloseHashCode(key);

console.log(position + '-' + key);

table[position] = value;

}

 

this.get = function(key) {

return table[loseloseHashCode(key)];

}

 

this.remove = function(key) {

table[loseloseHashCode(key)] = undefined;

}

 

this.getTb = function() {

return table;

}

}

 

var hash = new HashTable();

hash.put('gandalf', 'gandalf@email.com');

hash.put('rose', 'rose@email.com');

hash.put('andy', 'andy@email.com');

 

console.log(hash.get('gandalf'));

 

hash.remove('gandalf');

console.log(hash.getTb());

function HashTable() {
      var table = [];

      var loseloseHashCode = function(key) { //
        var hash = 0;
        for(var i = 0; i < key.length; i++) {
          hash += key.charCodeAt(i);
        }
        return hash % 37;
      }


      this.put = function(key, value) {
        var position = loseloseHashCode(key);
        console.log(position + '-' + key);
        table[position] = value;
      }

      this.get = function(key) {
        return table[loseloseHashCode(key)];
      }

      this.remove = function(key) {
        table[loseloseHashCode(key)] = undefined;
      }

      this.getTb = function() {
        return table;
      }
    }

    var hash = new HashTable();
    hash.put('gandalf', 'gandalf@email.com');
    hash.put('rose', 'rose@email.com');
    hash.put('andy', 'andy@email.com');

    console.log(hash.get('gandalf'));

    hash.remove('gandalf');
    console.log(hash.getTb());

// 上面方式创建散列表,数据够多的情况下,则会发生冲突问题,就是position不是一一对应关系,而是一对多的关系

// 处理这种冲突有几种方法: 分离链接,线性探查和双散列法

// 首先是 分离链接法:

function HashTable() {
      var table = [];

      var loseloseHashCode = function(key) { //
        var hash = 0;
        for(var i = 0; i < key.length; i++) {
          hash += key.charCodeAt(i);
        }
        return hash % 37;
      }

      var ValuePair = function(key, value) {
        this.key = key;
        this.value = value;
        this.toString = function() {
          return '[' + this.key + '-' + this.value + ']';
        }
      }

      this.put = function(key, value) {
        var position = loseloseHashCode(key);
        if(table[position] == undefined) {
          table[position] = new LinkedList();
        }
        table[position].append(new ValuePair(key, value));
      }

      this.get = function(key) {
        var position = loseloseHashCode(key);
        if(table[position] !== undefined) {
          var current = table[position].getHead();
          while(current.next) {
            if(current.element.key === key) {
              return current.element.value;
            }
            current = current.next;
          }
          if(current.element.key === key) {// 需要查找的元素在链表的第一个或者最后一项
            return current.element.value;
          }
        }
        return undefined;
      }

      this.remove = function(key) {
        var position = loseloseHashCode(key);
        if(table[position] !== undefined) {
          var current = table[position].getHead();
          while(current.next) {
            if(current.element.key === key) {
              table[position].remove(current.element);
              if(table[position].isEmpty()) {
                table[position] = undefined;
              }
              return true;
            } 
            current = current.next;
          }
          // 检查是否为第一个或者最后一个元素
          if(current.element.key === key) {
            table[position].remove(current.element);
            if(table[position].isEmpty()) {
              table[position] = undefined;
            }
            return true;
          }
        }
        return false;
      }

      this.getTb = function() {
        return table;
      }
    }

 

// 二是 线性探查法:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值