There are a lot of tutorials on the Internet, and you can search for the basic knowledge on your own.
The exercise is mainly based on orelly's book "data structure and algorithm JavaScript description.
See Code visible: https://github.com/dashnowords/blogs/tree/master/Structure/Hash
Hash basic knowledge
Definition
A hash table is a data ing structure that searches for values based on key codes. The most intuitive application is the dictionary (a realistic dictionary, not a dictionary concept of a data structure ).
Features:
- Insert, delete, and access are fast, and query is slow (for example, you need to use other data structures to improve efficiency ).
- The hash function should distribute location results as much as possible to reduce location collisions.
- A well-designed hash table can find the required data at a constant level.
Common Hash Functions
Division hash
The ××× key is used to modulo the length of the bucket. Therefore, the length of the Bucket generally takes the prime number (the prime number can reduce the hash collision, which is hard to understand ).
Square hash Method
Fibonacci hash
Common Solution to hash collision
Zipper
When a position collision occurs, the linked list or other data structures are used to connect the collision elements.
Linear addressing
When a hash collision occurs, find the first unused position from the current position and place the data to be added. It is generally used when the available space is twice the data size to be stored.
Hash Function Application
Hash Functions are widely used. For example, the hash value added to the file name during webpack packaging and the encrypted information converted from the given information to a fixed-digit string are all hash functions, interested readers can search by themselvesEncryption,Digest algorithmLearn related keywords.
Basic exercises
Write a simpleHash
Class:
- Attribute
This. Table
Linear storage space
- Method
Simplehash ()
Simple Hash Function
Show ()
Display the entire storage Information
Put (value)
Store a value in a hash table
Find (value)
Search Method Based on actual needs
Exercises after class (the eighth exercise in the book)
- Create a dictionary using the linear detection method to save the definition of words. The program needs to contain two parts: the first part reads a group of words and their definitions from the text, and stores them in a hash; the second part asks the user to enter words, program to find the definition of the word.
- Use the Open Chain Method to re-implement Exercise 1.
Exercise ideas
You can introduceHash
Class, and then inherit through extendsHash
Class and rewriteSet/get
Method or add a new method.
Basic wild front-end data structure exercises (5) -- hash