Basic wild front-end data structure exercises (5) -- hash

Source: Internet
Author: User

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 simpleHashClass:

  • Attribute
    • this.tableLinear 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)
  1. 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.
  2. Use the Open Chain Method to re-implement Exercise 1.
Exercise ideas

You can introduceHashClass, and then inherit through extendsHashClass and rewriteset/getMethod or add a new method.

Basic wild front-end data structure exercises (5) -- hash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.