Implement a simple hash table using the link address Method

Source: Internet
Author: User
Tags table definition

Implement a simple hash table using the link address Method


[1] Hash Function Definition

Calculate the storage location of the record corresponding to the key based on the key
Position = f (key)
Function f is the hash function.

[2] hash conflict Definition

Different keys may obtain the same position. The reason is that the key value range is too large. Such as Int.
If you create a table with no conflict at all. The corresponding address space is 4 GB.
. To solve this problem. Different keys can have the same position.

[3] hash table definition

Ing set

[4] designing a good hash function is very important.

[2] shows that a good hash function can make positions distributed in a certain range of memory addresses.
And the position should be as balanced as possible in the range of memory. In this way, the position corresponding to each key is as much as possible.
Less, that is, less hash conflicts. Search faster.

[5] so-called hash bucket Definition

Is the pre-allocated continuous storage space.

[6] Methods to handle conflicts

(1) Open addressing: specify a bucket first, and then position = f (Key, d). If postion already exists, change the parameter D,
Continue to calculate position = f (Key, d) until postion does not exist
Algorithms with different values of D include linear detection and re-partitioning, secondary detection and re-partitioning, and pseudo-random detection and re-partitioning.
I think this name is very advanced. In fact, it is to calculate the unused position more efficiently.
(2) link address Method
The same position item is inserted into a chain table.

Now we use the link address method to implement a simple hash table.

// Hashtable. h <br/> # pragma once <br/> # include <string> <br/> Using STD: string; <br/> typedef unsigned int uint; <br/> // linked list address method <br/> class node <br/> {<br/> Public: <br/> node (INT key, const string & Str); <br/> int key; <br/> string value; <br/> node * Next; <br/> }; <br/> class hashtable <br/>{< br/> Public: <br/> hashtable (); <br/> // insert operation <br/> // return value: whether the insert operation is successful <br/> bool insert (INT key, const string & value ); <br/> // query operation <br/> // return value: whether to find <br/> bool find (INT key ); <br/> // access operation <br/> string & operator [] (INT key); <br/> PRIVATE: <br/> // insert operation <br/> // return value: whether the insertion is successful <br/> bool insert (node ** node, int key, const string & value ); <br/> node * hashtable: findnode (INT key); <br/> // hash function <br/> unsigned int hasher (INT key ); </P> <p> Enum {size = 100}; <br/> node * nodes [size]; </P> <p> };

 

 

// Hashtable. CPP <br/> # include "hashtable. H "<br/> # include <cmath> <br/> # include <cassert> <br/> # include <memory. h> <br/> node: node (INT key, const string & Str) <br/>: Key (key), value (STR), next (0) <br/>{< br/>}< br/> hashtable: hashtable () <br/>{< br/> memset (nodes, 0, size * sizeof (node *); <br/>}< br/> unsigned int hashtable: hasher (INT key) <br/>{< br/> // simplest hash function <br/> return ABS (key) % size; <br />}< Br/> bool hashtable: insert (INT key, const STD: string & value) <br/>{< br/> uint ADR = hasher (key); <br/> node * node = nodes [ADR]; <br/> If (node = 0) <br/> {<br/> nodes [ADR] = new node (Key, value ); <br/>}< br/> else <br/>{< br/> return insert (& node-> next, key, value ); <br/>}< br/> bool hashtable: insert (node ** next, int key, const string & value) <br/> {<br/> node * node = * Next; <br/> If (n Ode = 0) <br/>{< br/> (* Next) = new node (Key, value); <br/> return true; <br/>}< br/> else <br/>{< br/> return insert (& node-> next, key, value ); <br/>}< br/> bool hashtable: Find (INT key) <br/>{< br/> uint ADR = hasher (key ); <br/> node * node = nodes [ADR]; <br/> If (node = 0) <br/>{< br/> return false; <br/>}< br/> else <br/> {<br/> DO <br/> {<br/> If (node-> key = key) <br/>{< br/> return true; <br/>}< B R/> else <br/>{< br/> node = node-> next; <br/>}< br/>}while (node! = 0); <br/> return false; <br/>}< br/> node * hashtable: findnode (INT key) <br/>{< br/> uint ADR = hasher (key); <br/> node * node = nodes [ADR]; <br/> If (node = 0) <br/>{< br/> return 0; <br/>}< br/> else <br/> {<br/> DO <br/> {<br/> If (node-> key = key) <br/>{< br/> return node; <br/>}< br/> else <br/>{< br/> node = node-> next; <br/>}< br/>}while (node! = 0); <br/> return 0; <br/>}< br/> string & hashtable: operator [] (INT key) <br/>{< br/> node * node = findnode (key); <br/> assert (node! = 0); <br/> return node-> value; <br/>}
 

 

// Main. CPP <br/> # include <iostream> <br/> # include <string> <br/> # include "hashtable. H "<br/> using namespace STD; <br/> int main () <br/>{< br/> hashtable HT; <br/> ht. insert (1, "you"); <br/> string value = HT [1]; <br/> cout <value <Endl; <br/> ht. insert (101, "girl"); <br/> value = HT [101]; <br/> cout <value <Endl; <br/> ht. insert (201, "boy"); <br/> value = HT [201]; <br/> cout <value <Endl; <br/> HT [201] = "man"; <br/> cout <HT [201] <Endl; <br/> cin. get (); <br/> return 0; <br/>}


 

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.