As one of the most important data structures, hash table is also called hash list. Use PHP to implement the hash table functionality. PHP can simulate the implementation of hash table additions and deletions to check. Accessed by mapping the key to a location in the array. A mapping function is called a hash function, and an array of records is called a hash table.
The hash function converts any length and type key to a fixed length output. The different key may have the same hash.
The time complexity of the hash table is O (1)
| The code is as follows |
Copy Code |
|
<?php
Class hashtable{
Private $arr = Array ();
Private $size = 10;
Public Function __construct () {
Splfixedarray creates an array that is more efficient than a generic array () because it is closer to the array of C. Need to specify dimensions when creating
$this->arr = new Splfixedarray ($this->size);
}
/**
* Description: Simple hash algorithm. Input key, output the integer after the hash
* @param $key
* @return int
*/
Private Function Simplehash ($key) {
$len = strlen ($key);
The value of ASCII for each character in the //key
$asciiTotal = 0;
for ($i =0; $i < $len; $i + +) {
$asciiTotal + + ord ($key [$i]);
}
return $asciiTotal% $this->size;
}
/**
* Description: Assigning value
* @param $key
* @param $value
* @return BOOL
*/
Public function set ($key, $value) {
$hash = $this->simplehash ($key);
$this->arr[$hash] = $value;
return true;
}
/**
* Description: Take value
* @param $key
* @return Mixed
*/
Public function Get ($key) {
$hash = $this->simplehash ($key);
return $this->arr[$hash];
}
Public Function GetList () {
return $this->arr;
}
Public Function Editsize ($size) {
$this->size = $size;
$this->arr->setsize ($size);
}
}
?>
|
The following is a test of our hashtable.
| The code is as follows |
Copy Code |
|
<?php
//test 1
$arr = new HashTable ()
for ($i =0; $i <15; $i + +) {
$arr->set (' Key '. $i, ' value '. $i);
}
Print_r ($arr->getlist ());
//splfixedarray Object
//(
// [0] => Value14
// [1] => value4
// [2] => value5
// [3] => VA Lue6
// [4] => value7
// [5] => Value8
// [6] =&G T Value10
// [7] => value11
// [8] => value12
// [ 9] => value13
//)
///The different key may produce the same hash value, then the operation will overwrite the operation before the assignment.
Test 2
$arr->editsize (15);
For ($i =0 $i <15; $i + +) {
$arr->set (' key '. $i, ' value '. $i);
}
Print_r ($arr->getlist ());
Splfixedarray Object
//(
[0] => Value14
[1] => Value4
[2] => Value0
[3] => value1
[4] => value2
[5] => Value3
[6] => Value10
[7] => Value11
[8] => Value12
[9] => Value13
[Ten] => value14
[One] => value9
[=>]
[=>]
[=>]
//)
?>
|
After you change the value, you can store more elements. But there are still different keys that may produce the same hash value, then the operation will overwrite the problem before the assignment. The problem of this kind of conflict we use zipper method to solve.
The Zipper method solves the conflict. The method of zipper resolution is to put all the same hash value of the key in a linked list, such as Key3 and KEY14 after the hash is 0, then the array of key to 0 store the two values, in the form of the linked list. If you can't understand my text, take a look at the example below and see the print information. Zipper method is what, is linked list.
Creates a Hashnode class that stores the value of key and value, and stores another element of the same hash. On the same chain, the more time-consuming the lookup element is. Time complexity is O (n).
| The code is as follows |
Copy Code |
|
<?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;
}
}
Class newhashtable{
Private $arr;
Private $size = 10;
Public Function __construct () {
$this->arr = new Splfixedarray ($this->size);
}
Private Function Simplehash ($key) {
$asciiTotal = 0;
$len = strlen ($key);
for ($i =0; $i < $len; $i + +) {
$asciiTotal + + ord ($key [$i]);
}
Return $asciiTotal% $this->size;
}
Public function set ($key, $value) {
$hash = $this->simplehash ($key);
if (Isset ($this->arr[$hash])) {
$newNode = new Hashnode ($key, $value, $this->arr[$hash]);
}else{
$newNode = new Hashnode ($key, $value, NULL);
}
$this->arr[$hash] = $newNode;
return true;
}
Public function Get ($key) {
$hash = $this->simplehash ($key);
$current = $this->arr[$hash];
while (!empty ($current)) {
if ($current->key = = $key) {
return $current->value;
}
$current = $current->nextnode;
}
return NULL;
}
Public Function GetList () {
return $this->arr;
}
}
?>
|
To test our new Hashtable.
| The code is as follows |
Copy Code |
<?php
Test 1
$NEWARR = new Newhashtable ();
For ($i =0 $i <30; $i + +) {
$NEWARR->set (' key '. $i, ' value '. $i);
}
Print_r ($newArr->getlist ());
Var_dump ($newArr->get (' Key3 '));
Splfixedarray Object
//(
[0] => Hashnode Object
//(
[Key] => key23
[Value] => value23
[NextNode] => Hashnode Object
//(
[Key] => KEY14
[Value] => value14
[NextNode] => Hashnode Object
//(
[Key] => Key3
[Value] => value3
[NextNode] =>
// )
//
// )
//
// )
//
[1] => Hashnode Object
//(
[Key] => key24
[Value] => Value24
[NextNode] => Hashnode Object
//(
[Key] => KEY15
[Value] => VALUE15
[NextNode] => Hashnode Object
//(
[Key] => Key4
[Value] => value4
[NextNode] =>
// )
//
// )
//
// )
//
[2] => Hashnode Object
//(
[Key] => key25
[Value] => value25
[NextNode] => Hashnode Object
//(
[Key] => KEY16
[Value] => Value16
[NextNode] => Hashnode Object
//(
[Key] => Key5
[Value] => value5
[NextNode] =>
// )
//
// )
//
// )
//
[3] => Hashnode Object
//(
[Key] => key26
[Value] => Value26
[NextNode] => Hashnode Object
//(
[Key] => KEY17
[Value] => Value17
[NextNode] => Hashnode Object
//(
[Key] => Key6
[Value] => value6
[NextNode] =>
// )
//
// )
//
// )
//
[4] => Hashnode Object
//(
[Key] => key27
[Value] => Value27
[NextNode] => Hashnode Object
//(
[Key] => Key18
[Value] => value18
[NextNode] => Hashnode Object
//(
[Key] => Key7
[Value] => Value7
[NextNode] =>
// )
//
// )
//
// )
//
[5] => Hashnode Object
//(
[Key] => key28
[Value] => value28
[NextNode] => Hashnode Object
//(
[Key] => key19
[Value] => value19
[NextNode] => Hashnode Object
//(
[Key] => Key8
[Value] => Value8
[NextNode] =>
// )
//
// )
//
// )
//
[6] => Hashnode Object
//(
[Key] => Key29
[Value] => value29
[NextNode] => Hashnode Object
//(
[Key] => Key10
[Value] => Value10
[NextNode] => Hashnode Object
//(
[Key] => Key9
[Value] => value9
[NextNode] =>
// )
//
// )
//
// )
//
[7] => Hashnode Object
//(
[Key] => Key20
[Value] => VALUE20
[NextNode] => Hashnode Object
//(
[Key] => Key11
[Value] => value11
[NextNode] => Hashnode Object
//(
[Key] => Key0
[Value] => value0
[NextNode] =>
// )
//
// )
//
// )
//
[8] => Hashnode Object
//(
[Key] => Key21
[Value] => VALUE21
[NextNode] => Hashnode Object
//(
[Key] => Key12
[Value] => value12
[NextNode] => Hashnode Object
//(
[Key] => Key1
[Value] => value1
[NextNode] =>
// )
//
// )
//
// )
//
[9] => Hashnode Object
//(
[Key] => Key22
[Value] => Value22
[NextNode] => Hashnode Object
//(
[Key] => KEY13
[Value] => value13
[NextNode] => Hashnode Object
//(
[Key] => Key2
[Value] => value2
[NextNode] =>
// )
//
// )
//
// )
//
//)
String (6) "Value3"
?> |