


Detailed explanation of examples of implementing Hash table function in php
Feb 27, 2017 am 09:21 AMphp implements Hash table function
Hash table is one of the most important data structures, also called hash table. Use PHP to implement the function of Hash table. PHP can simulate addition, deletion, modification and query of Hash table. Accessed by mapping the key to a position in the array. The mapping function is called a Hash function, and the array storing records is called a Hash table.
The Hash function converts keys of any length and type into fixed-length output. Different keys may have the same hash.
The time complexity of the Hash table is O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
Let’s test our HashTable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
More elements can be stored after changing the value. However, there is still a problem that different keys may produce the same hash value, so when assigning a value, the subsequent operation will overwrite the previous operation. Let's use the zipper method to solve this conflict problem.
The zipper method resolves conflicts. The zipper method solves conflicts by putting all keys with the same hash value in a linked list. For example, key3 and key14 are both 0 after hashing. Then store these two values where the key of the array is 0, in the form of a linked list. . If you don't understand my words, please look at the example below and you will understand after looking at the printed information. What is the zipper method? It is a linked list.
Create a HashNode class to store the values of key and value, and store another element of the same hash. On the same chain, searching for later elements takes more time. The time complexity is O(n).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
Test our new HashTable
1 2 3 4 5 6 7 8 9 |
|
The above is the detailed explanation of the example of how to implement the Hash table function in PHP, more For related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
