Key PHP technologies and best practices-Hash table conflict _ PHP Tutorial

Source: Internet
Author: User
The core PHP technology conflicts with the Hash table of best practices. PHP core technology and best practice Hash table conflict next article, output value1value2 after testing. when $ ht-insert (key12, value12); Ech PHP core technology conflicts with Hash table of best practices

Hash table conflicts between PHP core technologies and best practices

Next, in the previous article, value1value2 is output after testing. when

$ Ht-> insert ('key12', 'value12 ');

Echo $ ht-> find ('key12,

Value12value12 is output. why?

This problem is called a Hash table conflict. Because the string is inserted, the algorithm used is to add the ASIIC code of the string. According to this method, a conflict occurs. Print the Hash values of key12 and key1 and find that they are both 8. that is to say, value1 and value12 are both stored on the 9th position of the Hash table (the index starts from 0 ), therefore, the value of value1 is overwritten by value12.

Common methods to resolve conflicts are: open addressing and zipper. Because the zipper is easy to understand, this article uses the zipper method to solve conflicts.

Zipper method to resolve conflicts:

The method is to link all the nodes with the same Hash value keyword to the same linked list.

The zipper method connects the same hash value key nodes to a linked list. when searching for an element, you must traverse the linked list and compare the keywords of each element in the linked list with the search keywords, if they are equal, they are the elements to be searched.

Because the node needs to save the keywords (key) and data (value), and record the nodes with the same hash value. Therefore, create a HashNode class to store the information.

The HashNode structure is as follows:

  
 key = $key;       $this ->value = $value;       $this ->nextNode = $nextNode;}}?>


HashNode has three attributes: $ key, $ value, and $ nextNode. $ Key is the key of a node, $ value is the value of a node, and $ nextNode is a pointer to a node with the same Hash value. Modify the insert method as follows:

Public function insert ($ key, $ value) {$ index = $ this-> hashfunc ($ key ); // create a new node if (isset ($ this-> buckets [$ index]) {$ newNode = new HashNode ($ key, $ value, $ this-> buckets [$ index])} else {$ newNode = newHashNode ($ key, $ value, null );} $ this-> buckets [$ index] = $ newNode; // save new node}


The modified algorithm is as follows:

1) use the Hash function to calculate the Hash value of the keyword and use the Hash value to locate the specified position in the Hash table.

2) If this location is already occupied by other nodes, direct $ nextNode of the new node to this node. otherwise, set $ nextNode to null.

3) save the new node to the current position of the Hash table.

After these three steps, the same Hash value node will be connected to the same linked list.

Modify the search algorithm to the following format:

Public functionfind ($ key) {$ index = $ this-> hashfunc ($ key); $ current = $ this-> buckets [$ index]; while (isset ($ current) {// traverse the current linked list if ($ current-> key ==$ key) {// compare the keyword return $ current-> value of the current node; // search successful} $ current = $ current-> nextNode; // compare the next node} Return null; // search failed}


The modified search algorithm process is as follows:

1) use the Hash function to calculate the Hash value of the keyword and use the Hash value to locate the specified position in the Hash table.

2) traverse the current linked list and compare the keywords of each node in the linked list with the search keywords. If they are equal, the search is successful.

3) if no keyword is found for the entire linked list, the search fails.

After testing, the zipper method is used to solve the conflict problem.

Conflicts between the kernel PHP Technology and the Hash table of best practices followed by the previous article. value1value2 is output after testing. when $ ht-insert (key12, value12); Ech...

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.