Hash table algorithm

Source: Internet
Author: User

Overview

?? Hash, the general translation to do "hash", there is a direct transliteration of "hash", is the arbitrary length of the input (also known as pre-mapping, pre-image), through the hash algorithm, transformed into a fixed-length output, the output is the hash value. This conversion is a compression map, that is, the space of the hash value is usually much smaller than the input space, the different inputs may be hashed to the same output, but not from the hash value to uniquely determine the input value. Simply, a function that compresses messages of any length to a message digest of a fixed length.

theory

http://blog.csdn.net/v_july_v/article/details/6256463
Http://blog.sina.com.cn/s/blog_64e21a0a0100hy8a.html
http://blog.csdn.net/jdh99/article/details/8490704

Code (PHP)

In PHP, object assignment defaults to an object reference assignment, similar to a pointer in C + +.

1. List Node class
//定义链表节点class Node{    public$datanull;//当前节点数据    public$next=null;//下一个节点    publicfunction __construct($data){        $this$data;    }}
2. hash Table class
//define hash table class HashTable{    protected $arr _table=Array();//Hash table     Public  $length=0;//Hash table length    //Based on the input, determine its position in the hash table, that is, the hash function    protected  function hashposition($input){        if(!is_numeric ($input)){//Not numeric, mapped to numeric            $input= CRC32 ($input); }$position=$input%$this->length;//modulo operation        returnAbs$position); }//Initialize     Public  function __construct($length=ten){        $this->length =$length; }//initialization of a hash table     Public  function inithashtable($arr){         for($i=0;$i<$this->length;$i++){$this->arr_table[$i] =NULL; }foreach($arr  as $value){$this->insertnode ($value); }    }//Insert a value     Public  function insertnode($input){        if(Empty($input)){return Array(); }$position=$this->hashposition ($input);//Gets the position of the input in the hash table        $num=1;if(Empty($this->arr_table[$position])){$this->arr_table[$position] =NewNode ($input); }Else{//Zipper method to resolve the conflict between Kazakhstan and Greece            $temp _node=$this->arr_table[$position]; while(true){if($temp _node->next = =NULL){$temp _node->next =NewNode ($input); Break; }$num++;$temp _node=$temp _node->next; }        }return Array($position,$num); }//Delete a value     Public  function deletenode($input){        if(Empty($input)){return false; }$position=$this->hashposition ($input);if(Empty($this->arr_table[$position])){return false; }Else{$per _node=NULL;$temp _node=$this->arr_table[$position]; while($temp _node!=NULL){if($temp _node->data = =$input){ Break; }$per _node=$temp _node;$temp _node=$temp _node->next; }if(Empty($temp _node)){return false; }if($per _node==NULL){//First node, for the node to be deleted                $this->arr_table[$position] =$temp _node->next; }Else{//Not first node                $per _node->next =$temp _node->next; }        }return true; }//Find a node     Public  function searchnode($input){        if(Empty($input)){return Array(); }$position=$this->hashposition ($input);$num=1;if(Empty($this->arr_table[$position])){return Array(); }Else{$temp _node=$this->arr_table[$position]; while($temp _node!=NULL){if($temp _node->data = =$input){ Break; }$num++;$temp _node=$temp _node->next; }if($temp _node==NULL){return Array(); }        }return Array($position,$num); }//Show current hash table     Public  function printhashtable(){        foreach($this->arr_table as $hash=$item){Echo $hash.' | ';$temp _node=$item; while($temp _node!=NULL){Echo $temp _node->data.', ';$temp _node=$temp _node->next; }Echo ' ; }    }}
3. Data Preparation
 $item  = array  ( ' 1 ' , , ,  ' test ' ,  ' test2 ' , , , , " ,  ' 90 ' , ,  ' ", ,  ' TXM ' , ,  ' cat ' , , ,  777 ' ,  ' 333 ' );  
4. Call
$hashTablenew HashTable(10);$hashTable->initHashTable($item);$hashTable->printHashTable();var_dump($hashTable->deleteNode(‘100‘));var_dump($hashTable->searchNode(‘100‘));//$hashTable->printHashTable();
Results

Hash table algorithm

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.