当前位置:网站首页>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 = 14
hash("Rose") --> index = 01
hash("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^0
jack
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 .
边栏推荐
- Maya lamp modeling
- torch. Tensor and torch The difference between tensor
- Use of redis publish subscription
- 杰理之AD 系列 MIDI 功能说明【篇】
- 每日一题-LeetCode1200-最小绝对差-数组-排序
- TCP三次握手,四次挥手,你真的了解吗?
- 旋变串判断
- Jerry added the process of turning off the touch module before turning it off [chapter]
- 杰理之增加进关机前把触摸模块关闭流程【篇】
- 【optimtool.unconstrain】无约束优化工具箱
猜你喜欢
Detailed explanation of multi-mode input event distribution mechanism
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
杰理之AD 系列 MIDI 功能说明【篇】
Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?
IIC (STM32)
Day24:文件系统
超详细教程,一文入门Istio架构原理及实战应用
杰理之增加进关机前把触摸模块关闭流程【篇】
华为ensp模拟器实现通信安全(交换机)
华为ensp模拟器 给路由器配置DHCP
随机推荐
y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)
Flutter TextField示例
Jerry's ad series MIDI function description [chapter]
Huawei ENSP simulator configures ACL access control list
__ init__ () missing 2 required positive arguments
旋变串判断
如何借助自动化工具落地DevOps
Analysis of maker education technology in the Internet Era
In the release version, the random white screen does not display the content after opening the shutter
[early knowledge of activities] list of recent activities of livevideostack
Kubeadm初始化报错:[ERROR CRI]: container runtime is not running
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
Difference between ApplicationContext and beanfactory (MS)
Y56. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (29)
redis RDB AOF
Huawei ENSP simulator enables devices of multiple routers to access each other
Golang interview finishing three resumes how to write
华为ensp模拟器 三层交换机
华为ensp模拟器实现通信安全(交换机)
刘锦程荣获2022年度中国电商行业创新人物奖