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