`
wei1.z
  • 浏览: 3839 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hash表

阅读更多
key elements: underlying array and a hash function
when you want to insert an object and its key, the hash function maps the key to an integer, which indicates the
index in the array. The object is then stored at that index.

Implementation
one:
extremely large array, the result of the function can not be duplicated. (high space complexity)
search time complexity are: O(1).
two:
the result of the function can be duplicated, then, smaller array, each index points to an linked list.(each
position is hash(key)%array_length)
search time complexity is: O(n/array_length).
three:
using a tree to store the indexes and objects, if it is balanced tree, time complexity is O(logn).

1, use what strategy to implement the hash function?
division, mid-square, folding, digit analysis

2, how to deal with overflow?
open addressing, chaining




package topic_1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

//Version one: no collision
//public class HashImplementation {
//    private List<Integer> list;
//    private static final int mode=12;
//    HashImplementation(){
//        list=new ArrayList<Integer>(mode);
//        int m=0;
//        while(m<mode){
//            list.add(null);
//            m++;
//        }
//    }
//   
//    public void put(int key){
//        int index=hashFunction(key);
//        list.set(index, key);
//    }
//   
//    public boolean hasKey(int key){
//        int index=hashFunction(key);
//        if(list.get(index)!=null&&list.get(index)==key){
//            return true;
//        }
//        return false;
//    }
//   
//    private int hashFunction(int key){
//        return key%mode;
//    }
//   
//    public int size(){
//        return list.size();
//    }
//}

//Version two: has collision
//solution one: open addressing
//public class HashImplementation {
//    private List<Integer> list;
//    private static final int mode = 12;
//
//    HashImplementation() {
//        list = new ArrayList<Integer>(mode);
//        int m = 0;
//        while (m < mode) {
//            list.add(null);
//            m++;
//        }
//    }
//
//    public void put(int key) {
//        int index = hashFunction(key);
//        while(list.get(index)!=null){
//            //has collision
//            //solution one: open addressing
//            index=hashFunction(index+1);
//           
//        }
//        list.set(index, new Integer(key));
//    }
//
//    public boolean hasKey(int key) {
//        int index = hashFunction(key);
//        while(list.get(index)!=null){
//            if(list.get(index)==key){
//                return true;
//            }
//            index=hashFunction(index+1);
//        }
//        return false;
//    }
//
//    private int hashFunction(int key) {
//        return key % mode;
//    }
//
//    public int size() {
//        return list.size();
//    }
//}

//Version three: has collision
//solution two: chaining
public class HashImplementation {
    private List<LinkedList<Integer>> list;
    private static final int mode = 12;
   
    HashImplementation() {
      list = new ArrayList<LinkedList<Integer>>(mode);
      int m = 0;
      while (m < mode) {
          list.add(null);
          m++;
      }
    }
   
    public void put(int key) {
      int index = hashFunction(key);
      if(list.get(index)==null){
          LinkedList<Integer> llist=new LinkedList<Integer>();
          llist.add(key);
          list.set(index,llist);
      }else{
          LinkedList<Integer> ll=list.get(index);
          ll.offer(key);
      }
    }
   
    public boolean hasKey(int key) {
      int index = hashFunction(key);
      if(list.get(index)==null){
          return false;
      }else{
          LinkedList<Integer> llist=list.get(index);
          for(int i=0;i<llist.size();i++){
              Integer ints=llist.get(i);
              if(key==ints){
                  return true;
              }
          }
          return false;
      }
    }
   
    private int hashFunction(int key) {
      return key % mode;
    }
   
    public int size() {
      return list.size();
    }
}


分享到:
评论

相关推荐

    hash表的应用

    Hash表应用 (必做) (查找) [问题描述]  设计散列表实现身份证查找系统,对身份证号进行Hash。 [基本要求] (1) 设每个记录有下列数据项:身份证号码(虚构,位数和编码规则与真实一致即可)、姓名、地址; ...

    C语言实现的Hash表(代码)

    C语言实现的Hash表(代码)。C语言实现的Hash表(代码)。C语言实现的Hash表(代码)。C语言实现的Hash表(代码)。

    c语言hash表源码

    c语言hash表源码 来自于开源软件项目

    双向链表与hash表

    从linux内核提取出来的,双向链表和hash表实现。在普通的用户态C程序中,均可使用

    HASH_hash_stm32hash_stm32hash表_stm32f407_

    stm32f407平台上实现的hash算法

    自己实现的hash表

    自己实现的hash表,自己的hash函数,优化了的内存管理

    C#两级嵌套hash表

    封装hashtable的两级hash表,两个键值索引和访问。适合存放稀疏数据,如稀疏矩阵,稀疏表等结构,由于提供key-value的索引遍历,数据稀疏的情况下,相比于传统矩阵遍历的速度更快。

    Hash表模板类

    C++写的hash表模板类,效率还是很不错的。另付有测试代码和可运行文件

    打造最快的Hash表

    打造最快的Hash表.doc 据说来自暴雪 nop nop nop nop

    基于HASH表和SYN计算的TCP包重组方法.rar

    基于HASH表和SYN计算的TCP包重组方法.rar

    hash表操作

    该文档消息描述了hash表的创建、数据插入、数据删除、数据查找以及hash表销毁等操作

    Hash表的分析以及组成原理解析及代码实现.md

    Hash表采用了数组加链表的结构,即一个数组元组中不再是存储单个元素,而是存储一个链表,就好比包租婆收租的时候,一个握把上面挂了一连串的钥匙一样。Hash表的引出是为了减少查询数据库操作所带来的时间问题,将...

    数据结构作业Hash表

    数据结构第16次作业,hash表 Spellchecking Prerequisites, Goals, and Outcomes Prerequisites: Students should have mastered the following prerequisite skills. • Hash Tables - Understanding of the ...

    基于Hash表的代码相似度度量

    本人的数据结构实习作业“基于Hash表的代码相似度度量”,代码简洁明了,可读性强,并附带较多的注释,方便他人查看。一般通过查看注释便能了解程序的结构与功能,方便进行修改。以下是实习作业的具体要求: 对于两...

    hash表学习基础程序

    简单的hash学习程序。 关于Hash的详细介绍请见我的文章http://blog.csdn.net/yankai0219/article/details/8185796

    uthash表操作函数

    hash表操作函数 HASH_ADD_INT HASH_ADD_STR

    base64和hash表

    base64加解密, hash表, fnmatch的windows下的实现简单实现版本。是从mosquitto的auth_plug中copy和https://blog.csdn.net/tttmt/article/details/24824291?utm_source=blogxgwz8 看到的 c语言代码。在qt上测试了

    hash表设计

    hash表的源代码#include &lt;stdio.h&gt; /*标准输入输出函数库*/ #include&lt;stdlib.h&gt; /*标准函数库*/ #include&lt;string.h&gt; #define HASH_LEN 50 /*哈希表的长度 */ #define M 47 #define NAME_N 30 /*人名拼音的最大个...

Global site tag (gtag.js) - Google Analytics