Maison > php教程 > PHP源码 > le corps du texte

PHP 实现HASH表

大家讲道理
Libérer: 2016-11-09 14:43:05
original
1343 Les gens l'ont consulté

Hash 表又称散列表,通过关键字Key 映射到数组中一个位置来访问记录

Hash 函数的作用是把任意长度的输入,通过HASH算法变换成固定长度的输出,该输出就是HASH值

HASH表的时间复杂度为O(1)

下文使用直接取余法实现

创建一个hashtable

class HashTable{
     
    private $buckets;          //用于存储数据的数组
    private $size = 12;          //记录buckets 数组的大小
    public function __construct(){
         
        $this->buckets = new SplFixedArray($this->size);
        //SplFixedArray效率更高,也可以用一般的数组来代替
    }
     
    private function hashfunc($key){
        $strlen = strlen($key);  //返回字符串的长度
        $hashval = 0;     
        for($i = 0; $i<$strlen ; $i++){
            $hashval +=ord($key[$i]); //返回ASCII的值
        }
        return $hashval%$this->size;    //    返回取余数后的值
    }
    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)){    //遍历当前链表
                if($current->key==$key){    //比较当前结点关键字
                    return $current->value;
                }
                $current = $current->nextNode;
                //return $current->value;
            }
            return NULL;
        }
     
}
Copier après la connexion

上述可能会有冲突问题,比如HASH表指向的

插入两个元素,第二个元素的HASH值与第一个值得HASH值相同

则第二个元素将覆盖第一个元素的值

这时我们用拉链法解决冲突:具有相同HASH值得字节点链接在同一个链表中。查找这个元素的时候就必须遍历这条链表。

创建 HASHNODE

class HashNode{
            public $key;       //关键字
            public $value;     //数据
            public $nextNode;  //HASHNODE来存储信息
            public function __construct($key,$value,$nextNode = NULL){
                        $this->key = $key;
                        $this->value = $value;
                        $this->nextNode = $nextNode;
                         
            }
         
}
Copier après la connexion

实现

    $ht = new HashTable();
     $ht->insert(&#39;key1&#39;,&#39;value1&#39;);
     //$ht->insert(&#39;key12&#39;,&#39;value12&#39;);
        echo $ht->find(&#39;key1&#39;);
Copier après la connexion


source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!