当前位置:网站首页>Hash table
Hash table
2022-07-04 21:38:00 【Wu_ Candy】
This is the first step of infinite testing 163 Original article
During the interview , Have been asked : Do you know what the underlying data structure of the dictionary is ?
What we are mainly talking about today is what the data structure of hash table looks like ; How hash collision is caused and how to solve hash collision .
1. Definition
Hashtable (Hash table, Also called a hash table ), According to the key code value (Key value) Data structures that are accessed directly . in other words , It accesses records by mapping key values to a location in a table , To speed up the search . This mapping function is called the hash function , The array of records is called a hash table .
The example of dictionary stored value is as follows . In order to understand the data structure of hash table more popularly , Let's look at the picture below :
put("jack","666")
put("Rose","777")
put("Evan","888")
Hash Table Mainly by 2 Part of it is made up of :
- hash function
- Table 「 The bottom layer is an array , The general array size is 2n 」 As for why , For the convenience of bit operation
Hash Functions and Table The role of
hash The main function is to convert key To operate , Generate an index value of an integer index. Corresponding to our case :
hash("Jack") --> index = 14hash("Rose") --> index = 01hash("Evan") --> index = 03
And then according to index The corresponding value Store in Table Array . To complete the key and value Mapping .
2.Hash Collision
Let's add another set of data to the dictionary (Jeffery,999), hypothesis hash("Jeffery") --> index = 03, At this point, you will find that ,Jeffery and Rose Of index All for 3, The conflict , This is it. Hash Collision .
3. solve Hash Collision
solve Hash Common methods of conflict :
- 1. Open addressing (Open Addressing): According to certain rules, probe other addresses , Until you meet an empty place
- 2. Then the hash method (Rr-Hashing)“ Design multiple hash function
- 3. Chain address (Separate Chaining) For example, through a linked list, the same index The elements of
Today we mainly want to introduce the chain address method , If I found hash At the time of the collision , You can use a single linked list to list the same index The elements of , As shown in the figure below :
4. How to survive key Hash value of ?
key Common types may be : Integers 、 Floating point numbers 、 character string 、 Define the object . Different kinds of key, Hash values are generated differently , But the goal is the same :
- 1. Try to make every key The hash value of is unique
- 2. Try to make key All of the information in the calculation
In this paper, the key All are strings , With jack For example :jack The hash value of can be expressed as :j * n^3 + a * n^2 + c * n^1 + k * n^0jack Of ASCII It's all traceable , Therefore, an integer can be obtained through the above calculation T, And then through Table The size of the array & An operation perhaps % Decouple operation , You can get inedx Subscript value .
About integers 、 Floating point numbers 、 Define how the hash value of an object is calculated , If you are interested, you can search , There is a systematic explanation .
summary
Today, I mainly introduce how the hash table data structure stores values , Then it explains how hash collision is generated and how to solve it , Finally, the string key How to calculate the hash value of . I believe you will read this article carefully , I have a clear understanding of what a hash table is .
边栏推荐
- Jerry's ad series MIDI function description [chapter]
- MYSQL 用!=查询不出等于null的数据,解决办法
- Huawei simulator ENSP common commands
- Redis pipeline
- 应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
- Maidong Internet won the bid of Beijing life insurance
- Analyzing the maker space contained in steam Education
- Jerry's ad series MIDI function description [chapter]
- ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
- 杰理之AD 系列 MIDI 功能说明【篇】
猜你喜欢

ApplicationContext 与 BeanFactory 区别(MS)

Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?

Detailed explanation of multi-mode input event distribution mechanism

数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?

IIC (STM32)

B站视频 声音很小——解决办法

华为模拟器ensp的路由配置以及连通测试
![Jerry's ad series MIDI function description [chapter]](/img/d7/348d85eb9f69ffd75612eeba56b16e.png)
Jerry's ad series MIDI function description [chapter]

奋斗正当时,城链科技战略峰会广州站圆满召开

Huawei ENSP simulator realizes communication security (switch)
随机推荐
2021 CCPC Harbin I. power and zero (binary + thinking)
【LeetCode】17、电话号码的字母组合
华为ensp模拟器 DNS服务器的配置
Lambdaquerywrapper usage
Daily question -leetcode1200- minimum absolute difference - array - sort
redis03——Redis的网络配置与心跳机制
Flutter 返回按钮的监听
Kubeadm初始化报错:[ERROR CRI]: container runtime is not running
旋变串判断
Y56. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (29)
MYSQL 用!=查询不出等于null的数据,解决办法
Day24:文件系统
y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)
Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
CAD中能显示打印不显示
Kubedm initialization error: [error cri]: container runtime is not running
华为ensp模拟器实现通信安全(交换机)
maya灯建模
每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
为什么说不变模式可以提高性能