Hash table conflict between PHP core technology and best practices ____php

Source: Internet
Author: User

Hash table conflict between PHP core technology and best practices

Then, after the test, the output value1value2. When

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

Echo $ht->find (' key12 ');

Found output value12value12. What's the reason?

This problem is called a hash table conflict. Since the insert is a string, the algorithm used is to add the ASIIC code of the string, in this way the conflict arises. By printing the hash values of KEY12 and key1, they are all 8, which means that value1 and value12 are stored in the 9th position of the hash table at the same time (the index starts at 0), so the value1 value is overwritten by Value12.

The common methods of conflict resolution are open addressing and zipper method. Because the zipper is easy to understand, this article uses the Zipper method to solve the conflict problem.

The Zipper method solves the conflict:

The approach is to link all the same hash-worthy key byte points in the same list.

Zipper method of the same hash worthy of the key nodes linked to a linked list, then the search for elements must traverse this list, compare the list of each element of the keyword and find the keyword is equal, if the equality is the element we want to find.

Because the node needs to hold the key and data (value), it also records nodes with the same hash value. So create a Hashnode class to store this information.

The HASHNODE structure is as follows:

 
 
? PHP
       Class hashnode{public
              $key;
              public $value;
              public $nextNode;
              Public Function__construct ($key, $value, $nextNode = null) {
       $this->key = $key;
       $this->value = $value;
       $this->nextnode = $nextNode;
}
? >


Hashnode has 3 properties: $key, $value, and $nextnode. The $key is the node's keyword, $value is the value of the node, and $nextnode is pointing to a pointer with the same hash value node. The insertion method is modified as follows:

Public Function Insert ($key, $value) {
                            $index = $this-> hashfunc ($key);
                            Creates 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 insert algorithm flow is as follows:

1 The hash function is used to compute the hash value of the keyword, and the hash value is positioned to the hash table at the specified position.

2 If this position is already occupied by another node, point the $nextnode of the new node to this node, otherwise set the new node $nextnode to null.

3 Save the new node to the current location of the hash table.

After these three steps, the same hash-worthy node is connected to the same list.

The lookup algorithm is modified to the following format:

Public Functionfind ($key) {
                           $index = $this->hashfunc ($key);
                           $current = $this->buckets[$index];
                           while (Isset ($current)) {//traverse the current list
                                  if ($current->key== $key) {  //Compare the current node's keyword
                                         return$current-> value ;//Find Success
                                  }
                                  $current = $current->nextnode;  Compare the next node
                           } return
                           null  ; Lookup failed
               }


The modified lookup algorithm flows as follows:

1 The hash function is used to compute the hash value of the keyword, and the hash value is positioned to the hash table at the specified position.

2) traverse the current list to compare the keywords of each node in the linked list with the search keywords. If equal, the lookup succeeds.

3 If the entire list does not have the keyword to find, the lookup fails.

After testing, the use of zipper method to solve the conflict problem.

refers to this article, please note: http://blog.csdn.net/u012675743/article/details/45048411

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.