PHP Implementation Hash Table

Source: Internet
Author: User

Hash table, also known as a hash list, is mapped to a location in the array through the keyword key to access the record

The function of the hash function is to convert any length of input, through the hash algorithm to the fixed-length output, the output is the hash value

The time complexity of the hash table is O (1)

The following method is used to implement the direct take redundancy

Create a Hashtable

class hashtable{private  $buckets;//Array private  $size  = 12;//records for storing data buckets  The size of the array public function __construct () {$this->buckets = new splfixedarray ($this Size),//splfixedarray is more efficient and can also be used as a generic array instead of}private function hashfunc ($key) {$strlen  = strlen ($ key);  //returns the length of the string $hashval = 0;for ($i  = 0;  $i < $strlen  ;  $i + +) {$ Hashval +=ord ($key [$i]);  //returns the value of ASCII}return  $hashval% $this->size;    //      returns the value after the remainder}public function insert ($key, $value) {$index  =  $this Hashfunc ($key), if (Isset ($this->buckets[$index)) {         $newNode  = new hashnode ($key, $value, $this->buckets[$index]);} else{$newNode  = new hashnode ($key, $value, null);} $this->buckets[$index] =  $newNode;} Public function find ($key) {$index  =  $this->hAshfunc ($key); $current  =  $this->buckets[$index];echo  "</br>"; Var_dump ($current); while ( Isset ($current)) {    //Iterates through the current list if ($current->key== $key) {    // Compare the current node keywords return  $current->value;} $current  =  $current->nextnode;//return  $current->value;} Return null;}}

There may be conflicting issues, such as the hash table pointing to the

Insert two elements, the second element has the same hash value as the first worth hash value

The second element overrides the value of the first element

Then we use the Zipper method to resolve the conflict: With the same hash is worth the byte point link in the same linked list. This list must be traversed when looking for this element.

Create Hashnode

Class Hashnode{public $key;//keywords public $value;//Data public $nextNode;//hashnode to store information public function __construct ($key, $ Value, $nextNode = NULL) {$this->key = $key; $this->value = $value; $this->nextnode = $nextNode;}}

Realize

$ht = new HashTable ();     $ht->insert (' Key1 ', ' value1 '); $ht->insert (' key12 ', ' value12 '); Echo $ht->find (' Key1 ');


PHP Implementation Hash Table

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.