美文网首页
hash table

hash table

作者: Leahlijuan | 来源:发表于2019-10-13 05:33 被阅读0次

[TOC]

direct address

适用于数量小且没有重复的key的情况
都是O(1)时间


image.png

hash table

with direct address, key k store in slot k; while with hashing, we have a hash function h(k)


image.png

collision

collision: two keys hash to the same slot.
solution: chaining; open addressing

chaining
image.png
image.png

load factor = n/m, n is total elements, m is the number of slots.

Search element
worst-case: all elements in the same slots
average case: O(1)
choose a simple uniform hashing (各个元素均匀地分布在各个slots)

Open addressing

每个slot只有一个element,但我们在search的时候,可能要search多个slots

hash function h 不仅需要input k, 还需要input一个i

image.png image.png image.png

当删除一个key的时候,不能找到这个key之后直接标记为null,而应当标记为deleted,不然会影响后面的search

Double hashing效果不错:h(i, k) = (h1(k) + i * h2(k)) mod m

linear probing

如果slot已经被占据了,就看下一个slot

会使得average search time 增加

quadratic probing

类似linear probing,但不是看下一个slot,而是通过加上一个次方的数再mode,寻找下一个探测是否能够插入的slot

double hashing

计算h的时候使用另外两个hash function

hash functions

a good hash function should be simple uniform hashing.

division method

h(k) = k mod m

get the remainder as h(k) ---- 取一个质数,并且这个质数和keys的分布没有关联

a prime not too close to an exact power of 2 is often a good choice of m

The multiplication method

image.png

相关文章

  • Hash Table 之 Collision resolutio

    Hash Table相关术语: A hash table uses a hash function to comp...

  • 算法简单理解(三)

    Hash table Hash table它大概可以说是效能最快的搜索方法。 首先看下Hash table 最基本...

  • Two Sigma Phone Screen Summary

    How does Hash Table work Hash Table stores key, value pai...

  • Hash

    Definition: a hash table (hash map) is a data structure u...

  • LinkedHashMap 与运算替换取模

    今天看到一篇文章提到 hash & (table.length-1) 替代 hash % (table.lengt...

  • Hash Table

    最近开始跟着唐博主刷题,希望自己能跟上吧!第一个专题是Hash Table。对做的每一题,如果有什么要注意的点,我...

  • hash table

    [TOC] direct address 适用于数量小且没有重复的key的情况都是O(1)时间 hash tabl...

  • Hash Table

    ArrayList is hashtable. For hash table, you can keep addi...

  • Hash table

    散列表 基本概念 散列表(Hash table,也叫哈希表),是根据键(Key)而直接访问在内存存储位置的数据结构...

  • hash table

    哈希冲突 1.开放定址法 2.再哈希法 3.链地址法(JAVA官方,默认使用单向链表将元素串起来,在添加元素时,可...

网友评论

      本文标题:hash table

      本文链接:https://www.haomeiwen.com/subject/xechmctx.html